首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >计算融合表中的点之间的距离

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

Stack Overflow用户
提问于 2013-05-26 15:15:38
回答 1查看 3.5K关注 0票数 6

我的编程知识非常有限,我正在做一个包括编程在内的大学项目。

我想做的是一张地图,它显示了你当前的位置和回收点的位置。我已经完成了当前位置部分,并使用融合表在地图上显示回收点。但我也想给你一个选择,找到你现在的位置和回收点之间的最短路线。

所以它要做的是计算你当前位置和每个回收点之间的距离,并显示最短的一个。

现在,我正在尝试理解谷歌地图api教程,我不知道这是否可能,使用融合表。所以我想知道有没有人知道怎么做。

非常感谢!

代码语言:javascript
运行
复制
<!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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-26 17:46:32

更新

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

代码语言:javascript
运行
复制
// 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上也有一个例子。

代码语言:javascript
运行
复制
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通过人行道和人行道询问步行方向。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16757011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档