如何在Mapbox 3.6中旋转汽车标记图像?
我想根据路线方向旋转标记图像。我已经使用Map box SDK实现了地图。坐标&方向是通过webservice获取的。 我试过imageForMarker,但它没有奏效。 Implemetation是使用自定义的重用标识符遵循如何在Mapbox 3.6中旋转汽车标记图像?
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
let img = imageRotatedByDegrees(oldImage: UIImage(named: "car")!, deg: CGFloat(self.bearing))
return MGLAnnotationImage(image: img, reuseIdentifier: "car")
}
func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage
{
let size = oldImage.size
UIGraphicsBeginImageContext(size)
let bitmap: CGContext = UIGraphicsGetCurrentContext()!
//Move the origin to the middle of the image so we will rotate and scale around the center.
bitmap.translateBy(x: size.width/2, y: size.height/2)
//Rotate the image context
bitmap.rotate(by: (degrees * CGFloat(Double.pi/180)))
//Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
let origin = CGPoint(x: -size.width/2, y: -size.width/2)
bitmap.draw(oldImage.cgImage!, in: CGRect(origin: origin, size: size))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
user928622
回答
尝试,就像这样:
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
let annotation = annotation as! CustomMapAnnotation
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: annotation.id)
if annotationImage == nil {
var image = UIImage(named: "taxi")!
if let heading = annotation.heading {
image = image.imageRotatedByDegrees(degrees: heading)
}
image = image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: image.size.height/2, right: 0))
annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: annotation.id)
}
return annotationImage
}
如果此注释离开屏幕,则返回它总是会面向相同的方向。如果该值以某种方式得到更新,则不会。对于OP跟踪当前用户位置的情况,这可能不起作用。似乎每次都需要使用独特的MGLAnnotationImage,或者至少将可能的标题分解为间隔(例如30或15度),并且对于每个可能的间隔具有单个重用标识符。标题可能不准确,但足够接近。我会选择15或30,因为这样你就会达到所有四个主要方向。 –
看来,这样做的正确方法是使用MGLSymbolStyleLayer与注释,而不是添加注释直接使用mapview委托方法:https://www.mapbox.com/ios-sdk/api/3.6.2/Classes/MGLSymbolStyleLayer.html#/c:objc(cs)MGLSymbolStyleLayer(py)iconRotation
有一个iconRotation属性可以使用,它需要度数。
如何在Mapbox 3.6中旋转汽车标记图像?
相关文章
- 读Java8函数式编程笔记05_数据并行化
- Google谷歌浏览器Post请求预见strict-origin-when-cross-origin跨域问题的 解决办法
- springboot~openfeign开启熔断之后MDC为null的理解
- elementUI使用el-upload上传文件写法总结及避坑,上传图片/视频到本地/服务器以及回显+删除
- 深入使用Element-plus系列-入门按需引入安装
- 读Java8函数式编程笔记04_类库
- ChatGPT初体验——震撼,好用,贾维斯已来
- 《RPC实战与核心原理》学习笔记Day11
- 【宜搭】低代码开发师高级认证实操题1难点指导
- 使用Spring框架进行Web项目开发(初级)
- 前端框架 Electron 使用总结
- 读Java8函数式编程笔记03_高级集合类和收集器
- 《RPC实战与核心原理》学习笔记Day10
- 读Java8函数式编程笔记02_流
- unipush2.0教程
- 《RPC实战与核心原理》学习笔记Day9
- 读Java8函数式编程笔记01_Lambda表达式
- 《RPC实战与核心原理》学习笔记Day8
- 学习笔记——Linux中搜索查找类命令;压缩和解压类;Linux挂载和卸载;进程线程类命令;RPM;YUM
- 学习笔记——CentOS中的帮助命令;常用快捷键;文件目录类命令