在javascript中查找中间纬度和经度



我有以下数据。1.来源名称。2.目的地名称。我需要在从源到目的地的路径上找到lat-long对。有谷歌Api可供使用吗??。我参考了这个问题,在两个给定的点之间寻找中间的Latitude经度,但不能理解整个问题。对此,有人能有更简单的答案吗?

编辑这是我试过的

 //store lat and long
    var sd_array=[];
        $('#processbtn').click(function(e){
    //get source and destination name       
                var source=$('#source').val();
                var destination=$('#destination').val();
                if(source==''||destination==''){
                    alert("all fields required");
                }else{
                   //decode lat-long from address
                    getLatitudeAndLongitude(source);
                    getLatitudeAndLongitude(destination);
                }
            });
        function getLatitudeAndLongitude(address){
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': address}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
               //store to array
                sd_array.push(latitude);
                sd_array.push(longitude);
              }
            }); 
        }
    function getInterMediateLatLong(sd_array){
      //get intermediate locations 
      /*sd_array[0] source lat
      sd_array[1] source long
      sd_array[2] destination lat
      sd_array[3] destination long*/
    }

我需要帮助来获得中间位置。

你必须首先明白这是一个数学问题,但很简单。谷歌将返回一组XY点。。。程序是:

  1. 正确表达"参考点"SDS源和Ddestination点。所以Sx,Sy,Dx,Dy。它们不是"名字",而是XY坐标(!)。

  2. 检查如何对Google-API说你需要多少点

  3. 将您的查询提交到Google-API:编辑您的问题以向我们显示您的查询(Javascript代码)我们可以在这里讨论。


你需要表达

{path[]: LatLngPathes, samples: 4}

Javascript片段示例获取LatLngPathes:

 var source = new google.maps.LatLng(36.578581, -118.291994);
 var destination = new google.maps.LatLng(36.606111, -118.062778);

由于lat和lon中每一个的基础类型都是实数(也称为double或float),因此只需添加源变量和端点变量,并将每个结果除以2.0。

var mid_lat = (lat_source + lat_dest ) / 2.0 ;
var mid_lon = (lon_source + lon_dest ) / 2.0 ;

那么中点的坐标是mid_lat,mid_lon。

最新更新