Marker overlays must be png, jpg, or webp while using MapboxStatic api

183 Views Asked by At

This is how I create markers and add it to array of overlays:

let text = "1"
let url = URL(string: "http://texttopng.azurewebsites.net/Home/TextToPng?text=\(text)")!
let marker = CustomMarker(coordinate: label.location.coordinate, url: url)

but when I try to do a snapshot:

_ = Snapshot(options: options, accessToken: nil).image { image, error in
    //error: Error Domain=MBStaticErrorDomain Code=-1 "Marker overlays must be png, jpg, or webp" UserInfo={NSLocalizedFailureReason=Marker overlays must be png, jpg, or webp}
}

Why it happens like this?

This is what I have tried:

let image = UIImage(named: "icon-card")!
let png = UIImagePNGRepresentation(image)!
let newurl = png.write(withName: "\(text).png")
let marker = CustomMarker(coordinate: label.location.coordinate, url: newurl)

extension Data {
func write(withName name: String) -> URL {

    let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(name)

    try! write(to: url, options: .atomicWrite)

    return url
}
}

enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

I'd like to share my code and it works without any issues:

let text = "Hello"
let urlWithText = URL(string: "http://texttopng.azurewebsites.net/Home/TextToPng?text=\(text)")!
let marker = CustomMarker(coordinate: CLLocationCoordinate2D(latitude: 53.705912, longitude: 23.840836), url: urlWithText)

let camera = SnapshotCamera(
    lookingAtCenter: CLLocationCoordinate2D(latitude: 53.705912, longitude: 23.840836),
    zoomLevel: 12)
let options = SnapshotOptions(
    styleURL: URL(string: "mapbox://styles/mapbox/satellite-streets-v9")!,
    camera: camera,
    size: CGSize(width: 400, height: 200))
options.overlays = [marker]

_ = Snapshot(options: options, accessToken: nil).image { image, error in
    _ = image?.jpegData(compressionQuality: 1.0)?.write(withName: "my_snapshot.jpg")
}

I used your extension for Data for saving of jpg file

extension Data {
    func write(withName name: String) -> URL {
        let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(name)
        print(url.path)
        try! write(to: url, options: .atomicWrite)
        return url
    }
}

and as result I have a shapshot like this: enter image description here I guess you can try use my code or part of it and check with your environment. I use MapboxStatic.swift (0.11.0) Mapbox pod