smeijer / leaflet-geosearch

A geocoding/address-lookup library supporting various api providers.
https://smeijer.github.io/leaflet-geosearch/
MIT License
1.03k stars 273 forks source link

Add reverse geocoding #90

Open smeijer opened 7 years ago

ertejaspatel commented 6 years ago

Any plan to add reverse geocoding functionality?

smeijer commented 6 years ago

Yes, but I'm really short on time lately. This project definitely needs a second maintainer.

jongbonga commented 5 years ago

Hi Stephan

first of all, thank you for the plugin, it is really helpful.

Any feedback on the reverse geocoding option?

vinceramcesoliveros commented 4 years ago

I was using axios to use a reverse geocoding in google maps.

// ES7 version
import axios from 'axios';
export const searchReverseLocation = async ({ latitude, longitude }) => {
    try {
        const response = await axios.get(
            `https://maps.googleapis.com/maps/api/geocode/json?latlng=
            ${latitude},${longitude}&key=${API_KEY}`
        );
        return response.data.results[0].formatted_address
    } catch (error) {
        console.error(error);
        return error.toString();
    }
}

I don't know for some providers. I don't have any billing account(except for open street map) for other services that I could test.

clamchoda commented 2 years ago

I have a solution supporting Nominatim, Bing, Google (Geocoding API Key) and MapBox. You can do this with the javascript fetch now. Just change the url to change providers. Aside from Nominatim (no key required), the required api keys are the same ones that would be provided to the leaflet-geosearch provider.

var url = 'https://nominatim.openstreetmap.org/reverse?format=json&lat=42.39330&lon=-82.17380&zoom=18&addressdetails=1';
//var url = 'https://dev.virtualearth.net/REST/v1/Locations/42.39330,-82.17380?o=json&key=' + bingKey;
//var url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=42.39330,-82.17380&key=' + googleKey;
//var url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/-82.17380,42.39330.json?access_token=' + mapBoxKey; // (Notice long,lat)
var result = fetch(url)
.then(response => { 
    if(!response.ok){
        let err = new Error("HTTP status code: " + response.status);
        err.response = response;
        err.status = response.status;
        throw err;
    }
    return response.json();
})
.then(responseJson =>{
    console.log('Reverse Geocode Result', responseJson);
})
.catch(error => console.log('Reverse Geocode',error));