在情況下,它可以幫助... 這裏是你如何使用谷歌的地理編碼器,以獲得郵編緯度/經度。假設你有id爲「位置」(使用jQueryUI的自動完成構件)的HTML輸入:
// insert a div for autocomplete results
$("#location").after('<div id="location-results"></div>');
$("#location").autocomplete({
appendTo:$("#location-results"),
minLength: 3,
open: acOpen,
select: acSelect,
close: acClose,
source: acSource
});
function acOpen(event, ui){
// no op, but put code here to handle results list open if you need it
}
function acSelect(event, ui){
// fired when user selects a result from the autocomplete list
// assuming you are using the json2.js library for json parsing/decoding
alert(JSON.stringify(ui.item));
// the lat/lng is in ui.item.latLng.lat() and ui.item.latLng.lng()
// ui.item.latLng is of type google.maps.LatLng (see v3 api)
}
function function acClose(event, ui){
// no op, but put code here to handle results list close if you need it
}
function acSource(request, response) {
// implements the autocomplete source for the widget
geocoder.geocode({'address': request.term }, function(results, status) {
if(undefined!==results){
var postal_code='';
var city = '';
var state = '';
var country = '';
response($.map(results, function(item) {
$.each(item.address_components, function(item_i, item_v){
$.each(item_v.types, function(type_i, type_v){
switch(type_v){
case "locality": case "administrative_area_level_3":
city = item_v.long_name;
break;
case "administrative_area_level_2":
break;
case "street_number":
break;
case "route":
break;
case "administrative_area_level_1":
state = item_v.long_name;
break;
case "country":
country = item_v.long_name;
break;
case "postal_code":
postal_code = item_v.long_name;
break;
}
});
});
// the object that gets returned and is ui.item on select event
return {
label : item.formatted_address,
value : item.formatted_address,
latLng : item.geometry.location,
country : country,
state : state,
city : city,
postal_code : postal_code,
source : "google"
};
}));
}
}
}
}
谷歌的地理編碼器可以進行地址解析郵編,沒有問題。
重要的是要注意,谷歌的地理編碼器不能在沒有包括GMAP的情況下使用(這是違反TOS的)。不,你不能在地圖上使用'display =「none」'... – tylerl 2011-08-28 05:05:25