如何在mapkit中显示用户位置? [英] How to display user location in mapkit?

查看:212
本文介绍了如何在mapkit中显示用户位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示用户位置的蓝色脉冲点。我这样做:

   - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation: (CLLocation *)oldLocation {
//一些其他东西在这里
[self.mapView setShowsUserLocation:YES];
}

但我最终会得到

   -  [MKUserLocation establishment]:无法识别的选择器发送到实例0x125e90 

我应该用其他方法吗?



- EDIT -



也是这样做的,这是我最终得到以上异常:

   - (MKAnnotationView *)mapView:(MKMapView *)_ mapView viewForAnnotation:(AddressNote *)annotation {

if(annotation.establishment!= nil){
// do something}

建立是一个我在AddressNote上的自定义类。当建立具有值时,发生异常。如果我不设置ShowsUserLocation,一切工作正常,但是当然,我没有看到用户位置。

解决方案

请求 viewForAnnotation ,您需要检查给定注释是否对应于当前用户位置。如果是,返回nil,否则返回自己的正常annoationView。

 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
if == self.mapView.userLocation)
return nil;
//然后是您的所有经典注释的代码的其余部分。







另一种方法是检查注释类的类型:




 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
if [annotation isKindOfClass:[MKUserLocation class]])
return nil;
//然后你的所有经典注释的代码的其余部分。



I'd like to display the blue pulsing dot for a user's location. I'm doing this:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation    *)newLocation fromLocation:(CLLocation *)oldLocation{
//some other stuff here
[self.mapView setShowsUserLocation:YES];
}

But I eventually get

-[MKUserLocation establishment]: unrecognized selector sent to instance 0x125e90

Should I be doing this some other way?

-- EDIT --

I'm also doing this, which is where I eventually get the above exception:

- (MKAnnotationView *) mapView:(MKMapView *)_mapView viewForAnnotation:(AddressNote *) annotation{

if(annotation.establishment != nil){
//do something}

establishment is a custom class I have on AddressNote. When establishment has a value, the exception occurs. If I don't set ShowsUserLocation, everything works fine but of course, I don't see the user location.

解决方案

When the viewForAnnotation is requested, you need to check wether or not the given annotation corresponds to the current user location. If yes, return nil, otherwise return your own normal annoationView.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
    if (annotation==self.mapView.userLocation)
        return nil;
    //then the rest of your code for all your 'classic' annotations.

[EDIT] an alternative way is to check the kind of the class of the annotation :

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    //then the rest of your code for all your 'classic' annotations.

这篇关于如何在mapkit中显示用户位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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