How to use custom icons with mapKit framework?

后端 未结 2 704
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 21:42

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 view         


        
相关标签:
2条回答
  • 2020-12-16 22:20

    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.

    0 讨论(0)
  • 2020-12-16 22:27

    Swift 4 and 5:

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            // Check for type here, not for the Title!!!
            guard !(annotation is MKUserLocation) else {
                // we do not want to return a custom View for the User Location
                return nil
            }
            let identifier = "Identifier for this annotation"
            let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView.image = UIImage(named: "MyMapIcon")
            annotationView.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
    
            annotationView.canShowCallout = false
            return annotationView
        }
    
    0 讨论(0)
提交回复
热议问题