簡體   English   中英

如何在mapkit中顯示用戶位置?

[英]How to display user location in 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}

establishment是我在AddressNote上的一個自定義類。 當establishment具有值時,會發生異常。 如果我沒有設置ShowsUserLocation,一切正常,但當然,我沒有看到用戶位置。

當請求viewForAnnotation ,您需要檢查給定的注釋是否與當前用戶位置相對應。 如果是,則返回nil,否則返回您自己的正常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.

不確定實際問題是什么,但您不需要在每次更新時都這樣做。 創建mapView后設置一次,您應該開展業務。

mapView.showsUserLocation = YES;

您可能需要將地圖縮放到用戶實際所在的區域,以使點可見。

您正在使用錯誤的委托方法。 對於mapview,您應該使用此委托方法來捕獲用戶位置:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM