我有一个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。