2011-06-15 46 views
35

從Geocoder結果獲取不同數組內容時出現問題。從地理編碼結果中獲取城市?

item.formatted_address有效,但不是item.address_components.locality?

geocoder.geocode({'address': request.term }, function(results, status) { 

     response($.map(results, function(item) { 

     alert(item.formatted_address+" "+item.address_components.locality) 
    }    
}); 

//返回的數組是:

"results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "London", 
       "short_name" : "London", 
       "types" : [ "locality", "political" ] 
      } ], 
      "formatted_address" : "Westminster, London, UK" // rest of array... 

任何幫助表示讚賞!

直流

回答

53

得到使用,到底這個工作:

var arrAddress = item.address_components; 
var itemRoute=''; 
var itemLocality=''; 
var itemCountry=''; 
var itemPc=''; 
var itemSnumber=''; 

// iterate through address_component array 
$.each(arrAddress, function (i, address_component) { 
    console.log('address_component:'+i); 

    if (address_component.types[0] == "route"){ 
     console.log(i+": route:"+address_component.long_name); 
     itemRoute = address_component.long_name; 
    } 

    if (address_component.types[0] == "locality"){ 
     console.log("town:"+address_component.long_name); 
     itemLocality = address_component.long_name; 
    } 

    if (address_component.types[0] == "country"){ 
     console.log("country:"+address_component.long_name); 
     itemCountry = address_component.long_name; 
    } 

    if (address_component.types[0] == "postal_code_prefix"){ 
     console.log("pc:"+address_component.long_name); 
     itemPc = address_component.long_name; 
    } 

    if (address_component.types[0] == "street_number"){ 
     console.log("street_number:"+address_component.long_name); 
     itemSnumber = address_component.long_name; 
    } 
    //return false; // break the loop 
}); 
+4

很好地工作,但會更優雅,更少的代碼作爲「轉換」語句。 – Grant 2014-04-24 17:50:03

12

嘗試了不同的要求:

MK107BX

Cleveland Park Crescent, UK

像你說的,返回數組的大小不一致,但對於兩個結果城市似乎是在address_component項目,類型爲[「locality」,「political」]。也許你可以用它作爲一個指標?

編輯:使用jQuery獲得當地對象,將它添加到您的響應功能:

var arrAddress = item.results[0].address_components; 
// iterate through address_component array 
$.each(arrAddress, function (i, address_component) { 
    if (address_component.types[0] == "locality") // locality type 
     console.log(address_component.long_name); // here's your town name 
     return false; // break the loop 
    }); 
+0

害怕不起作用。不知道它會如何,因爲'地址'沒有變量/數組? – v3nt 2011-06-15 15:43:11

+0

對不起,我是關於不同的谷歌地圖API請求。編輯。 – Tsar 2011-06-16 08:17:21

+0

不用擔心!任何想法我怎麼能得到當地?我只是在我的嘗試中得到了未定義; - /如果item.formatted_address的作品我錯了,認爲這也應該 - item.address_components.locality? – v3nt 2011-06-16 09:24:46

10

我不得不創建程序當用戶點擊地圖上的位置,將填寫緯度,經度,市,縣,和州字段用戶的形式。該頁面可以在http://krcproject.groups.et.byu.net發現,它是用戶的形式,讓公衆作出貢獻的數據庫。我並不聲稱自己是專家,但它很棒。

<script type="text/javascript"> 
    function initialize() 
    { 
    //set initial settings for the map here 
    var mapOptions = 
    { 
     //set center of map as center for the contiguous US 
     center: new google.maps.LatLng(39.828, -98.5795), 
     zoom: 4, 
     mapTypeId: google.maps.MapTypeId.HYBRID 
    }; 

    //load the map 
    var map = new google.maps.Map(document.getElementById("map"), mapOptions); 

    //This runs when the user clicks on the map 
    google.maps.event.addListener(map, 'click', function(event) 
    { 
     //initialize geocoder 
     var geocoder = new google.maps.Geocoder() 

     //load coordinates into the user form 
     main_form.latitude.value = event.latLng.lat(); 
     main_form.longitude.value = event.latLng.lng(); 

     //prepare latitude and longitude 
     var latlng = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng()); 

     //get address info such as city and state from lat and long 
     geocoder.geocode({'latLng': latlng}, function(results, status) 
     { 
     if (status == google.maps.GeocoderStatus.OK) 
     { 
      //break down the three dimensional array into simpler arrays 
      for (i = 0 ; i < results.length ; ++i) 
      { 
      var super_var1 = results[i].address_components; 
      for (j = 0 ; j < super_var1.length ; ++j) 
      { 
       var super_var2 = super_var1[j].types; 
       for (k = 0 ; k < super_var2.length ; ++k) 
       { 
       //find city 
       if (super_var2[k] == "locality") 
       { 
        //put the city name in the form 
        main_form.city.value = super_var1[j].long_name; 
       } 
       //find county 
       if (super_var2[k] == "administrative_area_level_2") 
       { 
        //put the county name in the form 
        main_form.county.value = super_var1[j].long_name; 
       } 
       //find State 
       if (super_var2[k] == "administrative_area_level_1") 
       { 
        //put the state abbreviation in the form 
        main_form.state.value = super_var1[j].short_name; 
       } 
       } 
      } 
      } 
     } 
     }); 
    }); 
    } 
</script> 
6

我假設你想要得到的城市,州/省:

var map_center = map.getCenter(); 
reverseGeocode(map_center); 


function reverseGeocode(latlng){ 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var level_1; 
      var level_2; 
      for (var x = 0, length_1 = results.length; x < length_1; x++){ 
       for (var y = 0, length_2 = results[x].address_components.length; y < length_2; y++){ 
        var type = results[x].address_components[y].types[0]; 
        if (type === "administrative_area_level_1") { 
         level_1 = results[x].address_components[y].long_name; 
         if (level_2) break; 
        } else if (type === "locality"){ 
         level_2 = results[x].address_components[y].long_name; 
         if (level_1) break; 
        } 
       } 
      } 
      updateAddress(level_2, level_1); 
     } 
    }); 
} 

function updateAddress(city, prov){ 
    // do what you want with the address here 
} 

不要試圖返回結果,你會發現他們是不確定的 - 的結果異步服務。你必須調用一個函數,比如updateAddress();

1
// Use Google Geocoder to get Lat/Lon for Address 
function codeAddress() { 
    // Function geocodes address1 in the Edit Panel and fills in lat and lon 
    address = document.getElementById("tbAddress").value; 
    geocoder.geocode({ 'address': address }, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      loc[0] = results[0].geometry.location.lat(); 
      loc[1] = results[0].geometry.location.lng(); 
      document.getElementById("tbLat").value = loc[0]; 
      document.getElementById("tbLon").value = loc[1]; 
      var arrAddress = results[0].address_components; 
      for (ac = 0; ac < arrAddress.length; ac++) { 
       if (arrAddress[ac].types[0] == "street_number") { document.getElementById("tbUnit").value = arrAddress[ac].long_name } 
       if (arrAddress[ac].types[0] == "route") { document.getElementById("tbStreet").value = arrAddress[ac].short_name } 
       if (arrAddress[ac].types[0] == "locality") { document.getElementById("tbCity").value = arrAddress[ac].long_name } 
       if (arrAddress[ac].types[0] == "administrative_area_level_1") { document.getElementById("tbState").value = arrAddress[ac].short_name } 
       if (arrAddress[ac].types[0] == "postal_code") { document.getElementById("tbZip").value = arrAddress[ac].long_name } 
      } 
      document.getElementById("tbAddress").value = results[0].formatted_address; 
     } 
     document.getElementById("pResult").innerHTML = 'GeoCode Status:' + status; 
    }) 
} 
0
  //if (arrAddress[ac].types[0] == "street_number") { alert(arrAddress[ac].long_name) } // SOKAK NO 
      //if (arrAddress[ac].types[0] == "route") { alert(arrAddress[ac].short_name); } // CADDE 
      //if (arrAddress[ac].types[0] == "locality") { alert(arrAddress[ac].long_name) } // İL 
      //if (arrAddress[ac].types[0] == "administrative_area_level_1") { alert(arrAddress[ac].short_name) } // İL 
      //if (arrAddress[ac].types[0] == "postal_code") { alert(arrAddress[ac].long_name); } // POSTA KODU 
      //if (arrAddress[ac].types[0] == "neighborhood") { alert(arrAddress[ac].long_name); } // Mahalle 
      //if (arrAddress[ac].types[0] == "sublocality") { alert(arrAddress[ac].long_name); } // İlçe 
      //if (arrAddress[ac].types[0] == "country") { alert(arrAddress[ac].long_name); } // Ülke 
3

我認爲這是一個真正的痛苦,谷歌並沒有提供某種功能,以得到這些。無論如何,我認爲找到合適的對象的最好的辦法是:

geocoder.geocode({'address': request.term }, function(results, status){ 

    response($.map(results, function(item){ 

     var city = $.grep(item.address_components, function(x){ 
     return $.inArray('locality', x.types) != -1; 
     })[0].short_name; 

     alert(city); 
    }    
}); 
0

下面是一些代碼,你可以用lodash JS庫使用:(只是用自己的變量名替換$ scope.x存儲的值)

_.findKey(vObj.address_components, function(component) { 

      if (component.types[0] == 'street_number') { 
       $scope.eventDetail.location.address = component.short_name 
      } 

      if (component.types[0] == 'route') { 
       $scope.eventDetail.location.address = $scope.eventDetail.location.address + " " + component.short_name; 
      } 

      if (component.types[0] == 'locality') { 
       $scope.eventDetail.location.city = component.long_name; 
      } 

      if (component.types[0] == 'neighborhood') { 
       $scope.eventDetail.location.neighborhood = component.long_name; 
      } 

     }); 
0

我用了一個叫做find的lodash函數,它返回謂詞返回true的對象。就如此容易!

let city = find(result, (address) => { 
    return typeof find(address.types, (a) => { return a === 'locality'; }) === 'string'; 
}); 
0

返回locality是否存在。如果不是 - 退貨administrative_area_1

city = results[0].address_components.filter(function(addr){ 
    return (addr.types[0]=='locality')?1:(addr.types[0]=='administrative_area_level_1')?1:0; 
}); 
相關問題