0
我已經得到了一個jQuery腳本的幫助,它將通過谷歌地理編碼系統對輸入的位置進行地理編碼。從谷歌地理編碼抓取國家jquery
我需要在country字段中輸入country_name的short_name。對於我的生活,我無法理解我應該如何整合,我所嘗試的一切都不會回報價值。任何人都可以請提供一些見解,以實現這一目標的正確方式嗎?
的jsfiddle:http://jsfiddle.net/spadez/jCB4s/2/
$(function() {
var input = $("#loc"),
lat = $("#lat"),
lng = $("#lng"),
lastQuery = null,
lastResult = null, // new!
autocomplete;
function processLocation(callback) { // accept a callback argument
var query = $.trim(input.val()),
geocoder;
// if query is empty or the same as last time...
if(!query || query == lastQuery) {
callback(lastResult); // send the same result as before
return; // and stop here
}
lastQuery = query; // store for next time
geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: query }, function(results, status) {
if(status === google.maps.GeocoderStatus.OK) {
lat.val(results[0].geometry.location.lat());
lng.val(results[0].geometry.location.lng());
lastResult = true; // success!
} else {
alert("Sorry - We couldn't find this location. Please try an alternative");
lastResult = false; // failure!
}
callback(lastResult); // send the result back
});
}
autocomplete = new google.maps.places.Autocomplete(input[0], {
types: ["geocode"],
componentRestrictions: {
country: "uk"
}
});
google.maps.event.addListener(autocomplete, 'place_changed', processLocation);
$('#searchform').on('submit', function (event) {
var form = this;
event.preventDefault(); // stop the submission
processLocation(function (success) {
if(success) { // if the geocoding succeeded, submit the form
form.submit()
}
});
});
});
按照API,我需要這樣的:
results[]: {
types[]: string,
formatted_address: string,
address_components[]: {
short_name: string,
long_name: string,
types[]: string
},
geometry: {
location: LatLng,
location_type: GeocoderLocationType
viewport: LatLngBounds,
bounds: LatLngBounds
}
}