0

How to adjust size of the compass in mapview for IOS SDK

Use MapboxNavigation 2.5.0, I can change the position,but I don't know how to change size

Bland
  • 1

1 Answers1

0

I'm not sure about MapboxNavigation, but for MapboxMaps 10.0+ I was able to achieve this inspired by the following answer

I assume you do want to reuse the same image. To have it resized to for example 20x20 pts, you can use following code:

let image = mapView.ornaments.compassView.subviews
    .compactMap { ($0 as? UIImageView)?.image }.first?            
    .resized(to: CGSize(width: 20.0, height: 20.0))
rawMapView.ornaments.options.compass.image = image

And, to resize an image:

extension UIImage {
    func resized(to newSize: CGSize) -> UIImage {
        let scale = min(newSize.height / size.height, newSize.width / size.width)
        let size = CGSize(width: size.width * scale, height: size.height * scale)
        let origin = CGPoint(x: (newSize.width - size.width) / 2.0, y: (newSize.height - size.height) / 2.0)
        let imageRect = CGRect(origin: origin, size: size)
        
        return UIGraphicsImageRenderer(size: newSize).image { context in
            context.cgContext.interpolationQuality = .high
            draw(in: imageRect)
        }
    }
}
zalogatomek
  • 666
  • 1
  • 9
  • 19