如何在Mapbox 3.6中旋转汽车标记图像? [英] How to rotate car marker image in Mapbox 3.6?

查看:373
本文介绍了如何在Mapbox 3.6中旋转汽车标记图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照路线方向旋转标记图像.我已经使用Map box SDK实现了map.坐标与方向是通过webservice获取的. 我尝试了imageForMarker,但没有成功.实施如下

I want to rotate marker image as per the route direction.I had implemented map using Map box SDK. Coordinates & directions are fetched through webservice. I tried imageForMarker but it didnt work. Implemetation is as follow

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
}

推荐答案

看来,正确的方法是将MGLSymbolStyleLayer与注释一起使用,而不是直接添加注释并使用mapview委托方法: https: //www.mapbox.com/ios-sdk/api/3.6.2/Classes/MGLSymbolStyleLayer.html#/c:objc(cs)MGLSymbolStyleLayer(py)iconRotation

It appears that the proper way to do this is using an MGLSymbolStyleLayer with your annotation, instead of adding the annotation directly and using the mapview delegate method: https://www.mapbox.com/ios-sdk/api/3.6.2/Classes/MGLSymbolStyleLayer.html#/c:objc(cs)MGLSymbolStyleLayer(py)iconRotation

有一个iconRotation属性可以使用,该属性需要度数.

There is an iconRotation property you can use, which takes the degrees.

这很不幸,因为我的用例具有多个相同的图像,但标题不同,因此我必须为每个图像添加一个图层.

This is unfortunate as my use case has several of the same image but different headers, so I've got to add a layer for each one.

这篇关于如何在Mapbox 3.6中旋转汽车标记图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆