我有一個MKMapView類,我想在地圖上顯示由圖像表示的用戶位置。 所以,我讀了這個教程:http://www.switchonthecode.com/tutorials/getting-your-location-in-an-iphone-applicationiOS:獲取位置用戶的最佳方式
這個傢伙在視圖上使用locationmanager,我需要在MKMapView上使用它。
對我來說,最好的方法是創建一個類來控制用戶的位置? 我該怎麼做?
謝謝!
我有一個MKMapView類,我想在地圖上顯示由圖像表示的用戶位置。 所以,我讀了這個教程:http://www.switchonthecode.com/tutorials/getting-your-location-in-an-iphone-applicationiOS:獲取位置用戶的最佳方式
這個傢伙在視圖上使用locationmanager,我需要在MKMapView上使用它。
對我來說,最好的方法是創建一個類來控制用戶的位置? 我該怎麼做?
謝謝!
首先,你想要的位置顯示在地圖上:
yourMapView.showsUserLocation = YES;
然後,你需要處理的委託方法爲用戶位置顯示annotationview:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
// Create a custom MKAnnotationView here that will display your image.
}
}
製作確定你的MKMapView
正確設置了delegate
。
爲此,您需要實施MKAnnotation
& MKMapView
代表。
執行此方法將默認引腳更改爲自定義圖像。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
// Create a custom MKAnnotationView here that will display your image.
//user current location
}
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]])
{
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
else
{
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image=[UIImage imageNamed:@"yourImage.png"];
return annotationView;
}
return nil;
}
請參考tutorial。