Google Distance Matrix返回对象的空数组

问题描述 投票:-1回答:1

[我正在尝试结合Google的距离矩阵并放置一个库,在该库中我首先在原点周围搜索派出所,在派出所中,我会返回每个派出所的纬度和经度,在其中我将每个值附加到数组中将其传递给distanceMatrix目标。到目前为止,还没有任何错误,但是当我尝试记录回调函数的结果时,它返回一个空对象数组。

当前问题

[尝试记录distanceMatrixService的回调函数的结果时,它返回一个空对象数组

emptyArray

通常应该看起来像这样valid response

代码

这些代码很冗长,但是每个部分都是必不可少的,并且彼此互连。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=API_KEY_HERE&libraries=places"></script>


var map;
var infoWindow;
var markers = [];
var serviceMatrix

//Array Containing all lng/lat of police station
var allPoliceLocation = [];
var origin1 = new google.maps.LatLng(3.120231, 101.595247);

//Default Dummy Data
// var destinationA = new google.maps.LatLng(3.117386, 101.635133);
// var destinationB = new google.maps.LatLng(3.117386, 101.635333);

function initMap() {

    map = new google.maps.Map(document.getElementById("map"), {
        zoom: 15,
        center: origin1
    });

    var request = {
        location: origin1,
        radius: 3000,
        types: ["police"]
    };

    infoWindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, callback);
}

function createMarker(place) {
    var placeLat = place.geometry.location.lat();
    var placeLng = place.geometry.location.lng();

    createLatLng(placeLat, placeLng);

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

    var homeMarker = new google.maps.Marker({
        position: {
            lat: 3.120231,
            lng: 101.595247
        },
        map: map,
        icon: {
            url:
                "http://maps.google.com/mapfiles/ms/icons/green-dot.png"
        }
    });

    google.maps.event.addListener(homeMarker, "click", function () {
        infoWindow.setContent(place.name);
    });

    google.maps.event.addListener(marker, "click", function () {
        infoWindow.setContent(place.name);
        infoWindow.open(map, this);
    });

    return marker;
}

function createLatLng(lat, lng) {
    var newLocation = new google.maps.LatLng(lat, lng);
    allPoliceLocation.push(newLocation);
}


(function () {

    //This does returns all the object of LatLng created from createLatLng function
    console.log(allPoliceLocation);
    serviceMatrix = new google.maps.DistanceMatrixService();
    serviceMatrix.getDistanceMatrix(
        {
            origins: [origin1],
            destinations: allPoliceLocation,
            travelMode: "DRIVING",
            avoidHighways: false,
            avoidTolls: true
        },
        function (status,results) {

            //Returns empty array here 
            console.log(results);

            //Invalid Request
            console.log(status);
        }
    );
})();


function callback(results, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {

            //Create a new marker for every new police station
            markers.push(createMarker(results[i]));
        }
    }

}

google.maps.event.addDomListener(window, "load", initMap);

编辑

我已经在回调函数中添加了第二个参数以检查状态,并且它返回了INVALID_REQUEST。我也尝试过手动将2个虚拟数据添加到数组中,并在记录日志时将其作为目标arr传递,与allPoliceLocation数组相比,它似乎有所不同。

allPoliceArray empty

javascript google-maps google-distancematrix-api
1个回答
0
投票

已解决

设法找到一种解决方案,“ hacky”,显然我怀疑distanceMatrix函数在数据被推入数组之前就运行了,这就是为什么它显示为空数组。这是我解决的方法。

var allPoliceLocation = []; function createLatLng(results) { results.forEach(result => { const lat = result.geometry.location.lat(); const lng = result.geometry.location.lng(); newLocation = new google.maps.LatLng(lat, lng); allPoliceLocation.push(newLocation); }); getMatrixDistance(allPoliceLocation); } function getMatrixDistance(destination) { console.log("here"); serviceMatrix = new google.maps.DistanceMatrixService(); serviceMatrix.getDistanceMatrix( { origins: [origin1], destinations: destination, travelMode: "DRIVING", avoidHighways: false, avoidTolls: true }, function (results, status) { console.log(results); } ) } function callback(results, status) { if (status === google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i < results.length; i++) { //Create a new marker for every new police station markers.push(createMarker(results[i])); } createLatLng(results); } }

© www.soinside.com 2019 - 2024. All rights reserved.