用户位置的自定义注释视图不移动地图视图 [英] Custom Annotation view for userlocation not moving the mapview

查看:33
本文介绍了用户位置的自定义注释视图不移动地图视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以为 iOS 中的用户当前位置自定义注释视图吗?

我需要用我自己的自定义视图(比如一些 ping pin)删除蓝点(带圆圈).是否有可能做到这一点?

如果这样做,当用户的位置发生变化时,此图钉是否会移动到新位置?还是我们需要以编程方式处理它?<​​/p>

我观察到,如果我们对用户的当前位置使用默认的蓝点,那么当用户位置发生变化时,它会在地图中更新.

我只想知道这是否可以通过我们自己的自定义视图来完成.

解决方案

是的,您可以为用户的位置设置自定义视图.

不幸的是,它比它应该更难实现,因为即使 viewForAnnotation 委托方法的文档 声称如果注释类是 MKUserLocation<,你可以只提供你自己的视图/code>,自定义视图不会继续随着用户的位置移动.事实上,当为 MKUserLocation 返回自定义视图时,地图视图会完全停止更新用户位置(地图视图的 didUpdateUserLocation 委托方法不再触发).我相信这是一个错误.

解决方法是使用 CLLocationManager 和自定义注释...


确保 showsUserLocation 在地图视图中为 NO 或未选中.

使用实现 MKAnnotation 协议的自定义类声明 CLLocationManager 和自定义注释的属性(或者您可以只使用通用的 MKPointAnnotation 班级).

viewDidLoad 或其他合适的地方,创建CLLocationManager,设置它的delegate 并调用startUpdatingLocation.

位置管理器的didUpdateToLocation委托方法(不是地图视图的didUpdateUserLocation委托方法),创建或更新您的自定义注释:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{如果(myUserLocAnnot == nil){self.myUserLocAnnot = [[[MyUserLocClass alloc] init] autorelease];//如果使用ARC,则删除自动释放myUserLocAnnot.title = @"你在这里";myUserLocAnnot.coordinate = newLocation.coordinate;[mapView addAnnotation:myUserLocAnnot];}别的{myUserLocAnnot.coordinate = newLocation.coordinate;}}

最后,在地图视图的 viewForAnnotation 委托方法中,如果注释是您的自定义用户位置注释,您将返回自定义注释视图.

Can we have custom annotation view for the users current location in iOS?

I need to remove the blue dot (with circles) with my own custom view (say some ping pin). Is it possible to do this?

If we do, does this pin move to new location when there is change in user's location? Or do we need to handle it programmatically?

I observed that if we use default blue dot for user's current location, then its gets updated in the map when there is change in user location.

I just want to know if this can be done with our own custom view.

解决方案

Yes, you can have a custom view for the user's location.

Unfortunately, it's harder to implement than it should be because even though the documentation for the viewForAnnotation delegate method claims that you can just supply your own view if the annotation class is MKUserLocation, the custom view does not then continue to move with the user's location. In fact, when a custom view is returned for MKUserLocation, the map view stops updating the user location entirely (the map view's didUpdateUserLocation delegate method no longer fires). I believe this is a bug.

A workaround is to use CLLocationManager and a custom annotation...


Make sure showsUserLocation is NO or unchecked on the map view.

Declare properties for a CLLocationManager and a custom annotation using a custom class that implements the MKAnnotation protocol (or you could just use the generic MKPointAnnotation class).

In viewDidLoad or some other appropriate place, create the CLLocationManager, set its delegate and call startUpdatingLocation.

In the location manager's didUpdateToLocation delegate method (not the map view's didUpdateUserLocation delegate method), create or update your custom annotation:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (myUserLocAnnot == nil)
    {
        self.myUserLocAnnot = [[[MyUserLocClass alloc] init] autorelease];
          //remove the autorelease if using ARC
        myUserLocAnnot.title = @"You are here";
        myUserLocAnnot.coordinate = newLocation.coordinate;
        [mapView addAnnotation:myUserLocAnnot];
    }
    else
    {
        myUserLocAnnot.coordinate = newLocation.coordinate;
    }
}

Finally, in the map view's viewForAnnotation delegate method, you would return a custom annotation view if the annotation is your custom user location annotation.

这篇关于用户位置的自定义注释视图不移动地图视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆