Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add multiple images from a url #1376

Open
asar1 opened this issue Jun 7, 2022 · 12 comments
Open

[Feature] Add multiple images from a url #1376

asar1 opened this issue Jun 7, 2022 · 12 comments
Labels
feature 🍏 When working on a new feature or feature enhancement

Comments

@asar1
Copy link

asar1 commented Jun 7, 2022

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 asar1 added the bug 🪲 Something is broken! label Jun 7, 2022
@bobysdev
Copy link

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

@ZiZasaurus
Copy link
Contributor

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

@asar1
Copy link
Author

asar1 commented Jun 16, 2022

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

@ZiZasaurus
Copy link
Contributor

ZiZasaurus commented Jun 17, 2022

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()
        }

@asar1
Copy link
Author

asar1 commented Jun 18, 2022

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?

@sultan-ali786
Copy link

@asar1 did you find a solution for it ?

@ZiZasaurus
Copy link
Contributor

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.

@sultan-ali786
Copy link

@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
Copy link

sultan-ali786 commented Jun 22, 2022

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

@asar1
Copy link
Author

asar1 commented Jun 22, 2022

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")

@1ec5
Copy link
Contributor

1ec5 commented Jun 22, 2022

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.

@ZiZasaurus ZiZasaurus changed the title Is there any way to add image from a url to load in the circle layer in the unclustered layer? [Feature] Add multiple images from a url Jun 23, 2022
@ZiZasaurus ZiZasaurus added feature 🍏 When working on a new feature or feature enhancement and removed bug 🪲 Something is broken! labels Jun 23, 2022
@ZiZasaurus
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🍏 When working on a new feature or feature enhancement
Projects
None yet
Development

No branches or pull requests

5 participants