谷歌地图地理编码未定义

发布于 2024-09-29 18:58:50 字数 1445 浏览 2 评论 0原文

我有这个在加载时启动的代码

// 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

I have this code which is initated on load

// 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.');
        }
    });
}

i get the following error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-10-06 18:58:50

嗯...呃...它说什么。您说的是 geocoder.geocode(...),但您实际上并未定义任何名为 geocoder 的变量。你是否忘记了:

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

Well... er... what it says. You're saying geocoder.geocode(...), but you've not actually defined any variable called geocoder. Did you forget:

var geocoder= new google.maps.Geocoder();
尘世孤行 2024-10-06 18:58:50

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

geocoder = new google.maps.Geocoder();

Do you have this line somewhere? I don't see it in the code provided.

geocoder = new google.maps.Geocoder();
怕倦 2024-10-06 18:58:50

还要确保您

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

已经运行了 javascript 代码。

这就是让我陷入困境的原因。

Also make sure you have

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

prior to running the javascript code.

That's what messed me up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文