javascript - 如何找到谷歌地图的准确LatLng值

标签 javascript google-maps google-maps-api-3 google-maps-markers

在我的应用程序中,用户可以使用click事件找到地点名称,在获取地点名称后,我使用inputField向用户显示地点名称为此我编写了以下代码。

//辅助函数

function makingGeocodeRequest(obj,callback){
   var geocodeInstance=new google.maps.Geocoder();
    geocodeInstance.geocode(obj,callback);
}

google.maps.event.addListener(mapInstane,"click",function(event){
 makingGeocodeRequest(_.object(["location"],[event.latLng]),
                        function(res,status){
                            document.getElementById("field").value=res[0]["formatted_address"];
                        }
 )
})

用户单击保存按钮后。我根据地点名称查找latlng值。使用以下代码

makingGeocodeRequest(
    _.object(["address"],[document.getElementById("field").value]),
    function(res,status){
        if (status===google.maps.GeocoderStatus.OK) {
          var latLngObj=res[0]["geometry"]["location"];
          console.log(latLngObj;
        }
    }
)

这里的问题是,两个latlng值不同(点击事件时间latlng值和保存按钮操作latlng值)。

实际上两者都是从 Google 找到的,但它返回不同的 latlng 值。

当单击事件事件时,我正在使用 this 更改光标样式图标。单击保存按钮后恢复默认光标。

我该如何解决这个问题。有人可以帮助我吗?

谢谢。

最佳答案

要解决您的问题,您可以创建点数组,向该数组添加标记,然后在 map 上渲染该数组的结果。

以下是您可以执行此操作的方法:

使用 JavaScript,

<script type="text/javascript">
        var map;
        var markersList= [];

        function initGMap()
        {
            var latlng = new google.maps.LatLng(39, 20);
            var myOptions = {
                zoom: 10,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map"), myOptions);

            // add a click event handler to the map object and get the lat Lng and then place it on the map
            google.maps.event.addListener(map, "click", function(event)
            {
                // place a marker
                placeMarker(event.latLng);

                // display the lat/lng in your form's lat/lng fields
                document.getElementById("latVal").value = event.latLng.lat();
                document.getElementById("lngVal").value = event.latLng.lng();
            });
        }
        // here is the function to place Marker on the map
        function placeMarker(location) {
            // first remove all markers if there are any
            deleteOverlays();

            var marker = new google.maps.Marker({
                position: location, 
                map: map
            });

            // add marker in markers array
            markersList.push(marker);

            //map.setCenter(location);
        }

        // Here you can use this function to delete all markers in the array
        function deleteOverlays() {
            if (markersList) {
                for (i in markersList) {
                    markersList[i].setMap(null);
                }
            markersList.length = 0;
            }
        }
    </script>

使用 Html 代码,

<body onload="initGMap()">
    <div id="map"></div>
    <input type="text" id="latVal">
    <input type="text" id="lngVal">
</body>

关于javascript - 如何找到谷歌地图的准确LatLng值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24035842/

相关文章:

javascript - 获取绝对高度和宽度

javascript - 为什么这个持有 SVG 的 `blob:` URL 会污染 Chrome 中的 Canvas ?

javascript - 以下如何成为纯函数?

javascript - 我如何计算点之间的距离 Google Maps Api V3

javascript - 如何使用 jquery-ui-map 重新居中 map

css - 谷歌地图中的 ghost DIV 创建可点击的阴影

javascript - Google Maps API - 在从 ajax 页面加载新数据之前清除标记、多段线

javascript - VM1052 :19 Error: (SystemJS) TypeScript transpilation failed

android - 如何在 Android Google map 上显示 map 图例?

javascript - Google Maps API,在不使用坐标的情况下在国家/地区放置标记