google-maps - 从 Google 地图搜索中获取邮政编码
问题描述
我的代码可以从 long/lat 中找到邮政编码,我可以将其与下面的代码一起使用,但是(我认为)这将被视为几个请求(不想消耗我的 google 配额)。
有没有其他方法可以让我在下面显示邮政编码作为自动完成的一部分,最好是在另一个字段中。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=HERE&libraries=places"></script>
<script>
google.maps.event.addDomListener(window, 'load', initialize);
function initialize() {
var input = document.getElementById('autocomplete_search');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.addListener('place_changed', function () {
var place = autocomplete.getPlace();
// place variable will have all the information you are looking for.
$('#lat').val(place.geometry['location'].lat());
$('#long').val(place.geometry['location'].lng());
});
}
</script>
<title>Google Places Autocomplete InputBox Example Without Showing Map - Tutsmake.com</title>
<style>
.container{
padding: 10%;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12"><h2>Google Places Autocomplete InputBox Example Without Showing Map</h2></div>
<div class="col-12">
<div id="custom-search-input">
<div class="input-group">
<input id="autocomplete_search" name="autocomplete_search" type="text" class="form-control" placeholder="Search" />
<input type="hidden" name="lat">
<input type="hidden" name="long">
</div>
</div>
</div>
</div>
</div>
</body>
</html>
解决方案
您可以获取 PlaceResult 的地址组件并解析它们以获取邮政编码,就像在Place Autocomplete Address Form Example中所做的那样
autocomplete.addListener('place_changed', function () {
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details,
// and then fill-in the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
// place variable will have all the information you are looking for.
$('#lat').val(place.geometry.location.lat());
$('#long').val(place.geometry.location.lng());
});
推荐阅读
- android - 更改日期选择器对话框中按钮栏的颜色
- ios - 用 SwiftUI 2.0 中的工具栏替换 Tabbar
- docusignapi - 从 DocuSign powerform 中删除“稍后完成”按钮
- powershell - Powershell,验证当前目录的最佳方法等于某些东西
- javascript - Larave Vue 发布请求返回 500 内部服务器错误
- material-ui - UNO 材质 BottomNavigationBar 事件处理程序
- c# - 如何将创建的类或对象传递给 AssemblyInitialize?
- django - 在单个日期获得最高数量的订单
- ruby - 面临在 ruby 中创建一对一映射的问题
- python-3.x - Python Google Slides API - 调整表格的宽度和高度以适合页面内