如何将MKMapView的用户位置蓝点更改为自定义图像?

34

是否可能将在MKMapView中指示用户位置的蓝色点更改为图像?例如,小汽车或任何.png图像?

输入图像描述

5个回答

56

MKMapViewDelegateviewForAnnotation:方法中,你可能会有以下代码。

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

    if (annotation == mapView.userLocation) return nil;
    ...

如果annotationuserLocation,我们会返回nil以让mapView显示蓝色圆点和圆形动画。如果要为userLocation显示自定义标注,只需删除return nil;这行代码并在那里进行自定义即可。

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

    static NSString* AnnotationIdentifier = @"Annotation";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

    if (!pinView) {

        MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];   
        if (annotation == mapView.userLocation){
           customPinView.image = [UIImage imageNamed:@"myCarImage.png"];
        }
        else{
            customPinView.image = [UIImage imageNamed:@"mySomeOtherImage.png"];
        }
        customPinView.animatesDrop = NO;
        customPinView.canShowCallout = YES;
        return customPinView;

    } else {

        pinView.annotation = annotation;
    }

    return pinView;
}

1
只是顺便问一下 - 有没有简单的方法来维护自定义图像下面的浅蓝色“准确性”圆圈? - Pangolin
1
@Simon,这个在更改图像方面非常有效 - 但是一旦我移动,"car"就会停留在原始点。也就是说,屏幕跟随我移动,但注释没有跟随。这是我唯一的注释,所以我是否可以在mapView:viewForAnnotation中每次返回"car"? - Pangolin
@Nideo,它应该自动移动注释。当您使用默认的蓝点注释时,注释是否移动到新位置?只需检查一下即可。 - EmptyStack
@EmptyStack,我做了类似的事情,并让我的注释在蓝点移动时跟随它,即覆盖它。不幸的是,蓝点会以漂亮的动画效果移动到新位置,而我的注释则会提前跳转,因此每次都会短暂地看到蓝点。 - RodH257
2
创建一个MKAnnotationView而不是MKPinAnnotationView。 - Sjoerd Perfors
显示剩余2条评论

5

以下是Swift 2.0版本,其中可能有多个标记。

在此代码中,CustomAnnotation只是一个MKAnnotation的子类。基本上,如果注释不是您自定义类别之一,则为用户位置标记。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
    // This is false if its a user pin
    if(annotation.isKindOfClass(CustomAnnotation) == false)
    {
        let userPin = "userLocation"
        if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(userPin)
        {
            return dequeuedView
        } else
        {
            let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: userPin)
            mkAnnotationView.image = UIImage(named: C_GPS.ROUTE_WALK_ICON_NAME)
            let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2)
            mkAnnotationView.centerOffset = offset

            return mkAnnotationView
        }

    }

    let annotation = annotation as? CustomAnnotation
    if(annotation == nil)
    {
        return nil
    }

    let endPointsIdentifier = "endPoint"
    if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(endPointsIdentifier)
    {
        dequeuedView.image = annotation!.uiimage
        return dequeuedView
    } else
    {
        let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: endPointsIdentifier)
        mkAnnotationView.image = annotation!.uiimage
        let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2)
        mkAnnotationView.centerOffset = offset

        let gesture = UITapGestureRecognizer(target: self, action: "routeTouched:")
        mkAnnotationView.addGestureRecognizer(gesture)

        return mkAnnotationView
    }
}

1

好的,这里是Swift版本:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    let identifier = "User"

        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil{
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView.canShowCallout = true

        } else {
            annotationView.annotation = annotation
        }

    annotationView.image = UIImage(named: "image")

    return annotationView

}

1
这假设你只有一个注释。不是最好的解决方案。 - Aggressor
1
如果不更改MKPinAnnotationView为MKAnnotationView,它将始终在地图上绘制一个大头针。 - adheus
你需要检查注释是否与用户位置或其他注释相关。 - Satyam

0

这是要改变当前位置蓝点吗?

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> 
MKAnnotationView! {

let identifier = "User"

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

    if annotationView == nil{
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView.canShowCallout = true

    } else {
        annotationView.annotation = annotation
    }

    annotationView.image = UIImage(named: "image")

     return annotationView

}

0
请尝试类似这样的代码。它在Xcode 7和Swift 2中对我有效。
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    // want to show a custom image if the annotation is the user's location.
    guard !annotation.isKindOfClass(MKUserLocation) else {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
        annotationView.image = UIImage(named: "icon_coordinates_self")
        return annotationView
        //return nil
    }

    // for other annotation except current location 
    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
        annotationView = av
    }

    if let annotationView = annotationView {
        // Configure your annotation view here
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "Annotation_map")
    }

    return annotationView
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接