谷歌地图(google.maps.LatLng)更改值lat lng

时间:2015-07-20 08:24:04

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

大家好,我有一个问题,当我每次使用google.maps.LatLng方法时自动将经度值更改为不同的值,这在Google地图中使用时太长,所以我无法添加标记在我的地图中的位置。任何人都知道不同的方式来做它或问题是什么。我从json sting中获取数据并将其加载到数组中检查下面的代码。

我的纬度和经度

lat,lng

使用google.maps.LatLng纬度和经度后 google.maps.LatLng

var map;
var markers = [];
var child_details = [];

function initialize() {
    var haightAshbury = new google.maps.LatLng(34.8055973, 32.4104341);
    var mapOptions = {
        zoom: 12,
        center: haightAshbury,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

    // This event listener will call addMarker() when the map is clicked.
    google.maps.event.addListener(map, 'click', function(event) {
        addMarker(event.latLng);
    });
}


function load_children(child_list) {

    var marker;
    var obj = JSON.parse(child_list);

    for (var i in obj) {
        child_details.push([obj[i].cid, obj[i].uid, obj[i].Name, obj[i].Surname,
            obj[i].lat, obj[i].lng, obj[i].timestamp,
            obj[i].Sensor
        ]);
    }

    setUpMarkers(map, child_details);
}


function setUpMarkers(map, locations) {

    alert('set up');
    for (var i = 0; i < locations.length; i++) {
        var loc = locations[i];
        alert("lat" + loc[4] + "lng " + loc[5]);
        //The problem is here
        var myLatLng = new google.maps.LatLng(loc[4], loc[5]);
        //After i use this method to get Lat Lng i get wrong lng
        alert(myLatLng);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: loc[3]

        });
        markers.push(marker);
    }

}


// Add a marker to the map and push to the array.
function addMarker(location) {
    var marker = new google.maps.Marker({
        position: location,
        map: map
    });
    markers.push(marker);

}

// Sets the map on all markers in the array.
function setAllMap(map) {
    for (var i = 0; i < markers.length; i++) {
        alert('adding marker' + markers[i]);
        markers[i].setMap(map);
    }
}

// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
    setAllMap(null);
}

// Shows any markers currently in the array.
function showMarkers() {
    setAllMap(map);
}

// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
    clearMarkers();
    markers = [];
}

1 个答案:

答案 0 :(得分:1)

显示的坐标与GoogleMaps精度问题完全相同。

精度只有小数点后6位,在此之后,无需考虑小数位,以便在地图中定位标记。

在此处查看toUrlValue(precision?:number): https://developers.google.com/maps/documentation/javascript/reference#LatLng