简体   繁体   English

使用谷歌地图距离矩阵api JSON输出获取距离

[英]get distance using google maps distance matrix api JSON output

I developed the following code to get the distance of the given 2 cities through 2 textfields and i want to send them to google maps distance matrix api using the following URl.我开发了以下代码以通过 2 个文本字段获取给定 2 个城市的距离,我想使用以下 URl 将它们发送到谷歌地图距离矩阵 api。 I send those cities and got the JSON output file.我发送这些城市并获得 JSON 输出文件。 but my problem is how can i get the specific attribute(distance) from that JSON file and display on a another textfield.但我的问题是如何从该 JSON 文件中获取特定属性(距离)并显示在另一个文本字段上。

function m() {

            var x = document.getElementById("autocomplete").value;
            var y = document.getElementById("autocomplete1").value;

            var url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + x + "&destinations=" + y + "&key=AIzaSyD5KX6VRon6yQ0vu4s6GSnAVzazRWg8wrc";

window.location=url; // this will go to the above url and display the JSON output file on the browser. I dont want that to be shown. just some processing and should display only the distance form that file on a textfield!

}

the JSON output file http://pastebin.ca/3318529 JSON 输出文件http://pastebin.ca/3318529

Please help to solve this.请帮助解决这个问题。 i am new to JSON.我是 JSON 新手。 So if you have any other ideas tell me.因此,如果您有任何其他想法,请告诉我。 Many thanks :)非常感谢 :)

If you use Javascript in Browser, you need use Google Maps Api.如果您在浏览器中使用 Javascript,则需要使用 Google Maps Api。

Check this example:检查这个例子:

<script src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script>
function init() {
  var service = new google.maps.DistanceMatrixService;
  service.getDistanceMatrix({
    origins: ['Los Angeles'],
    destinations: ['New York'],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC,
    avoidHighways: false,
    avoidTolls: false
  }, function(response, status) {
    if (status !== google.maps.DistanceMatrixStatus.OK) {
      alert('Error was: ' + status);
    } else {
      alert(response.originAddresses[0] + ' --> ' + response.destinationAddresses[0] + ' ==> ' + response.rows[0].elements[0].distance.text);
    }
  });
}
</script>
<body onload="init()">
</body>

Running example https://jsfiddle.net/3b03m5mt/运行示例https://jsfiddle.net/3b03m5mt/

You can check out this code您可以查看此代码

 <?php $addressFrom="Your from address"; $addressTo="your to address"; // Change address format $formattedAddrFrom = str_replace(' ', '+', $addressFrom); $formattedAddrTo = str_replace(' ', '+', $addressTo); // Geocoding API request with start address $distance_data = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins='.$formattedAddrFrom.'&destinations='.$formattedAddrTo.'&sensor=false&key=YOUR-API-KEY'); $output_data = json_decode($distance_data); if(!empty($output_data->error_message)){ return $output_data->error_message; } //echo "<pre>"; //print_r($output_data) ; $output_status = $output_data->status; $output_km =$output_data->rows[0]->elements[0]->distance->text; $output_meter =$output_data->rows[0]->elements[0]->distance->value; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM