Javascript 谷歌地图API搜索

Javascript 谷歌地图API搜索,javascript,arrays,google-maps,google-api,Javascript,Arrays,Google Maps,Google Api,我目前正在开发一个使用谷歌地图API的Javascript程序。 它需要做的是: 允许用户输入位置 一旦用户单击“查找”按钮,它就会将用户输入的位置转换为经度和纬度坐标 然后,它运行一个算法来计算用户输入的位置与20个硬编码lng和lat坐标之间的距离 它将以公里为单位输出距离 然后它必须循环通过,找到最短的距离,抓取lng和lat id,以便我知道最短的距离是在什么位置 我已经设法完成了第1步和第2步,但我找不到一种方法来存储位置索引(识别位置的方法)以及lng和lat点,以便我可以循环通过它

我目前正在开发一个使用谷歌地图API的Javascript程序。 它需要做的是:

  • 允许用户输入位置
  • 一旦用户单击“查找”按钮,它就会将用户输入的位置转换为经度和纬度坐标
  • 然后,它运行一个算法来计算用户输入的位置与20个硬编码lng和lat坐标之间的距离
  • 它将以公里为单位输出距离
  • 然后它必须循环通过,找到最短的距离,抓取lng和lat id,以便我知道最短的距离是在什么位置
  • 我已经设法完成了第1步和第2步,但我找不到一种方法来存储位置索引(识别位置的方法)以及lng和lat点,以便我可以循环通过它们并将它们与用户输入的位置进行比较。我也无法找到找到最短距离的方法,所有点都无法找到最近的lng和lat

    任何帮助都将不胜感激


    谢谢

    我认为你应该能够根据自己的目的调整这个答案

    //forumla for calculating distances between points
    var getDistance = function(p1, p2) {
      var rad = function(x) {return x*Math.PI/180;}
      var R = 6371; // earth's mean radius in km
      var dLat  = rad(p2.lat() - p1.lat());
      var dLong = rad(p2.lng() - p1.lng());
    
      var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
              Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) * Math.sin(dLong/2) * Math.sin(dLong/2);
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
      var d = R * c;
    
      return d.toFixed(3);
    }
    
    // LatLng object with your current coordinates
    var yourLocation = new google.maps.LatLng(yourlat, yourlng);
    
    // populate an array of hard coded LatLng objects 
    var twentypoints = {};
    twentypoints['hardcodedName1'] = new google.maps.LatLng(lat1, lng1);
    twentypoints['hardcodedName2'] = new google.maps.LatLng(lat2, lng2);
    twentypoints['hardcodedName3'] = new google.maps.LatLng(lat3, lng3);
      ...
    
    // calculate the minimum distance
    var closestLocName, minDist;
    for (var hardcodedName in twentypoints) {
      tempMinDist = getDistance(twentypoints[hardcodedName], yourLocation));
      if (minDist === undefined || tempMinDist < minDist) {
        minDist = tempMinDist;
        closestLocName = hardcodedName;
      }
    }
    
    console.log('Closest location is called ' + closestLocName + '.');
    console.log('Closest location is ' + minDist + 'km away.');
    
    //用于计算点之间距离的forumla
    var getDistance=函数(p1、p2){
    var rad=函数(x){return x*Math.PI/180;}
    var R=6371;//地球的平均半径,单位为km
    var dLat=rad(p2.lat()-p1.lat());
    var dLong=rad(p2.lng()-p1.lng());
    变量a=Math.sin(dLat/2)*Math.sin(dLat/2)+
    Math.cos(rad(p1.lat())*Math.cos(rad(p2.lat())*Math.sin(dLong/2)*Math.sin(dLong/2);
    var c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
    var d=R*c;
    返回d.toFixed(3);
    }
    //将对象与当前坐标对齐
    var yourLocation=new google.maps.LatLng(yourlat,yourlng);
    //填充硬编码板条对象数组
    var twentypoints={};
    twentypoints['hardcodedName1']=新的google.maps.LatLng(lat1,lng1);
    twentypoints['hardcodedName2']=新的google.maps.LatLng(lat2,lng2);
    twentypoints['hardcodedName3']=新的google.maps.LatLng(lat3,lng3);
    ...
    //计算最小距离
    var closestLocName,明德主义者;
    用于(var硬编码名称,单位为二十分){
    tempMinDist=getDistance(二十点[硬编码名称],您的位置));
    if(minDist==未定义| | tempMinDist
    嗨!欢迎来到SO。向我们展示一些代码,这样我们就可以共享一个共同的起点。