2010-08-23 29 views
1

所以,我創建了一個CLLocationManager,叫它開始更新,將mapView.showsUserLocation設置爲YES,並返回nil作爲userLocation註釋。 下面是我的一些代碼片段在我UIMapViewController:showsUserLocation不顯示iPhone 4.0中的藍點

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

我在這裏錯過了什麼?

回答

1

當我做到這一點,而不是檢查annotation喜歡你,我做線沿線的東西:

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

謝謝,但這似乎並沒有改變任何東西。仍然沒有點。我可以瀏覽代碼並觀看{return nil; }被叫,所以我不認爲問題在那裏。 – Aeonaut 2010-08-24 01:39:28

+0

這是一個很好的觀點。地圖視圖的userLocation最初爲零,因此最好檢查註釋的類,而不是將其與mapView.userLocation進行比較。 – spstanley 2011-01-09 16:18:38

1

這是mapView.showUserLocation = YES;

您似乎使用了錯誤的語句'set'而不是'show'。

而且僅供參考,您不必爲了獲取用戶位置而弄亂位置管理器。當您設置mapview.showUserLoction = YES並加載地圖時,Corelocation會自動啓動。

希望它能幫助:)

+4

他使用屬性的setter方法來設置該值,這非常有效。它確實是「mapView.showUserLocation = YES;」確實,這要感謝Obj-C 2.0! – spstanley 2011-01-09 16:29:40

0

如果您使用的是iOS的模擬器,那麼你就無法看到藍點。我遇到了完全相同的問題,當我開始用真正的硬件試用我的軟件時,藍點出現了!

+0

這是因爲在模擬器中,無論您的「實際」位置(由您的Internet地址,btw決定),地圖視圖的用戶位置始終設置爲N19º19.901,W122º01.827。 – spstanley 2011-01-09 16:26:23

+0

是的,我知道。我只想說,如果他在模擬器中進行測試,那麼他實際上無法用藍點看到當前位置。全清? – Krismutt 2011-01-10 01:16:42

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

+0

是否正確:在模擬器中,Core Location使用您的實際位置(基於您的IP地址等),但MapKit使用CA的Cupertino作爲您當前的位置。如果您繪製的地圖位於核心位置的中心位置,您將看不到藍點,除非您縮小以查看北加州。不過,它可以在設備上正常工作。 – 2011-01-09 17:39:47

1

確保你不是從地圖上的任何位置移除所有註釋:

例如[mapView removeAnnotations:mapView.annotations]

相關問題