是否可以将表示用户在MKMapView
中的位置的blue dot更改为图片?例如一辆小汽车或任何.png
图片?
答案 0 :(得分:56)
在 MKMapViewDelegate 的 viewForAnnotation:方法中,你可能会得到这样的代码。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (annotation == mapView.userLocation) return nil;
...
如果注释是 userLocation ,我们返回 nil ,让 mapView 显示蓝点&amp;圆圈动画。要显示 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;
}
答案 1 :(得分:4)
这是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
}
}
答案 2 :(得分:1)
好的,这是Swift版本:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let identifier = "User"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.canShowCallout = true
} else {
annotationView.annotation = annotation
}
annotationView.image = UIImage(named: "image")
return annotationView
}
答案 3 :(得分:0)
这是改变当前位置蓝点???
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) ->
MKAnnotationView! {
let identifier = "User"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.canShowCallout = true
} else {
annotationView.annotation = annotation
}
annotationView.image = UIImage(named: "image")
return annotationView
}
答案 4 :(得分:0)
请尝试这样的事情。它在Xcode 7和swift 2中为我工作。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
annotationView.image = UIImage(named: "icon_coordinates_self")
return annotationView
//return nil
}
// for other annotation except current location
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "Annotation_map")
}
return annotationView
}