Javascript 从谷歌地理代码jquery抓取国家
我得到了一个jquery脚本的帮助,该脚本将通过谷歌地理编码系统对键入的位置进行地理编码 我需要在country字段中输入country值的简短名称。就我的一生而言,我无法思考我应该如何整合,我尝试的一切都没有回报。请任何人就实现这一目标的正确方法提供一些见解 JSFIDDLE: 根据我需要的信息:Javascript 从谷歌地理代码jquery抓取国家,javascript,jquery,google-maps,google-maps-api-3,Javascript,Jquery,Google Maps,Google Maps Api 3,我得到了一个jquery脚本的帮助,该脚本将通过谷歌地理编码系统对键入的位置进行地理编码 我需要在country字段中输入country值的简短名称。就我的一生而言,我无法思考我应该如何整合,我尝试的一切都没有回报。请任何人就实现这一目标的正确方法提供一些见解 JSFIDDLE: 根据我需要的信息: results[]: { types[]: string, formatted_address: string, address_components[]: { short_name:
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
}
}
分析结果,查找一个具有“country”类型的 类似的方法应该可以工作(未经测试):
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
}
}
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());
var address = results[0].address_components;
for (var p = address.length-1; p >= 0; p--) {
if (address[p].types.indexOf("country") != -1) {
// do something useful with the country...
// document.getElementById('postcode').innerHTML= address[p].long_name;
}
}
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
});