kor*_*gx9 4 objective-c ios mapbox
我开始在项目中使用MapBox。当我添加带有自定义图像的注释时,它居中不正确。它似乎比原始引脚低一些。是否可以更改注释图像的中心点?
应该如何:
现在的状况:
- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation {
MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"driverPinId"];
if (!annotationImage) {
UIImage *image = [UIImage imageNamed:@"DriverPin"];
annotationImageWithImage:image reuseIdentifier:@"driverPinId"];
}
return annotationImage;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您应该使用MGLAnnotationView而不是MGLAnnotationImage因为MGLAnnotationView是UIView的后代,所以您可以像使用普通UIView一样向其中添加任何想要的东西。
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
guard annotation is MGLPointAnnotation else {
return nil
}
guard let castAnnotation = annotation as? MapPointMarker else {
return nil
}
if (!castAnnotation.willUseImage) {
return nil;
}
let identifier = castAnnotation.imageName!
var image: UIImage = UIImage(named: castAnnotation.imageName!)!
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MGLAnnotationView(annotation: annotation, reuseIdentifier: identifier)
let imageView = UIImageView(image: image)
annotationView!.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
annotationView?.addSubview(imageView)
annotationView?.centerOffset = CGVector(dx: 0, dy: -image.size.height / 2.0)
}
return annotationView
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码片段中,我将UIImageView添加到了CommentView并向上annotationView?.centerOffset = CGVector(dx: 0, dy: -image.size.height / 2.0)移动了图像。
希望能有所帮助。
| 归档时间: |
|
| 查看次数: |
846 次 |
| 最近记录: |