将图像添加到MKPointAnnotation

将图像添加到MKPointAnnotation

本文介绍了将图像添加到MKPointAnnotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能吗?

我在这里执行的操作会给我一个图钉。但是我需要一张图片。 MKAnotnotation对我来说似乎很复杂。

Im performing an action here that gives to me a pin. But i need a image. And MKAnnotation seems complicated for me.

        - (void)abreMapa:(NSString *)endereco {

            NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
                                   [endereco stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
            NSArray *listItems = [locationString componentsSeparatedByString:@","];

            double latitude = 0.0;
            double longitude = 0.0;

            if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
                latitude = [[listItems objectAtIndex:2] doubleValue];
                longitude = [[listItems objectAtIndex:3] doubleValue];
            }
            else {
                //Show error
            }

            CLLocationCoordinate2D coordinate;
            coordinate.latitude = latitude;
            coordinate.longitude = longitude;
            myMap.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);



            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
            [annotation setCoordinate:coordinate];
            [annotation setTitle:@"Some Title"];
            [myMap addAnnotation:annotation];




            // Coloca o icone
            [self.view addSubview:mapa];


        }

谢谢!

推荐答案

您需要设置,并实现

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation

示例代码,这些代码是从Apple开发者网站上提供的MapCallouts示例代码中窃取的。我已经对其进行了修改,以使重点放在重要细节上。您可以在下面看到关键是将图像设置在注释视图上,然后从该方法返回该注释视图。

Here's sample code, stolen from the MapCallouts sample code provided on Apple's developer site. I've modified it to focus on the important details. You can see below that the key is to set the image on an annotation view, and return that annotation view from this method.

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
        static NSString *SFAnnotationIdentifier = @"SFAnnotationIdentifier";
        MKPinAnnotationView *pinView =
            (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
        if (!pinView)
        {
            MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                           reuseIdentifier:SFAnnotationIdentifier] autorelease];
            UIImage *flagImage = [UIImage imageNamed:@"flag.png"];
            // You may need to resize the image here.
            annotationView.image = flagImage;
            return annotationView;
        }
        else
        {
            pinView.annotation = annotation;
        }
        return pinView;
}

我们使用dequeueReusableAnnotationViewWithIdentifier来获取已经创建的视图以重用批注视图。如果未退回,我们将创建一个新的。如果只有几个同时出现,这将阻止我们创建数百个MKAnnotationView。

We use dequeueReusableAnnotationViewWithIdentifier to grab an already created view to make reuse of our annotation views. If one isn't returned we create a new one. This prevents us from creating hundreds of MKAnnotationViews if only a few are ever in sight at the same time.

这篇关于将图像添加到MKPointAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:00