简体   繁体   English

在纬度/经度值超过5位小数的Google地图上移动标记

[英]Moving markers on Google Map with latitude/longitude values longer than 5 decimal places

I am placing some markers on to a map from a JSON array, some have the same longitude and latitude so I am trying to offset these by 0.00005. 我将一些标记从JSON数组放置到地图上,一些标记具有相同的经度和纬度,因此我尝试将其偏移0.00005。 This works for most of the markers however it seems that there are some which do not want to budge, I can only put it down to the coordinates being longer than 5 decimal places and when I do my addition it doesn't calculate properly. 这适用于大多数标记,但是似乎有些标记不想让步,我只能将其放到小于5个小数位的坐标上,而当我加法时,它不能正确计算。

I've attempted to parseFloat(latitude).toFixed(5) for every latitude, same for longitude however it seems when added to the map it still has extra decimal places. 我尝试为每个纬度解析parfFloat(latitude).toFixed(5),对于经度也一样,但是当添加到地图时,它似乎仍具有额外的小数位。

JSON Array: JSON数组:

0: {machineName: "PC38", clientName: "The Client", latitude: "-32.93340", longitude: "151.76830"}
1: {machineName: "PC25", clientName: "The Client", latitude: "-32.92957", longitude: "151.76482"}
2: {machineName: "PC33", clientName: "The Client", latitude: "-32.92959", longitude: "151.76480"}
3: {machineName: "PC15", clientName: "The Client", latitude: "-32.92957", longitude: "151.76481"}
4: {machineName: "PC11", clientName: "The Client", latitude: "-32.75687", longitude: "151.62823"}
5: {machineName: "PC18", clientName: "The Client", latitude: "-32.92958", longitude: "151.76481"}
6: {machineName: "PC17", clientName: "The Client", latitude: "-32.92954", longitude: "151.76477"}
7: {machineName: "PC30", clientName: "The Client", latitude: "-32.92956", longitude: "151.76483"}

Function: 功能:

var map;
function initMap(locations) {
    map = new google.maps.Map(document.getElementById('tracker-map'), {
        center: {lat: -34.397, lng: 150.644},
        zoom: 8
    });
    var infowindow =  new google.maps.InfoWindow({ content: '' });
    var markers = [];
    var longArr = [];
    var latArr = [];
    for (var i=0; i < locations.length; i++) {
        var latitude = locations[i].latitude, longitude = locations[i].longitude;
        if (!latArr.includes(latitude)) { latArr.push(latitude); }
        else {
            latitude = latitude+0.0005;
            latArr.push(latitude);
        }
        if (!longArr.includes(longitude)) { longArr.push(longitude); }
        else {
            longitude = longitude+0.0005;
            longArr.push(longitude);
        }
        var latLng = new google.maps.LatLng(latitude,longitude);
        var marker = new google.maps.Marker({
            position: latLng,
            map: map,
            title: locations[i].machineName + " :: " + locations[i].clientName,
        });
        bindInfoWindow(marker, map, infowindow, "<p>" + locations[i].machineName + " :: " + locations[i].clientName + "</p><br>" + latLng);
        markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers,{imagePath: 'https://.../'});
}

I need for the markers to not be right on top of each other. 我需要标记不能彼此重叠。

If we add the following on click listener to our marker cluster: 如果我们在点击监听器上将以下内容添加到标记群集中:

google.maps.event.addListener(markerCluster, "click", function (c) {
    var m = c.getMarkers();
    var p = [];
    for (var i = 0; i < m.length; i++ ){
        p.push(m[i].getPosition());
    }
    console.log("Locations of managed markers: " + p.join(", "));
});

In our console the result is: Locations of managed markers: (-32.92957, 151.76482), (-32.92956, 151.76482999999996) 在我们的控制台中,结果为: Locations of managed markers: (-32.92957, 151.76482), (-32.92956, 151.76482999999996)

I resorted to using https://github.com/jawj/OverlappingMarkerSpiderfier 我诉诸于使用https://github.com/jawj/OverlappingMarkerSpiderfier

function initMap(locations) {
    map = new google.maps.Map(document.getElementById('tracker-map'), {
        center: {lat: -34.397, lng: 150.644},
        zoom: 8
    });
    var markerSpiderfier = new OverlappingMarkerSpiderfier(map, {
        markersWontMove: true,
        markersWontHide: true,
        basicFormatEvents: true,
        keepSpiderfied: true,
    });
    var markers = [];
    for (var i=0; i < locations.length; i++) {
        var latitude = locations[i].latitude, longitude = locations[i].longitude;
        var latLng = new google.maps.LatLng(latitude,longitude);
        var titleHTML = 'title info here...';
        var marker = new google.maps.Marker({
            position: latLng,
            map: map,
            title: titleHTML,
        });
        markers.push(marker);
        markerSpiderfier.addMarker(marker);
    }
    var iw =  new google.maps.InfoWindow({ content: '' });
    markerSpiderfier.addListener('click', function(marker, e) {
        iw.setContent(marker.title);
        iw.open(map, marker);
    });
    markerSpiderfier.addListener('spiderfy', function(markers) {
        iw.close();
    });
    var markerCluster = new MarkerClusterer(map, markers,{imagePath: '...'});
    markerCluster.setMaxZoom(15);
}

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

相关问题 Google地方信息-如何获取经度和纬度值 - Google Places - How to get latitude and longitude values 即使纬度和经度具有相同的值,我如何在谷歌 map 上显示所有标记? - How do i display all markers on google map even if latitude and longitude having same values? 如何解决相同纬度和经度的Google Map标记? - How to solve Google Map markers with same latitude and longitude? Laravel 从数据库中获取经度和纬度并在谷歌地图上显示标记 - Laravel fetch longitude and latitude from database and display markers on Google Map 限制Google API Map中经度和纬度返回的小数点数 - Limit the number Of Decimal Points Returned on Longitude and Latitude in Google API Map 将纬度和经度转换为十进制值 - Converting latitude and longitude to decimal values 如何在JPG地图上叠加经度和纬度标记? - How to overlay Longitude & latitude markers on JPG map? 如何刷新谷歌标记的经度和纬度? - How to refresh latitude and longitude of google markers? 根据解析的XML纬度和经度值居中Google地图 - Centering a Google Map based on parsed XML Latitude and Longitude values 如何在谷歌地图上找到最小和最大的纬度和经度 - how to find minimum & maximum values latitude and longitude on google map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM