距离矩阵 google 地图在 curl php 代码中使用时给出了两个位置之间的错误距离
distance matrix google maps gives wrong distance between two locations while using in curl php code
$userLat1=51.509904342252;
$userLong1= -0.13413459062576;
$userLat2=51.517618;
$userLong2= -0.096778;
$userLat3=51.5017863;
$userLong4= -0.0536478;
$userLat=51.509904342252;
$userLong= -0.13413459062576;
$lat2=51.495042;
$long2= -0.131382;
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$userLat.",".$userLong."&destinations=".$lat2.",".$long2."&mode=driving&sensor=false";
//$response = file_get_contents($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
当我在地图上标记定位点时
$userLat1=51.509904342252;
$userLong1= -0.13413459062576;
$userLat2=51.517618;
$userLong2= -0.096778;
$userLat3=51.5017863;
$userLong4= -0.0536478;
$userLat=51.509904342252;
$userLong= -0.13413459062576;
$lat2=51.495042;
$long2= -0.131382;
,我在地图上可以清楚地看到哪个标记靠近$lat2&$long2。
但是当我使用上面的代码来获取上面提到的两个位置之间的距离时,它给了我不正确的数据..
在某种程度上,它为最近的车站提供了更多的距离值,我在地图上看到的最接近地图上的 lat2 和 long2 位置。
同样,如果我使用上面的代码来获取
之间的距离
$userExtralat2=21.118692,
$userExtralong2=73.117554
&
$lat2=51.495042;
$long2= -0.131382;
google 提供以天和小时为单位的距离..
如何使用 Distancematrix Api 获得以公里为单位的距离?
我从距离矩阵得到的是(修改this example):
origin[0]:51.509904,-0.134135
origin[0]:A4, London W1J, UK to destination[0]:51.509904,-0.134135: 1 m in 1 min
origin[0]:A4, London W1J, UK to destination[1]:51.517618,-0.096778: 4.1 km in 15 mins
origin[0]:A4, London W1J, UK to destination[2]:51.501786,-0.053648: 7.4 km in 23 mins
origin[0]:A4, London W1J, UK to destination[3]:51.495042,-0.131382: 2.8 km in 9 mins
origin[0]:A4, London W1J, UK to destination[4]:21.118692,73.117554: 11,411 km in 7 days 5 hours
距离在结果中返回(至少在 Google Maps Javascript API v3 中,也应该在网络服务中):results[j].distance.text
代码片段:
var geocoder;
var map;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';
var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';
var locations;
function initialize() {
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
});
geocoder = new google.maps.Geocoder();
$userLat1 = 51.509904342252;
$userLong1 = -0.13413459062576;
$userLat2 = 51.517618;
$userLong2 = -0.096778;
$userLat3 = 51.5017863;
$userLong4 = -0.0536478;
// $userLat = 51.509904342252;
// $userLong = -0.13413459062576;
$lat2 = 51.495042;
$long2 = -0.131382;
$userExtralat2 = 21.118692,
$userExtralong2 = 73.117554
locations = [new google.maps.LatLng($userLat1, $userLong1),
new google.maps.LatLng($userLat2, $userLong2),
new google.maps.LatLng($userLat3, $userLong4),
// new google.maps.LatLng($userLat, $userLong),
new google.maps.LatLng($lat2, $long2),
new google.maps.LatLng($userExtralat2, $userExtralong2)
];
}
function calculateDistances() {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: locations,
destinations: locations,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = document.getElementById('outputDiv');
outputDiv.innerHTML = '';
deleteOverlays();
outputDiv.innerHTML += "origin[0]:" + locations[0].toUrlValue(6) + '<br>';
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
// addMarker(destinations[j], true);
outputDiv.innerHTML += "origin[" + i + "]:" + origins[i] + ' to destination[' + j + ']:' + locations[j].toUrlValue(6) /* destinations[j] */ + ': ' + results[j].distance.text + ' in ' + results[j].duration.text + '<br>';
}
}
}
}
function addMarker(location, isDestination) {
var icon;
if (isDestination) {
icon = destinationIcon;
} else {
icon = originIcon;
}
geocoder.geocode({
'address': location
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: icon
});
markersArray.push(marker);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
function deleteOverlays() {
for (var i = 0; i < markersArray.length; i++) {
markersArray[i].setMap(null);
}
markersArray = [];
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
#content-pane {
width: 100%;
padding-left: 2%;
}
#outputDiv {
font-size: 11px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
<div id="content-pane">
<div id="inputs">
<p>
<button type="button" onclick="calculateDistances();">Calculate distances
</button>
</p>
</div>
<div id="outputDiv"></div>
</div>
$userLat1=51.509904342252;
$userLong1= -0.13413459062576;
$userLat2=51.517618;
$userLong2= -0.096778;
$userLat3=51.5017863;
$userLong4= -0.0536478;
$userLat=51.509904342252;
$userLong= -0.13413459062576;
$lat2=51.495042;
$long2= -0.131382;
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$userLat.",".$userLong."&destinations=".$lat2.",".$long2."&mode=driving&sensor=false";
//$response = file_get_contents($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
当我在地图上标记定位点时
$userLat1=51.509904342252;
$userLong1= -0.13413459062576;
$userLat2=51.517618;
$userLong2= -0.096778;
$userLat3=51.5017863;
$userLong4= -0.0536478;
$userLat=51.509904342252;
$userLong= -0.13413459062576;
$lat2=51.495042;
$long2= -0.131382;
,我在地图上可以清楚地看到哪个标记靠近$lat2&$long2。
但是当我使用上面的代码来获取上面提到的两个位置之间的距离时,它给了我不正确的数据.. 在某种程度上,它为最近的车站提供了更多的距离值,我在地图上看到的最接近地图上的 lat2 和 long2 位置。
同样,如果我使用上面的代码来获取
之间的距离$userExtralat2=21.118692,
$userExtralong2=73.117554
&
$lat2=51.495042;
$long2= -0.131382;
google 提供以天和小时为单位的距离..
如何使用 Distancematrix Api 获得以公里为单位的距离?
我从距离矩阵得到的是(修改this example):
origin[0]:51.509904,-0.134135
origin[0]:A4, London W1J, UK to destination[0]:51.509904,-0.134135: 1 m in 1 min
origin[0]:A4, London W1J, UK to destination[1]:51.517618,-0.096778: 4.1 km in 15 mins
origin[0]:A4, London W1J, UK to destination[2]:51.501786,-0.053648: 7.4 km in 23 mins
origin[0]:A4, London W1J, UK to destination[3]:51.495042,-0.131382: 2.8 km in 9 mins
origin[0]:A4, London W1J, UK to destination[4]:21.118692,73.117554: 11,411 km in 7 days 5 hours
距离在结果中返回(至少在 Google Maps Javascript API v3 中,也应该在网络服务中):results[j].distance.text
代码片段:
var geocoder;
var map;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';
var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';
var locations;
function initialize() {
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
});
geocoder = new google.maps.Geocoder();
$userLat1 = 51.509904342252;
$userLong1 = -0.13413459062576;
$userLat2 = 51.517618;
$userLong2 = -0.096778;
$userLat3 = 51.5017863;
$userLong4 = -0.0536478;
// $userLat = 51.509904342252;
// $userLong = -0.13413459062576;
$lat2 = 51.495042;
$long2 = -0.131382;
$userExtralat2 = 21.118692,
$userExtralong2 = 73.117554
locations = [new google.maps.LatLng($userLat1, $userLong1),
new google.maps.LatLng($userLat2, $userLong2),
new google.maps.LatLng($userLat3, $userLong4),
// new google.maps.LatLng($userLat, $userLong),
new google.maps.LatLng($lat2, $long2),
new google.maps.LatLng($userExtralat2, $userExtralong2)
];
}
function calculateDistances() {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: locations,
destinations: locations,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = document.getElementById('outputDiv');
outputDiv.innerHTML = '';
deleteOverlays();
outputDiv.innerHTML += "origin[0]:" + locations[0].toUrlValue(6) + '<br>';
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
// addMarker(destinations[j], true);
outputDiv.innerHTML += "origin[" + i + "]:" + origins[i] + ' to destination[' + j + ']:' + locations[j].toUrlValue(6) /* destinations[j] */ + ': ' + results[j].distance.text + ' in ' + results[j].duration.text + '<br>';
}
}
}
}
function addMarker(location, isDestination) {
var icon;
if (isDestination) {
icon = destinationIcon;
} else {
icon = originIcon;
}
geocoder.geocode({
'address': location
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: icon
});
markersArray.push(marker);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
function deleteOverlays() {
for (var i = 0; i < markersArray.length; i++) {
markersArray[i].setMap(null);
}
markersArray = [];
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
#content-pane {
width: 100%;
padding-left: 2%;
}
#outputDiv {
font-size: 11px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
<div id="content-pane">
<div id="inputs">
<p>
<button type="button" onclick="calculateDistances();">Calculate distances
</button>
</p>
</div>
<div id="outputDiv"></div>
</div>