2010-03-14 83 views
3

我想显示用户位置的蓝色脉冲点。我这样做:如何在mapkit中显示用户位置?

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

但我最终得到

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

我应该这样做一些其他的方式?

- 编辑 -

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

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

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

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

回答

8

不确定实际的问题是什么,但不需要每次更新时都这样做。在创建mapView之后将其设置一次,并且您应该开展业务。

mapView.showsUserLocation = YES; 

您可能需要将地图缩放到用户实际所在的区域,以使点可见。

+0

谢谢。我现在在viewDidLoad中做这件事,但得到相同的异常。我已经更新了这个问题以反映更多的细节。如果我没有设置位置,它工作正常。 – 4thSpace 2010-03-14 20:31:46

+1

我建议你将它分解为一个裸露的MKMapView并在其上设置相关选项。跳过注释和CLLocationManager的东西,事件处理等。当你有这个工作时,开始加入其余的部分,当问题再次出现时,你对它的位置有更好的了解。 – 2010-03-14 20:47:51

15

当请求viewForAnnotation时,您需要检查给定的注释是否与当前用户位置相对应。如果是,返回零,否则返回你自己的正常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.

[编辑] 的另一种方法是检查实物类注释的:

 
- (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. 

+0

这摆脱了例外,但脉冲蓝点没有出现。我猜这是因为我返回零?不应该返回某种类型的视图吗? – 4thSpace 2010-03-22 04:19:39

+1

你100%确定当你调用[self.mapView setShowsUserLocation:YES];你的mapView不是零?我在我的代码中返回nil,我得到了蓝点:/ – yonel 2010-03-22 09:13:28

+0

我同意yonel--这与我使用的代码几乎相同(我检查注释的类而不是它是否与位置注释相同),并且蓝点确实出现在我的地图上。 – 2010-05-12 01:06:04

0

你使用了错误的委托方法。对于mapview,您应该使用此代理方法来捕获用户位置:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
} 
相关问题