Google Distance Matrix ZERO_RESULTS返回 [英] Google Distance Matrix ZERO_RESULTS returned

查看:103
本文介绍了Google Distance Matrix ZERO_RESULTS返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言



我一直在使用Google Maps API一段时间,以前能够成功使用距离矩阵服务,但没有回答已被记录。

解决方案

问题是我的坐标被错误地从我建立的服务中获取数据。例如,当它们应该是 41.0259285,-81.5899887 时,坐标是 41.0259285,81.5899887 。经度前面的减号缺失,因此坐标无效,距离矩阵服务不知道该如何处理。


Preface

I've been working with the Google Maps API for quite some time, previously being able to successfully use the Distance Matrix service here. Then I decided to do some refactoring and issues arose...

Current Issue

When trying to get the results of the Distance Matrix service, the rows object contains an empty (status: ZERO_RESULTS) array of objects.

The empty object during runtime below:

Normally it would look like this below:

The code

An array of coords gets passed into the distanceMatrixCoords() function. They look like this:

And the other variables, such as var origins = pos; is populated correctly with a coordinates string.

The entire file containing this code is available here

function distanceMatrixCoords(coords) {
    var origins = pos;
    var destinations = coords;
    var service = new google.maps.DistanceMatrixService();

    service.getDistanceMatrix({
            origins: [origins],
            destinations: destinations,
            travelMode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.IMPERIAL,
            avoidHighways: false,
            avoidTolls: false
        },
        callback
    );

    function callback(response, status) {
        if (status == google.maps.DistanceMatrixStatus.OK) {
            var origins = response.originAddresses;
            var destinations = response.destinationAddresses;
            var distanceElement = [];
            var theRow = null;

            for (var i = 0; i < origins.length; i++) {

                var results = response.rows[i].elements;

                for (var j = 0; j < results.length; j++) {
                    var element = results[j];
                    var distance = element.distance.text;
                    var duration = element.duration.text;
                    var from = origins[i];
                    var to = destinations[j];

                    distanceElement[j] = document.createElement('p');
                    distanceElement[j].innerHTML = results[j].distance.text;
                    distanceElement[j].className = 'distance-cell';
                }
            }
        }
    }

}

Ultimately, based on the code above, the line var results = response.rows[i].elements; is where the code breaks. response.rows is the object that has the multiple ZERO_RESULTS in the array. Compared to my previous code (linked above), the distanceMatrixCoords() function receives the exact same type of data as before.

The response object that is passed into the callback() function contains these values:

destinationAddresses and originAddresses are populated correctly with coordinates that look like this comma separated value, "41.0259285,81.5899887". Once again the rows array is the issue.

I have looked at many other sources for an answer, but the Distance Matrix service of the Google Maps API does not seem to have many resources available out there. I saw this question, but no answers have been recorded yet.

解决方案

The issue was my coordinates were being returned incorrectly from the service I built to get data. The coordinates are, for example, 41.0259285,81.5899887 when they should be 41.0259285,-81.5899887. The minus sign in front of the longitude is missing, therefore the coordinate is not valid and the distance matrix service doesn't know what to do with them.

这篇关于Google Distance Matrix ZERO_RESULTS返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆