Google Maps API,从位置名称错误中添加标记:请求被拒绝

时间:2018-12-07 09:44:57

标签: google-maps

我正在尝试在Google Maps API上基于位置名称基于this implementation的标记。

我找回了一个请求被拒绝的错误。

enter image description here

与此类似的预期效果

enter image description here

var geocoder;
var map;
var address = "Tumbili Rd, Off Langata South Rd, Hardy, Nairobi.";
var address_2 = "Victoria Courts, Parklands Road, Westlands, Nairobi.";
function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -1.310747, lng: 36.787653},
        zoom: 11
    });
    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);
        }
    });

    geocoder.geocode({'address': address_2}, 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 src="/static/js/contact-script.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDqMKSQz5wNsQaMjhgLYZQe2NNx2KfheTg&callback=initMap"
async defer></script>

如果您单击标记,它将为您提供有关位置的信息。 (您也可以选择添加标记为lon的标记,同时添加点击时显示的信息)。

地图工作正常,正在加载无法正确完成的标记。

我还在本地服务器上工作,这可能是问题所在。

0 个答案:

没有答案