How to change marker icon?

回眸只為那壹抹淺笑 提交于 2019-12-12 21:52:47

问题


I was wonderring if there is a way to change those red pins that are used as markers. And if there is a way, how to do it?


回答1:


you can use 3 types of color pins in mapView are bellow..

  1. MKPinAnnotationColorGreen;
  2. MKPinAnnotationColorPurple
  3. MKPinAnnotationColorRed

and if you want to add customview or image then you can add with programatically

also you can change pin in delegate method of MKMapView like bellow..

- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
 MKPinAnnotationView *pinView  = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
        if (annotation == _mapView.userLocation) 
        {
//            pinView.pinColor = MKPinAnnotationColorRed;
//            return pinView;
            return nil;
        }
        pinView.pinColor = MKPinAnnotationColorGreen; // you can use MKPinAnnotationColorPurple, MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = NO;

        return pinView;
}

and for custom image or pin, use bellow code..

- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"yourImageName"];//add any image which you want to show on map instead of red pins
    annotationView.annotation = annotation;

    return annotationView;
}


来源:https://stackoverflow.com/questions/13761864/how-to-change-marker-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!