如何使用谷歌地图地理编码获取城市
how to get the city using Google Maps Geocoder
我正在使用Wordpress
开发一个网站,我正在使用一个主题,该主题允许插入名为"属性"的自定义帖子;这些功能是在自定义页面模板中处理的。在这个页面中,我可以添加属性的地址,它有自动完成地理编码的建议。这是代码:
this.initAutoComplete = function(){
var addressField = this.container.find('.goto-address-button').val();
if (!addressField) return null;
var that = thisMapField;
$('#' + addressField).autocomplete({
source: function(request, response) {
// TODO: add 'region' option, to help bias geocoder.
that.geocoder.geocode( {'address': request.term }, function(results, status) {
//$('#city').val(results.formatted_address);
response($.map(results, function(item) {
$('#city').val(item.formatted_address);
return {
label: item.formatted_address,
value: item.formatted_address,
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng()
};
}));
});
},
select: function(event, ui) {
that.container.find(".map-coordinate").val(ui.item.latitude + ',' + ui.item.longitude);
var location = new window.google.maps.LatLng(ui.item.latitude, ui.item.longitude);
that.map.setCenter(location);
// Drop the Marker
setTimeout(function(){
that.marker.setValues({
position: location,
animation: window.google.maps.Animation.DROP
});
}, 1500);
}
});
}
当点击一个地址时,地图会用收到的坐标绘制一个制造商。我想从点击的地址中提取城市,并将该值输入到另一个输入字段中。我该怎么做?谢谢
var city = '';
item.address_components.map(function(e){
if(e.types.indexOf('locality') !== -1 &&
e.types.indexOf('political') !== -1) {
city = e.long_name;
}
});
$('#city').val(city);
相关文章:
- 如何通过javascript从jsongooglemapapi中获取地理编码纬度和经度
- 获取 JSON 编码响应中的数组键
- 如何在javascript变量中从url中获取base64编码值
- 如何在internet explorer中从剪贴板中获取base64编码的图像
- 如何使用angular javascript从(php编码的json)中获取值
- 获取快速编码的 cookie 作为标头值
- 在谷歌地图 api 中使用邻域类型获取formatted_address - 反向地理编码
- 如何使用谷歌地理编码API和PHP获取用户的当前位置
- 自定义共享图标 - 获取URL,编码uri和替换
- 添加事件处理程序以获取按钮单击LeadPage,我可以在其中对正文进行编码
- JSON 编码多维数组获取值
- 如何从 json 编码数组中获取特定值
- 列出使用 Google 地图地理编码器 API 获取的一组地址
- 如何仅从反向地理编码结果中获取邮政编码
- 通过在 ng 自动完成中反向地理编码来获取区域和子区域名称
- 如何在 JavaScript 中获取 JSON 编码数组的长度
- 如何获取外部图像并将其编码为客户端 WebApp 上的 base64
- 如何从网站获取所有图像并用base64编码替换它们
- 解析 json 编码数组并获取值
- 如何使用谷歌地图地理编码获取城市