位置为 "off road" 时计算距离
Calculating distance when location is "off road"
有没有办法让 Google 距离矩阵 API return 在当前选择的位置不是最近的可驾驶位置时?
一点背景知识:在报价过程中,我需要计算到预期装运地点的预估运费。
最终目标是使用生成报价的人输入的地址进行实时距离计算。然而,目前这是通过使用 postal 代码的数据库来处理的,其中包含到我们每个工厂的里程数。例如:
| START | END | DIST |
-------------------------
| 47601 | 47715 | 29725 |
| 47601 | 47716 | 31741 |
这个数据是旧的,所以我一直负责更新它,直到我们推出交互式版本。
我正在使用 Google 地图距离矩阵(成功)来更新许多距离。但是,我遇到了一些我似乎无法通过代码更改解决的问题。当提供给Google的postal代码在水中是"centered"(相关post:) or on an island (such as 02543 : https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=47601,%20USA&destinations=02543,%20USA),状态returned是"ZERO_RESULTS".
虽然可以开车去马萨诸塞州伍兹霍尔 02543。例如 266 Woods Hole Road, Woods Hole, MA 02543。Woods Hold 海洋研究所的地址。
由于美国 postal 代码不是特定位置,我不是在寻找确切的距离...因此 "nearby" 可以用于引用目的。有什么建议吗?
对失败的邮政编码进行地理编码,然后对结果坐标进行反向地理编码,看起来会给出一组可以使用的结果。
我为你的例子做的结果是:
Boonville, IN 47601, USA to 16-22 Albatross St, Woods Hole, MA 02543, USA: 1,759 km in 16 hours 39 mins
Boonville, IN 47601, USA to 23 Albatross St, Woods Hole, MA 02543, USA: 1,759 km in 16 hours 39 mins
Boonville, IN 47601, USA to 520 Main St, Falmouth, MA 02540, USA: 1,754 km in 16 hours 33 mins
代码片段(使用 Google 地图 Javascript API DistanceMatrix 服务):
var geocoder = new google.maps.Geocoder();
var service = new google.maps.DistanceMatrixService();
var map;
// https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=47601,%20USA&destinations=02543,%20USA
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
service.getDistanceMatrix({
origins: ["47601, USA"],
destinations: ["02543, USA"],
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
console.log("status=" + status + ", response=" + response);
if (status != 'OK') {
alert('Error was: ' + status);
} else {
var originList = response.originAddresses;
var destinationList = response.destinationAddresses;
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = '';
for (var i = 0; i < originList.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
if (results[j].status != 'OK') {
alert("result[" + j + "] Error was: " + results[j].status);
geocoder.geocode({
'address': destinationList[i]
}, reverseGeocodeResult);
break;
}
outputDiv.innerHTML += originList[i] + ' to ' + destinationList[j] +
': ' + results[j].distance.text + ' in ' +
results[j].duration.text + '<br>';
}
}
}
});
}
function reverseGeocodeResult(response, status) {
if (status == 'OK') {
geocoder.geocode({
latLng: response[0].geometry.location
}, findDistancesToResponses);
}
}
function findDistancesToResponses(response, status) {
var destArr = [];
for (var i = 0; i < response.length; i++) {
destArr.push(response[i].geometry.location);
}
service.getDistanceMatrix({
origins: ["47601, USA"],
destinations: destArr,
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
var originList = response.originAddresses;
var destinationList = response.destinationAddresses;
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = '';
for (var i = 0; i < originList.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
if (results[j].status != 'OK') {
alert("result[" + j + "] " + destinationList[j] + " Error was: " + results[j].status);
}
outputDiv.innerHTML += originList[i] + ' to ' + destinationList[j] +
': ' + results[j].distance.text + ' in ' +
results[j].duration.text + '<br>';
}
}
})
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<div id="map_canvas"></div>
<div id="output"></div>
有没有办法让 Google 距离矩阵 API return 在当前选择的位置不是最近的可驾驶位置时?
一点背景知识:在报价过程中,我需要计算到预期装运地点的预估运费。
最终目标是使用生成报价的人输入的地址进行实时距离计算。然而,目前这是通过使用 postal 代码的数据库来处理的,其中包含到我们每个工厂的里程数。例如:
| START | END | DIST | ------------------------- | 47601 | 47715 | 29725 | | 47601 | 47716 | 31741 |
这个数据是旧的,所以我一直负责更新它,直到我们推出交互式版本。
我正在使用 Google 地图距离矩阵(成功)来更新许多距离。但是,我遇到了一些我似乎无法通过代码更改解决的问题。当提供给Google的postal代码在水中是"centered"(相关post:
虽然可以开车去马萨诸塞州伍兹霍尔 02543。例如 266 Woods Hole Road, Woods Hole, MA 02543。Woods Hold 海洋研究所的地址。
由于美国 postal 代码不是特定位置,我不是在寻找确切的距离...因此 "nearby" 可以用于引用目的。有什么建议吗?
对失败的邮政编码进行地理编码,然后对结果坐标进行反向地理编码,看起来会给出一组可以使用的结果。
我为你的例子做的结果是:
Boonville, IN 47601, USA to 16-22 Albatross St, Woods Hole, MA 02543, USA: 1,759 km in 16 hours 39 mins
Boonville, IN 47601, USA to 23 Albatross St, Woods Hole, MA 02543, USA: 1,759 km in 16 hours 39 mins
Boonville, IN 47601, USA to 520 Main St, Falmouth, MA 02540, USA: 1,754 km in 16 hours 33 mins
代码片段(使用 Google 地图 Javascript API DistanceMatrix 服务):
var geocoder = new google.maps.Geocoder();
var service = new google.maps.DistanceMatrixService();
var map;
// https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=47601,%20USA&destinations=02543,%20USA
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
service.getDistanceMatrix({
origins: ["47601, USA"],
destinations: ["02543, USA"],
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
console.log("status=" + status + ", response=" + response);
if (status != 'OK') {
alert('Error was: ' + status);
} else {
var originList = response.originAddresses;
var destinationList = response.destinationAddresses;
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = '';
for (var i = 0; i < originList.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
if (results[j].status != 'OK') {
alert("result[" + j + "] Error was: " + results[j].status);
geocoder.geocode({
'address': destinationList[i]
}, reverseGeocodeResult);
break;
}
outputDiv.innerHTML += originList[i] + ' to ' + destinationList[j] +
': ' + results[j].distance.text + ' in ' +
results[j].duration.text + '<br>';
}
}
}
});
}
function reverseGeocodeResult(response, status) {
if (status == 'OK') {
geocoder.geocode({
latLng: response[0].geometry.location
}, findDistancesToResponses);
}
}
function findDistancesToResponses(response, status) {
var destArr = [];
for (var i = 0; i < response.length; i++) {
destArr.push(response[i].geometry.location);
}
service.getDistanceMatrix({
origins: ["47601, USA"],
destinations: destArr,
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
var originList = response.originAddresses;
var destinationList = response.destinationAddresses;
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = '';
for (var i = 0; i < originList.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
if (results[j].status != 'OK') {
alert("result[" + j + "] " + destinationList[j] + " Error was: " + results[j].status);
}
outputDiv.innerHTML += originList[i] + ' to ' + destinationList[j] +
': ' + results[j].distance.text + ' in ' +
results[j].duration.text + '<br>';
}
}
})
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<div id="map_canvas"></div>
<div id="output"></div>