mapbox-maps-ios icon indicating copy to clipboard operation
mapbox-maps-ios copied to clipboard

[Feature] Add multiple images from a url

Open asar1 opened this issue 2 years ago • 12 comments

I've already achieved clustering/un-clustering using circle layers of mapbox, and they can only load static images from the assets folder. is it possible to load image from a url and add it to circle layer? Or is there any other way to achieve this functionality.

asar1 avatar Jun 07 '22 15:06 asar1

Facing the same problem, Anyone got some solution for this?

bobysdev avatar Jun 12 '22 21:06 bobysdev

@asar1 are you referring to the iconImage property within symbol layers?

ZiZasaurus avatar Jun 16 '22 22:06 ZiZasaurus

Yeah, Symbol layer's iconImage or circle layer's any property. Either works for me

asar1 avatar Jun 16 '22 22:06 asar1

You can use the following code to retrieve and display url images as your iconImage:

        if let url = URL(string: "https://media.istockphoto.com/photos/red-apple-picture-id184276818?k=20&m=184276818&s=612x612&w=0&h=QxOcueqAUVTdiJ7DVoCu-BkNCIuwliPEgtAQhgvBA_g=") {
            let task = URLSession.shared.dataTask(with: url) { data, response, error in
                guard let data = data, error == nil else { return }

                DispatchQueue.main.async { /// execute on main thread
                    self.image = UIImage(data: data)

                    try! self.mapView.mapboxMap.style.addImage(self.image, id: "apple")

                    var symbolLayer = SymbolLayer(id: "symbol-layer")
                    symbolLayer.source = sourceID
                    symbolLayer.iconImage = .constant(.name("apple"))

                    try! self.mapView.mapboxMap.style.addSource(source, id: sourceID)
                    try! self.mapView.mapboxMap.style.addLayer(symbolLayer)

                }
            }

            task.resume()
        }

ZiZasaurus avatar Jun 17 '22 17:06 ZiZasaurus

ok, i've got the idea. Thanks @ZiZasaurus. i have to load around 500+ images of users on the map, do you think that approach would be better in that scenario?

asar1 avatar Jun 18 '22 22:06 asar1

@asar1 did you find a solution for it ?

sultan-ali786 avatar Jun 21 '22 09:06 sultan-ali786

This is not something that is currently handled out of the box by Mapbox, as it is not supported by the style spec, however, there are third-party libraries that can assist with managing asynchronous URL requests, such as SDWebImage.

ZiZasaurus avatar Jun 21 '22 18:06 ZiZasaurus

@ZiZasaurus but how can we do it I need use images from URL when we un-cluster and use some static images when clustering can you please help here ?

sultan-ali786 avatar Jun 22 '22 13:06 sultan-ali786

is there any example or code which can help, we don't want to show any image but the markers with image (from URL)

sultan-ali786 avatar Jun 22 '22 13:06 sultan-ali786

This is not something that is currently handled out of the box by Mapbox, as it is not supported by the style spec, however, there are third-party libraries that can assist with managing asynchronous URL requests, such as SDWebImage.

Well i'm not having trouble in downloading and setting the images. I want that functionality in unclustered layer (could be circle or symbol layer), but the issue is that these layers could be 1000+ on the map.

So will i be downloading 1000+ images first to set on the map first like below?

try! self.mapView.mapboxMap.style.addImage(self.image, id: "apple")

asar1 avatar Jun 22 '22 14:06 asar1

I want that functionality in unclustered layer (could be circle or symbol layer), but the issue is that these layers could be 1000+ on the map.

If you need arbitrary images to be loaded on demand rather than upfront when the style loads, consider listening for MapEvents.EventKind.styleImageMissing. In your event handler, you can kick off a network request for the image, then call Style.addImage(_:id:sdf:contentInsets:) asynchronously once you’ve obtained the image. This ensures the application only requests images that the user will actually see. To ensure that the event is fired, set the iconImage property to something unique per URL, such as the URL itself.

1ec5 avatar Jun 22 '22 18:06 1ec5

I'll keep the issue open as a feature request.

ZiZasaurus avatar Jun 23 '22 20:06 ZiZasaurus