2016-02-04 36 views
0

我試圖在用戶的當前位置放置一個別針。從LocationManger中挑選地點並將其分配給MKAnnotation。但是當我運行該應用程序時,Map會在Map上的不同位置顯示用戶位置和Pin位置。不是很遠,但我想知道爲什麼它不一樣。在模擬器上,自定義引腳和用戶位置相同。使用MKMapView在iOS地圖上定製用戶的當前位置

下面是一些示例代碼。

self.locationManager = [[CLLocationManager alloc] init]; 
[self.locationManager requestWhenInUseAuthorization]; 
[self.locationManager startUpdatingLocation]; 

MyLocation *mylocation = [[MyLocation alloc] initWithName:@"name" address:@"address" coordinate:self.locationManager.location.coordinate] ; 

我需要設置一些精度等級?想知道MKMapView是如何顯示位置的。

+0

你可能會放置引腳當用戶的位置首先被更新。如您所知,該位置可以反彈,直到獲得最準確的用戶位置。您應該將引腳放置在didUpdateLocations中,以便引腳始終放置在最準確和最近的用戶位置。 –

回答

0

試試這個代碼:

#pragma mark - 
#pragma mark MKMapView delegate 
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 
    if(annotationView) 
     return annotationView; 
    else 
    { 
     MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                     reuseIdentifier:AnnotationIdentifier] autorelease]; 
     annotationView.canShowCallout = YES; 
     annotationView.image = [UIImage imageNamed:@"someImage.png"]; 
     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside]; 
     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
     annotationView.rightCalloutAccessoryView = rightButton; 
     annotationView.canShowCallout = YES; 
     annotationView.draggable = YES; 
     return annotationView; 
    } 
    return nil; 
} 

更多的細節更喜歡this

相關問題