Search code examples
javaandroidlocationmapboxmapbox-android

[Mapbox-Android]How can I show all the shops,hospitals near users location?


There is a tutorial: https://docs.mapbox.com/help/tutorials/android-store-locator/ This shows how to manually add our shops, creating a dataset and show that in our app. I want to get all the hospitals, banks near a specific user location. How can I achieve this? Please help!


Solution

  • You'll want to use the Mapbox Tilequery API to query the building layer of Mapbox Streets or query your own custom style that has bank/hospital data.

    https://docs.mapbox.com/android/java/overview/tilequery/

    https://docs.mapbox.com/android/java/examples/tilequery/

    Use https://docs.mapbox.com/playground/tilequery/ to play with the Mapbox Tilequery API. For example, https://api.mapbox.com/v4/mapbox.mapbox-streets-v8/tilequery/-122.4052,37.7564.json?limit=19&radius=536&layers=building&dedupe=true&access_token=PASTE_YOUR_MAPBOX_TOKEN_HERE gives you a FeatureCollection of Feature objects. Loop through each Feature to see whether it's type property is hospital, bank, etc.

    https://i.sstatic.net/C3xFN.jpg

    Regarding using the device's location to use for the Tilequery request:

    Set up the LocationComponent as seen in https://docs.mapbox.com/android/maps/examples/show-a-users-location . Then you can do the following to get the device location:

    locationComponent.getLastKnownLocation().getLatitude();
    locationComponent.getLastKnownLocation().getLongitude();
    

    Or if you need to track the device location:

    https://docs.mapbox.com/help/tutorials/android-location-listening

    https://docs.mapbox.com/android/maps/examples/location-change-listening