Google Geocoding - Grab Address along with coordinates

后端 未结 1 1854
挽巷
挽巷 2021-01-15 03:49

So this is what I am after, and I\'ve been told it isn\'t possibly but I am not going to give up just yet!

Let\'s say the user types in \"London\" into my location s

相关标签:
1条回答
  • 2021-01-15 04:27

    try this:

    function getLatLongDetail(latLng) {
    
        var geocoder = new google.maps.Geocoder(); 
        geocoder.geocode({ 'latLng': latLng },
          function (results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                  if (results[0]) {
    
                      var address = "", city = "", state = "", zip = "", country = "";
    
    
                      for (var i = 0; i < results[0].address_components.length; i++) {
                          var addr = results[0].address_components[i];
                          // check if this entry in address_components has a type of country
                          if (addr.types[0] == 'country')
                              country = addr.long_name;
                          else if (addr.types[0] == 'street_address') // address 1
                              address = address + addr.long_name;
                          else if (addr.types[0] == 'establishment')
                              address = address + addr.long_name;
                          else if (addr.types[0] == 'route')  // address 2
                              address = address + addr.long_name;
                          else if (addr.types[0] == 'postal_code')       // Zip
                              zip = addr.short_name;
                          else if (addr.types[0] == ['administrative_area_level_1'])       // State
                              state = addr.long_name;
                          else if (addr.types[0] == ['locality'])       // City
                              city = addr.long_name;
                      }
    
    
                      alert('City: '+ city + '\n' + 'State: '+ state + '\n' + 'Zip: '+ zip + '\n' + 'Country: ' + country);
    
                  }
    
              }
    
          });
    }
    

    EDIT:

    Sample example is here.

    0 讨论(0)
提交回复
热议问题