谷歌地图API - 通过地址添加标记javascript
问题描述:
我有地址的地方数据库,我想在谷歌地图上添加标记。谷歌地图API - 通过地址添加标记javascript
它只显示默认标记,看起来像geocoder.geocode()什么也不做。举个例子,我试图在“纽约市”添加一个标记,但没有成功。
<script>
var geocoder;
var map;
var address = "new york city";
geocoder = new google.maps.Geocoder();
function initMap() {
var uluru = { lat: -25.363, lng: 131.044 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
codeAddress(address);
}
function codeAddress(address) {
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == 'OK') {
var marker = new google.maps.Marker({
position: address,
map: map
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=XXX&callback=initMap"
async defer></script>
答
您没有得到预期结果的原因是因为您提供的示例中存在不正确的代码位置。在Google地图完全加载之前,您正尝试获取Google Maps API Geocoder的新实例。因此,谷歌地图API地理编码器将不会工作,因为这未捕获的ReferenceError:谷歌未定义。您应该在initMap()函数内部获得Google Maps API Geocoder的新实例。
你可以检查Maps JavaScript API Geocoding 了解更多。
您还可以检查Best Practices When Geocoding Addresses。
另请注意,发布Google Maps APi相关问题时,不应包含您的API_KEY。
这里是整个代码:
<!DOCTYPE html>
<html>
<head>
<title>Geocoding service</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var geocoder;
var map;
var address = "new york city";
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: -34.397, lng: 150.644}
});
geocoder = new google.maps.Geocoder();
codeAddress(geocoder, map);
}
function codeAddress(geocoder, map) {
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
</body>
</html>
现场演示here。
希望它有帮助!
答
你的代码有几个错误。通常情况下,它应该现在看起来OK:
var geocoder;
var map;
var address = "new york city";
function initMap() {
geocoder = new google.maps.Geocoder();
var uluru = { lat: -25.363, lng: 131.044 };
map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
codeAddress(address);
}
function codeAddress(address) {
geocoder.geocode({ 'address': address }, function (results, status) {
console.log(results);
var latLng = {lat: results[0].geometry.location.lat(), lng: results[0].geometry.location.lng()};
console.log (latLng);
if (status == 'OK') {
var marker = new google.maps.Marker({
position: latLng,
map: map
});
console.log (map);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
这是你的错误列表:
- 你必须初始化地理编码时,谷歌地图API是满载。这意味着您必须在
initMap()
中放入以下代码行:geocoder = new google.maps.Geocoder();
。 - 初始化地图时,必须引用全局变量。在你的代码中,你在你的函数中创建一个新的变量映射。但是,你必须通过全球变量映射。
- 何时,您想要获取地理编码器结果的位置,您必须通过以下代码行:
results[0].geometry.location.lat()
。
您可以参考documentation。
如果您有任何疑问,请告诉我。
太好了 - 你介意接受/提升我的回答吗?谢谢 – rafon
当然,我已经投票了,但只有当我有更多的声望点时才会发生。 –
我刚刚提出了你的问题 - 奇怪,因为我认为你已经> 15代表:)。接受它并不需要更高的代表:) – rafon