在为MKPinAnnotationView上传自定义图像后,我发现大头针偏离了中心。大头针应该位于路线折线的某一点上,并位于mkcircle的中心;然而,大头针似乎在折线的右侧和中心的略北位置。我尝试使用centerOffset属性进行实验,但是当我将值插入该属性时,似乎没有任何变化。下面是代码:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *viewID = @"MKPinAnnotationView";
MKPinAnnotationView *annotationView = (MKPinAnnotationView*)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:viewID];
if(annotationView ==nil){
annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:viewID];
}
annotationView.image = [UIImage imageNamed:@"Pin.png"];
annotationView.enabled = YES;
//doesn't move the pin, still offcentered
annotationView.centerOffset = CGPointMake(0,-50);
return annotationView;
}
有一点需要补充的是,我还注意到使用新的图钉图片时,单击图钉时不会弹出任何内容。之前,使用默认的图钉时,单击图钉后会出现一个文本气泡。因此,我想包含制作并放置地图上图钉的方法的代码。
-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{
MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = coordinate;
annotation.title = @"This is a pin!";
[mapView addAnnotation:annotation];
}
我还尝试更改图钉图像,看看是否会影响MKPinAnnotationView的定位。虽然我能够通过编辑图像来居中图钉,但对于其他折线而言,它并没有居中。如果您能提供任何帮助,将不胜感激!