未能在谷歌地图上设置标记

Failed to setup marker on google maps

本文关键字:设置 谷歌地图      更新时间:2023-09-26

嗨,我正试图在谷歌地图上实现标记,但它不起作用,我只是粘贴了我的脚本请告诉我哪里做错了。谢谢

    <script>
    var map;
    function initialize() {
        var Longitude = document.getElementById('Longitude').value;
        var Lattitude = document.getElementById('Lattitude').value;
        console.log(Longitude + '' + Lattitude);
        var map_canvas = document.getElementById('map_canvas');
        var isDraggable = $(document).width() < 1000 == false;
        var map_options = {
            center: new google.maps.LatLng(Lattitude, Longitude),
            zoom: 15,
            draggable: isDraggable,
            scrollwheel: false,
            //    navigationControl: false,
            //mapTypeControl: false,
            //scaleControl: false,

            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(map_canvas, map_options)
        var myLatlng = new google.maps.LatLng(Longitude, Lattitude);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'Hello World!',
            animation: google.maps.Animation.DROP,
        });
        marker.setMap(map)
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

好吧,您交换了经度/纬度。LatLng()首先要求lattitude(在你的地图上,地球的某个完全不同的地方可能有一个标记)。只需将代码的这一部分更改为:

...
var myLatlng = new google.maps.LatLng(Lattitude, Longitude);
var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title: 'Hello World!',
    animation: google.maps.Animation.DROP
});
//marker.setMap(map)
...

你不需要marker.setMap(map)。这已经用map完成了:map,

我让你的代码正常工作(尽管我伪造了lat和long而不是使用文本框)。

我可以提供几个建议:

* Make sure the google maps script and jQuery scripts are loaded before your code
* Ensure your textboxes are populated before the domready event fires
* When loading the page, press F12 and refresh.  Check the console that pops up for errors