Lou*_*ley 33 iphone objective-c mkmapview userlocation ios
是否可以将指示用户所在位置的蓝点更改为MKMapView图像?比如一辆小汽车或任何.png形象?

Emp*_*ack 56
在MKMapViewDelegate的viewForAnnotation:方法中,你可能会得到这样的代码.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (annotation == mapView.userLocation) return nil;
...
Run Code Online (Sandbox Code Playgroud)
如果注释是userLocation,则返回nil,让mapView显示蓝点和圆圈动画.为了显示userLocation的自定义注释,只需删除该行并在那里进行自定义.return nil;
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* AnnotationIdentifier = @"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
if (annotation == mapView.userLocation){
customPinView.image = [UIImage imageNamed:@"myCarImage.png"];
}
else{
customPinView.image = [UIImage imageNamed:@"mySomeOtherImage.png"];
}
customPinView.animatesDrop = NO;
customPinView.canShowCallout = YES;
return customPinView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
Run Code Online (Sandbox Code Playgroud)
这是 Swift 2.0 版本,其中您可能有多个引脚。
在此代码中,CustomAnnotation 只是 MKAnnotation 的子类。基本上,如果注释不是您的自定义类之一,那么它就是用户位置引脚。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
// This is false if its a user pin
if(annotation.isKindOfClass(CustomAnnotation) == false)
{
let userPin = "userLocation"
if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(userPin)
{
return dequeuedView
} else
{
let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: userPin)
mkAnnotationView.image = UIImage(named: C_GPS.ROUTE_WALK_ICON_NAME)
let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2)
mkAnnotationView.centerOffset = offset
return mkAnnotationView
}
}
let annotation = annotation as? CustomAnnotation
if(annotation == nil)
{
return nil
}
let endPointsIdentifier = "endPoint"
if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(endPointsIdentifier)
{
dequeuedView.image = annotation!.uiimage
return dequeuedView
} else
{
let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: endPointsIdentifier)
mkAnnotationView.image = annotation!.uiimage
let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2)
mkAnnotationView.centerOffset = offset
let gesture = UITapGestureRecognizer(target: self, action: "routeTouched:")
mkAnnotationView.addGestureRecognizer(gesture)
return mkAnnotationView
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36110 次 |
| 最近记录: |