javascript中两个位置之间的距离

 薛薛Sying 发布于 2023-02-10 14:47

有人可以帮我解决这个代码片段出了什么问题吗?我最终的目标是使用google javascript API查找距离两个位置.我已经使用geocomplete jquery函数进行地址自动完成.点击搜索没有任何事情发生.我只是一个初学者请帮忙



  
    Distance between Two Places
    
    
     
     
  
  

    

Nouphal.M.. 8

您可以使用Google地图的DistanceMatrixService来计算两点之间的距离.

以下函数计算

function calculateDistances() {
  origin = document.getElementById('source').value; //Get the source string
  destination = document.getElementById('dest').value; //Get the destination string
  var service = new google.maps.DistanceMatrixService(); //initialize the distance service
  service.getDistanceMatrix(
    {
      origins: [origin], //set origin, you can specify multiple sources here
      destinations: [destination],//set destination, you can specify multiple destinations here
      travelMode: google.maps.TravelMode.DRIVING, //set the travelmode
      unitSystem: google.maps.UnitSystem.METRIC,//The unit system to use when displaying distance
      avoidHighways: false,
      avoidTolls: false
    }, calcDistance); // here calcDistance is the call back function
}

回调函数calcDistance的代码

function calcDistance(response, status) {
  if (status != google.maps.DistanceMatrixStatus.OK) { // check if there is valid result
    alert('Error was: ' + status);
  } else {
    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;
    deleteOverlays();

    for (var i = 0; i < origins.length; i++) {
      var results = response.rows[i].elements;

      for (var j = 0; j < results.length; j++) {

        alert ('Distance from '+origins[i] + ' to ' + destinations[j]
            + ': ' + results[j].distance.text ); // alert the result
      }
    }
  }
}

你可以在这里找到一个工作演示

1 个回答
  • 您可以使用Google地图的DistanceMatrixService来计算两点之间的距离.

    以下函数计算

    function calculateDistances() {
      origin = document.getElementById('source').value; //Get the source string
      destination = document.getElementById('dest').value; //Get the destination string
      var service = new google.maps.DistanceMatrixService(); //initialize the distance service
      service.getDistanceMatrix(
        {
          origins: [origin], //set origin, you can specify multiple sources here
          destinations: [destination],//set destination, you can specify multiple destinations here
          travelMode: google.maps.TravelMode.DRIVING, //set the travelmode
          unitSystem: google.maps.UnitSystem.METRIC,//The unit system to use when displaying distance
          avoidHighways: false,
          avoidTolls: false
        }, calcDistance); // here calcDistance is the call back function
    }
    

    回调函数calcDistance的代码

    function calcDistance(response, status) {
      if (status != google.maps.DistanceMatrixStatus.OK) { // check if there is valid result
        alert('Error was: ' + status);
      } else {
        var origins = response.originAddresses;
        var destinations = response.destinationAddresses;
        deleteOverlays();
    
        for (var i = 0; i < origins.length; i++) {
          var results = response.rows[i].elements;
    
          for (var j = 0; j < results.length; j++) {
    
            alert ('Distance from '+origins[i] + ' to ' + destinations[j]
                + ': ' + results[j].distance.text ); // alert the result
          }
        }
      }
    }
    

    你可以在这里找到一个工作演示

    2023-02-10 14:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有