2016-11-07 2 views
0

У меня есть этот геокодера код:PhoneGap geocoder.geocode получить номер дома из результатов

function codeLatLng(lat, lng) { 
geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(lat, lng); 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     console.log(results) 
     if (results[1]) { 
     //formatted address 
     alert(results[0].formatted_address)//this is a string of all location 

     } else { 
      alert("No results found"); 
     } 
     } else { 
     alert("Geocoder failed due to: " + status); 
     } 
    }); 
    } 

Я хочу, чтобы сохранить улицу и номер дома в различных переменных, как я могу сделать это?

ответ

1

Согласно the documentation, вместо того, чтобы получать formatted_address вы можете получить address_components от ответа геокодирования API, а затем получить, например, street_number:

"address_components" : [ 
    { 
     "long_name" : "1600", 
     "short_name" : "1600", 
     "types" : [ "street_number" ] 
    }, 
    ... 
] 

Вы можете получить желаемые компоненты итерацию по address_components (Я извлекаю street_number в этом примере):

for (var i = 0; i < results[0].address_components.length; i++) { 
    var address_components = results[0].address_components[i]; 
    if (address_components.types[0] == 'street_number') { 
     console.log(address_components.long_name); 
    } 
} 
+0

Вы можете добавить код на javascript, как его получить? – foo

+0

Спасибо! много! – foo