计算融合表中各点之间的距离

时间:2013-05-26 07:15:38

标签: javascript google-maps-api-3 geolocation distance google-fusion-tables

我的编程知识非常有限,我正在开展一个包含编程的大学项目。

我想要做的是一张地图,显示您当前的位置和回收点的位置。我已经完成了当前位置部分,并使用融合表在地图上显示回收点。但我也希望能够找到当前位置和回收点之间的最短路线。

那么它的目的是计算当前位置和每个回收点之间的距离,并显示最短的距离。

现在我正在尝试了解谷歌地图api教程,我不知道这是否可行,使用融合表。所以我想知道是否有人知道如何做到这一点。

非常感谢你!

<!DOCTYPE html>
<html>
<head>
<section id="wrapper">
Clique no botão "permitir" para deixar o browser encontrar a sua localização
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<article>
</article>
<script>


function success(position) {
  var mapcanvas = document.createElement('div');
  mapcanvas.id = 'mapcontainer';
  mapcanvas.style.height = '350px';
  mapcanvas.style.width = '450px';
  document.querySelector('article').appendChild(mapcanvas);

  var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

  var options = {
    zoom: 15,
    center: coords,
    mapTypeControl: false,
    navigationControlOptions: {
        style: google.maps.NavigationControlStyle.SMALL
    },
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById("mapcontainer"), options);
  var marker = new google.maps.Marker({
      position: coords,
      map: map,
      title:"You are here!"
  });

  var layer = new google.maps.FusionTablesLayer({
    query: {
    select: 'Coordenadas',
    from: '1CwMDrcxebjsb8sjG42rbGVAZB25Zi7CvaLJXOCM'
  },


});
 layer.setMap(map)
}


if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success);
} else {
  error('Geo Location is not supported');
}
</script>
</section>

</head>
<body>

</body>

</html>

1 个答案:

答案 0 :(得分:6)

<强>更新

您可以通过向Google Visualization API发送查询来检索最近的点坐标:

// Initiate the GViz query
var queryList = [];
queryList.push("SELECT Location FROM ");
queryList.push(tableId);
queryList.push(" ORDER BY ST_DISTANCE(Location, LATLNG(");
queryList.push(currentLat + "," + currentLng);
queryList.push(")) ");
queryList.push(" LIMIT 1");

var queryText = encodeURIComponent(queryList.join(''));
var query = new google.visualization.Query(
                         'http://www.google.com/fusiontables/gvizdata?tq=' +
                          queryText);

// Handling the result of nearest location query
query.send(function(response) {
    dataTable = response.getDataTable();

    // If there is any result
    if (dataTable && dataTable.getNumberOfRows()) {
        console.log("Nearest Point is: " + dataTable.getValue(0,0));

        // Result holds the coordinates of nearest point
        var result = dataTable.getValue(0,0).split(",");

        // Creates a Google Map LatLng from "result"
        var latlng = new google.maps.LatLng(result[0], result[1]);
        showRoute(latlng);
    }

});

您可以查看实时示例Here


您可以将Distance Matrix Service用于此目的。您只需从当前位置读取您的来源,并向Distance Matrix Service发送每个回收点的请求。 Distance Matrix Sample还有一个例子。

var mylocation = new google.maps.LatLng(CURRENT_LOCATION_LAT, CURRENT_LOCATION_LNG);

// Destination by address
var destination1 = "Stockholm, Sweden";

// or destination by lat and lng
var destination2 = new google.maps.LatLng(50.087692, 14.421150);

// Sending request
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
  {
    origins: [mylocation],
    destinations: [destination1, destination2],
    travelMode: google.maps.TravelMode.DRIVING, 
    avoidHighways: false,
    avoidTolls: false
  }, callback);

function callback(response, status) {
  // See Parsing the Results for
  // the basics of a callback function.
}

您可以指定相应计算持续时间的Travel Mode

  

google.maps.TravelMode.DRIVING (默认)表示使用道路网络的标准行车路线。
   google.maps.TravelMode.BICYCLING 要求通过自行车道和骑自行车路径骑自行车。首选街道    google.maps.TravelMode.TRANSIT 通过公共交通路线请求路线。    google.maps.TravelMode.WALKING 通过行人路径请求步行路线&amp;人行道。