Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

ios - How to use custom icons with mapKit framework?

I am using the MapKit framework to load google maps on my application and I place on map 4 "simulated" places like this:

- (void)viewDidLoad {

[super viewDidLoad];

mapView.delegate = self;

mapView.showsUserLocation = YES;

MKUserLocation *userLocation = mapView.userLocation;

MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance (userLocation.location.coordinate,500,500);
[mapView setRegion:region animated:NO];

//Simulated annotations on the map
CLLocationCoordinate2D poi1Coord , poi2Coord , poi3Coord , poi4Coord;
//poi1 coordinates
poi1Coord.latitude = 37.78754;
poi1Coord.longitude = -122.40718;
//poi2 coordinates
poi2Coord.latitude = 37.78615;
poi2Coord.longitude = -122.41040;
//poi3 coordinates
poi3Coord.latitude = 37.78472;
poi3Coord.longitude = -122.40516;
//poi4 coordinates
poi4Coord.latitude = 37.78866;
poi4Coord.longitude = -122.40623;

MKPointAnnotation *poi1 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *poi2 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *poi3 = [[MKPointAnnotation alloc] init]; 
MKPointAnnotation *poi4 = [[MKPointAnnotation alloc] init];


poi1.coordinate = poi1Coord;
poi2.coordinate = poi2Coord;
poi3.coordinate = poi3Coord;
poi4.coordinate = poi4Coord;

poi1.title = @"McDonald's";
poi1.subtitle = @"Best burgers in town";
poi2.title = @"Apple store";
poi2.subtitle = @"Iphone on sales..";
poi3.title = @"Microsoft";
poi3.subtitle = @"Microsoft's headquarters";
poi4.title = @"Post office";
poi4.subtitle = @"You got mail!";

[mapView addAnnotation:poi1];
[mapView addAnnotation:poi2];
[mapView addAnnotation:poi3];
[mapView addAnnotation:poi4];    

}

The code is fairly simple. What i want to do is instead of using the typical red pin on my google map , use my own images to show different places. Is there a simple/straightforward way to do that, cause I got much confused from the samples I already found.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can change the annotation image by using following code,

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    if ([[annotation title] isEqualToString:@"Current Location"]) {    
        return nil;
    }

    MKAnnotationView *annView = [[MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    if ([[annotation title] isEqualToString:@"McDonald's"])
         annView.image = [ UIImage imageNamed:@"mcdonalds.png" ];
    else if ([[annotation title] isEqualToString:@"Apple store"])
         annView.image = [ UIImage imageNamed:@"applestore.png" ];
    else
         annView.image = [ UIImage imageNamed:@"marker.png" ];
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];   
    [infoButton addTarget:self action:@selector(showDetailsView) 
             forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView = infoButton;
    annView.canShowCallout = YES;
    return annView;
}

where "marker.png" is your image file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...