简体   繁体   English

谷歌地图动态标记不正确 position - 纬度和经度是准确的

[英]Google Maps dynamic markers not in correct position - Lat and Long is accurate

Using the Google Maps API I'm dynamically adding map markers to track 2 of our vehicles.The website is written in asp.net c# mvc with bootstrap 4.3.1, using an ajax request to grab the latest marker location from the vehicle tracker API that we use. Using the Google Maps API I'm dynamically adding map markers to track 2 of our vehicles.The website is written in asp.net c# mvc with bootstrap 4.3.1, using an ajax request to grab the latest marker location from the vehicle tracker API that we利用。 Map markers are updated every 5 minutes to show the current location. Map 标记每 5 分钟更新一次以显示当前位置。

When running the application in localhost and on one of our internal servers the markers display exactly where they should.在 localhost 和我们的一台内部服务器上运行应用程序时,标记会准确显示在它们应该显示的位置。 Now that I've uploaded the website to an external web server I've noticed that the map markers are not accurate and are always in an incorrect position.现在我已将网站上传到外部 web 服务器,我注意到 map 标记不准确,并且始终位于不正确的 position 中。

I have a screen shot which shows the marker being inspected in dev tools and you can see the position where the marker is supposed to be.我有一个屏幕截图,显示了在开发工具中检查的标记,您可以看到标记应该在的 position。

See marker and ghost position on screenshot请参阅屏幕截图上的标记和幽灵 position

I've checked the data for the markers and all seems to be fine on that front, after inspecting the code in dev tools it seems like the markers are being pushed down and right by the CSS but I can't seem to find and deselect any classes which affect the marker position.我检查了标记的数据,在这方面似乎一切都很好,在检查开发工具中的代码之后,似乎标记被 CSS 向下和向右推,但我似乎找不到并取消选择影响标记 position 的任何类。

I've managed to replicate the issue in a JSFiddle here, the location data has been hard coded for this demo;我已经设法在 JSFiddle 中复制了这个问题,这个演示的位置数据已经被硬编码;

https://jsfiddle.net/znqo5xaL/ https://jsfiddle.net/znqo5xaL/

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="row">
        <div id="map"></div>
        <script>
            var locs;
            var markersArray = [];
            var map;

            function initMap() {
                locs = [{"lat":"53,5658418","lng":"-0,8491593","distanceList":null},{"lat":"52,8815088","lng":"-2,1409461","distanceList":null}];

                map = new google.maps.Map(
                    document.getElementById('map'),
                    { center: new google.maps.LatLng(54.113616, -1.607444), zoom: 6 });


                for (var i = 0; i < locs.length; i++) {
                    var marker = new google.maps.Marker({
                        position: {lat: parseFloat(locs[i].lat), lng: parseFloat(locs[i].lng)},
                        animation: google.maps.Animation.DROP,
                        map: map,                                      

                    });
                    markersArray.push(marker);
                };

                window.setInterval(function() {loadMarkers(image, map, markersArray, locs)}, 300000);

            }

            function setMarkers(markersArray, map, locs, image) {

                var locArray;

                $.ajax({

                    url:'../Home/UpdateLoc',
                    dataType: "json",
                    type: "GET",
                    contentType: 'application/json; charset=utf-8',
                    cache: true,
                    success: function (data) {

                        locArray = data.locArray;

                        for (var i = 0; i < locArray.length; i++) {
                            var marker = new google.maps.Marker({

                                position: {lat: parseFloat(locArray[i].lat), lng: parseFloat(locArray[i].lng)},
                                animation: google.maps.Animation.DROP,
                                map: map

                            });
                            markersArray.push(marker);
                        };
                    },
                    error: function (xhr) {
                        alert('error');
                    }
                });


            }

            function loadMarkers (image, map, markersArray, locs) {

                var tst = markersArray;

                for (var i=0; i< tst.length; i++) {
                    tst[i].setMap(null);
                }
                setMarkers(markersArray, map, locs, image);
            }

        </script>


        <script async defer src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap"></script>

    </div>

You can see that when you search for the location: 53.5658418, 0.8491593 on Google maps it is in a different position as to what is shown on my map您可以看到,当您在 Google 地图上搜索位置:53.5658418, 0.8491593 时,它与我的 map 上显示的内容不同的是 position

The marker location shown on Google maps using the co-ordinates above is where my marker should be displayed.使用上面的坐标在谷歌地图上显示的标记位置是我的标记应该显示的位置。

What could be causing this issue?什么可能导致此问题?

What does parseFloat(53,5658418) return? parseFloat(53,5658418)返回什么? See below.见下文。

 console.log(parseFloat(53,5658418));

And what does parseFloat(53.5658418) return? parseFloat(53.5658418)返回什么? See below.见下文。

 console.log(parseFloat(53.5658418));

53.5658418 is a number. 53.5658418是一个数字。

53,5658418 is not. 53,5658418不是。

It looks like this inaccuracy is because you are using parseFloat to parse comma-separated strings.看起来这种不准确是因为您使用parseFloat来解析逗号分隔的字符串。 Please refer to this thread Javascript parse float is ignoring the decimals after my comma请参考这个线程Javascript parse float is ignores the decimals after my comma

Try logging this out:尝试将其注销:

 console.log("Commas: " + parseFloat(locs[i].lat) + ", " + parseFloat(locs[i].lng));
 console.log("Dots: " + parseFloat(locs[i].lat.replace(',', '.')) + ", " + parseFloat(locs[i].lng.replace(',', '.')));

You'll see the different coordinates in the output:您将在 output 中看到不同的坐标:

Commas: 53, 0
Dots: 53.5658418, -0.8491593
Commas: 52, -2
Dots: 52.8815088, -2.1409461

A quick fix is to replace the commas with decimal points using replace as shown in the above code, so the marker's position is set as follows:一个快速的解决方法是使用replace替换逗号,如上面的代码所示,因此标记的position设置如下:

var marker = new google.maps.Marker({
     position: {
        lat: parseFloat(locs[i].lat.replace(',', '.')),
        lng: parseFloat(locs[i].lng.replace(',', '.')),
     },
     ...
});

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM