showsUserLocation在iPhone 4.0中不显示蓝点

时间:2010-08-23 23:17:05

标签: iphone cocoa-touch mkmapview mkannotation userlocation

所以,我创建了一个CLLocationManager,调用它来开始更新,将mapView.showsUserLocation设置为YES,并为userLocation注释返回nil。 以下是我在UIMapViewController中的代码中的一些片段:

- (void)viewDidLoad
{
     CLLocationManager *locationManager = [[CLLocationManager alloc] init];
     [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{ 
 CLLocationCoordinate2D userCoordinate = locationManager.location.coordinate;
 [map setCenterCoordinate:userCoordinate animated:YES];
 [map setShowsUserLocation:YES];  
}

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

 MKAnnotationView *mapIconView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:@"mapIconView"];

 // Don't mess with the user location annotation
     if (annotation == mapView.userLocation)
              return nil;

    // etc.
}     

这一切似乎都非常简单。一切正常 - 地图缩放到我应该的位置,我已经确认所有方法都按预期调用 - 但没有蓝点。无论我说的在哪里或多少次,都无法获得我生命中的蓝点

mapView.setUserLocation = YES;

我在这里缺少什么?

5 个答案:

答案 0 :(得分:1)

当我这样做时,不是像你一样检查annotation,而是按照以下方式做点什么:

if([annotation class] == MKUserLocation.class) {
    return nil;
}

答案 1 :(得分:1)

是mapView.showUserLocation = YES;

您似乎使用了错误的声明'set'而不是'show'。

而且仅供参考,您不必为了获得用户位置而与位置管理员混淆。设置mapview.showUserLoction = YES并加载地图后,Corelocation会自动触发。

希望有所帮助:)

答案 2 :(得分:1)

我遇到了同样的问题,蓝点不会出现。事实证明它出现了,而不是我认为的那样。

与我的问题类似,您的代码看起来既处理位置更新又要求MKMapView跟踪用户位置。请注意,在模拟器中,这些是两个不同的位置!当MKMapView跟踪模拟器中的用户位置时,无论您的“实际”位置如何,它都会为您提供位于加州库比蒂诺的Apple的位置。

使用此代码并让MKMapView跟踪代表的位置而不是自己跟踪位置可以显示蓝点:

- (void)mapView:(MKMapView *)theMapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [theMapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}

你可能想要放大一点。我使用此博客条目中的方便代码来执行此操作:Set the Zoom Level of an MKMapView

答案 3 :(得分:1)

确保您没有从地图中删除所有注释:

e.g。 [mapView removeAnnotations:mapView.annotations]

答案 4 :(得分:0)

如果您使用的是iOS模拟器,则无法看到蓝点。我有完全相同的问题,当我开始尝试使用真实硬件的软件时,蓝点出现了!