抓取国家从谷歌地理代码jquery
Grabbing country from google geocode jquery
我已经帮助得到一个jquery脚本,将地理编码一个键入的位置通过谷歌地理编码系统。
我需要国家值的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
}
}
解析结果,查找类型为"country"的
查找类型为"postal_code"的结果的示例
像这样的东西应该可以工作(未测试):
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
});
相关文章:
- 如何动态插入jquery代码
- 如何在php变量中嵌入JQuery代码
- 将 jQuery 代码添加到 Index.aspx 页面
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- Jquery:代码在rails中的页面加载时未执行
- 您有更好的动态方式来缩短复杂的代码jquery吗
- Don't工作警报代码jQuery
- <script src=“//代码.jquery.com/jquery-1.11.1.min.js”></
- 如何确保我的值已添加并存在,然后运行另一个代码jquery
- 命令代码jquery文件错误
- 解释此代码 jQuery
- 使用 JavaScript 编写 HTML 代码 (jquery)
- 将 Ruby 数组传递给 Ruby on Rails 中 Haml 视图中的 JavaScript 代码 (jquery
- 通过国家代码jquery获取时区
- 什么是错误的这个自动完成代码(JQuery-UI自动完成)
- 抓取国家从谷歌地理代码jquery
- 检查列表中的元素是否有来自变量的SRC,然后运行代码- jQuery / Javascript
- 在附加代码jquery中包含附加内容
- 将变量php问题化为代码jquery