Javascript 在 Google Maps API 中获取标记的坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11030730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:07:34  来源:igfitidea点击:

Getting coordinates of marker in Google Maps API

javascriptgoogle-maps

提问by Wern Ancheta

I'm using Google Maps API. And I'm using the code below to get the coordinates of the marker.

我正在使用谷歌地图 API。我正在使用下面的代码来获取标记的坐标。

var lat = homeMarker.getPosition().$a;
var lng = homeMarker.getPosition().ab;

Everything is working fine with the app that I built using Google Maps. But I tested it today and I got problems getting the correct coordinates. Only to discover that the code above is getting undefined. After I console.log(homeMarker.getPosition()) I discovered that this is now the variables that they used for the coordinates.

我使用 Google 地图构建的应用程序一切正常。但是我今天对其进行了测试,但在获取正确坐标时遇到了问题。只是发现上面的代码变得未定义。在我 console.log(homeMarker.getPosition()) 之后,我发现这现在是他们用于坐标的变量。

var lat = homeMarker.getPosition().ab;
var lng = homeMarker.getPosition().cb;

I won't ask why Google Maps acts this way but you can also include it in your answer. My question is that how do I properly get the coordinates without changing my code everytime google maps changes the variables that they're using.

我不会问为什么谷歌地图会这样,但你也可以将它包含在你的答案中。我的问题是,每次谷歌地图更改他们正在使用的变量时,我如何正确获取坐标而不更改我的代码。

回答by Engineer

var lat = homeMarker.getPosition().lat();
var lng = homeMarker.getPosition().lng();

See the google.maps.LatLngdocs and google.maps.MarkergetPosition().

请参阅google.maps.LatLng文档和google.maps.MarkergetPosition()

回答by Dev-Systematix

One more alternative options

另一种选择

var map = new google.maps.Map(document.getElementById('map_canvas'), {
    zoom: 1,
    center: new google.maps.LatLng(35.137879, -82.836914),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var myMarker = new google.maps.Marker({
    position: new google.maps.LatLng(47.651968, 9.478485),
    draggable: true
});

google.maps.event.addListener(myMarker, 'dragend', function (evt) {
    document.getElementById('current').innerHTML = '<p>Marker dropped: Current Lat: ' + evt.latLng.lat().toFixed(3) + ' Current Lng: ' + evt.latLng.lng().toFixed(3) + '</p>';
});

google.maps.event.addListener(myMarker, 'dragstart', function (evt) {
    document.getElementById('current').innerHTML = '<p>Currently dragging marker...</p>';
});

map.setCenter(myMarker.position);
myMarker.setMap(map);

and html file

和 html 文件

<body>
    <section>
        <div id='map_canvas'></div>
        <div id="current">Nothing yet...</div>
    </section>
</body>

回答by Alex Khimich

Also, you can display current position by "drag" listener and write it to visible or hidden field. You may also need to store zoom. Here's copy&paste from working tool:

此外,您可以通过“拖动”侦听器显示当前位置并将其写入可见或隐藏字段。您可能还需要存储缩放。这是来自工作工具的复制和粘贴:

            function map_init() {
            var lt=48.451778;
            var lg=31.646305;

            var myLatlng = new google.maps.LatLng(lt,lg);
            var mapOptions = {
                center: new google.maps.LatLng(lt,lg),
                zoom: 6,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var map = new google.maps.Map(document.getElementById('map'),mapOptions);   
            var marker = new google.maps.Marker({
                position:myLatlng,
                map:map,
                draggable:true
            });

            google.maps.event.addListener(
                marker,
                'drag',
                function() {
                    document.getElementById('lat1').innerHTML = marker.position.lat().toFixed(6);
                    document.getElementById('lng1').innerHTML = marker.position.lng().toFixed(6);
                    document.getElementById('zoom').innerHTML = mapObject.getZoom();

                    // Dynamically show it somewhere if needed
                    $(".x").text(marker.position.lat().toFixed(6));
                    $(".y").text(marker.position.lng().toFixed(6));
                    $(".z").text(map.getZoom());

                }
            );                  
            }