Google maps 谷歌地图地理编码未定义

Google maps 谷歌地图地理编码未定义,google-maps,geocoding,Google Maps,Geocoding,我有这个代码,它是在加载时初始化的 // Display the map function map_it() { var myLatlng = new google.maps.LatLng(13.005621, 77.577531); var myOptions = { zoom: zoomLevel, center: myLatlng, disableDefaultUI: true, mapTypeId: goo

我有这个代码,它是在加载时初始化的

// Display the map

function map_it() {
    var myLatlng = new google.maps.LatLng(13.005621, 77.577531);
    var myOptions = {
        zoom: zoomLevel,
        center: myLatlng,
        disableDefaultUI: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        navigationControl: true,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        }
    }
    map = new google.maps.Map(document.getElementById("map-it"), myOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function (event) {
        document.getElementById('mapLat').value = marker.getPosition().lat();
        document.getElementById('mapLng').value = marker.getPosition().lng();
        geocodePosition(marker.getPosition());
    });
}

function geocodePosition(pos) {
    geocoder.geocode({
        latLng: pos
    }, function (responses) {
        if (responses && responses.length > 0) {
            updateMarkerAddress(responses[0].formatted_address);
            alert(responses[0].formatted_address);
        } else {
            updateMarkerAddress('Cannot determine address at this location.');
        }
    });
}
我得到以下错误

geocoder is not defined
http://localhost/bakasura1/js/modal.js
Line 74

嗯。。。呃。。。它说什么。你说的是
geocoder.geocode(…)
,但实际上你没有定义任何名为
geocoder
的变量。你忘了:

var geocoder= new google.maps.Geocoder();

你有这条线路吗?我在提供的代码中没有看到它

geocoder = new google.maps.Geocoder();

还要确保你有

    <script src="https://maps.google.com/maps?file=api&amp;v=3&amp;sensor=false"
     type="text/javascript"></script>

在运行javascript代码之前


这就是把我搞砸的原因。

这是这个特定案例的答案,但是否应该因为这个解决方案不适用而打开一个新的重复问题?我在代码的开头确实有这一行,而我调用它的匿名函数(在上面一行之前不执行,但是onclick)发现它没有定义。我用console.log在这个函数链中的每个函数中测试了geocoder,但没有一个能找到它。这就像在文档级别使用“var geocoder=…”是不够的。嗯,是的,对我来说,听起来是另一个问题!建议发布一个最小的可复制测试用例。