【问题标题】:How to change MKMapView's user-location blue dot to an image of choice?如何将 MKMapView 用户位置蓝点更改为选择的图像?
【发布时间】:2011-09-11 17:08:31
【问题描述】:

是否可以将表示用户在MKMapView 中的位置的blue dot 更改为图像?例如一辆小汽车或任何.png 图像?

【问题讨论】:

    标签: iphone objective-c ios mkmapview userlocation


    【解决方案1】:

    MKMapViewDelegateviewForAnnotation: 方法中,您可能会有这样的代码。

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    
        if (annotation == mapView.userLocation) return nil;
        ...
    

    如果 annotationuserLocation 我们返回 nilmapView 显示蓝点和圆圈动画.为了显示我们对 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;
    }
    

    【讨论】:

    • 附带说明 - 是否有一种简单的方法可以保持自定义图像下方的浅蓝色“准确性”圆圈?
    • @Nideo,我猜你必须用 userLocation 注释的 coordinates 创建一个注释。在 didUpdateToLocation: 方法中,您还需要更新新注释。但我不知道这是否会按您的预期工作。这可能会在条件 if (annotation == mapView.userLocation) 中失败,因为您的新注释还包含用户位置的坐标。不过我不确定。试试看!
    • @Simon,这在改变图像方面效果很好——但只要我四处走动——“汽车”就会停留在原来的位置。 IE。屏幕跟随我,但不是注释。这是我将拥有的唯一注释,所以我是否可以每次都在 mapView:viewForAnnotation 中返回“汽车”?
    • @Nideo,它应该会自动移动注释。使用默认蓝点注释时,注释是否会移动到新位置?只需检查一下
    • 创建一个 MKAnnotationView 而不是一个 MKPinAnnotationView
    【解决方案2】:

    这里是 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
        }
    }
    

    【讨论】:

      【解决方案3】:

      好的,这里是 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 个注释。不是最好的解决方案。
      • 您需要将 MKPinAnnotationView 更改为 MKAnnotationView,否则它将始终在地图上绘制图钉。
      • 您必须检查注释是否与用户位置或其他注释有关。
      【解决方案4】:

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

      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
      
      }
      

      【讨论】:

        【解决方案5】:

        请尝试这样的事情。它在 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
        }
        

        【讨论】:

          猜你喜欢
          • 2013-01-03
          • 1970-01-01
          • 1970-01-01
          • 2011-09-18
          • 1970-01-01
          • 2011-08-30
          • 2014-08-29
          • 2012-09-16
          • 2016-08-22
          相关资源
          最近更新 更多