Get definite City name in Google maps reverse geocoding

前端 未结 3 586
野的像风
野的像风 2021-01-03 01:05

Using the Google Maps Geocoding API, i\'m able to get the formatted address for a particular coordinate. To get the exact city name, I\'m doing the following:



        
3条回答
  •  有刺的猬
    2021-01-03 02:01

    For what it's worth, I was looking for something similar and am trying https://plus.codes/

    If you strip the encoded bit it yields a fairly consistent city, state, country name:

    const extractCityName = latlng => {
      googleMapsClient.reverseGeocode({ latlng }, (err, response) => {
        if (!err) {
          return response.json.plus_code.compound_code.split(' ').slice(1).join(' ');
        }
      });
    };
    
    
    // examples: 
    
    console.log(extractCityName(40.6599718,-73.9817292));
    // New York, NY, USA
    
    console.log(extractCityName(37.386052, -122.083851));
    // Mountain View, CA, USA
    
    console.log(extractCityName(51.507351, -0.127758));
    // Westminster, London, UK
    

提交回复
热议问题