将距离矩阵与地点和距离api结合使用

恶魔

我需要在自动位置搜索和路线API中使用计算距离API。我已经组合了地点搜索和路线API,但无法将其与距离矩阵API结合使用。任何人都可以给我,知道怎么做吗?

这是我用于位置搜索和路线API的代码,并且可以正常工作。

function initAutocomplete() {

    var origin_place_id = null;
    var destination_place_id = null;

    var travel_mode = google.maps.TravelMode.DRIVING;

    var map = new google.maps.Map(document.getElementById('map-canvas'), {
      mapTypeControl: false,
      center: {lat: -33.8688, lng: 151.2195},
      zoom: 13
    });

    var directionsService = new google.maps.DirectionsService;
    var directionsDisplay = new google.maps.DirectionsRenderer;
    directionsDisplay.setMap(map);

    var origin_input = document.getElementById('origin-input');
    var destination_input = document.getElementById('destination-input');


    map.controls[google.maps.ControlPosition.TOP_LEFT].push(origin_input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(destination_input);


    var origin_autocomplete = new google.maps.places.Autocomplete(origin_input);
    origin_autocomplete.bindTo('bounds', map);

    var destination_autocomplete = new google.maps.places.Autocomplete(destination_input);
    destination_autocomplete.bindTo('bounds', map);

    // Sets a listener on a radio button to change the filter type on Places
    // Autocomplete.

    function expandViewportToFitPlace(map, place) {
      if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
      } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);
      }
    }

    origin_autocomplete.addListener('place_changed', function() {
      var place = origin_autocomplete.getPlace();
      if (!place.geometry) {
        window.alert("Autocomplete's returned place contains no geometry");
        return;
      }
      expandViewportToFitPlace(map, place);

      // If the place has a geometry, store its place ID and route if we have
      // the other place ID
      origin_place_id = place.place_id;
      route(origin_place_id, destination_place_id, travel_mode,
            directionsService, directionsDisplay);
    });

    destination_autocomplete.addListener('place_changed', function() {
      var place = destination_autocomplete.getPlace();
      if (!place.geometry) {
        window.alert("Autocomplete's returned place contains no geometry");
        return;
      }
      expandViewportToFitPlace(map, place);

      // If the place has a geometry, store its place ID and route if we have
      // the other place ID
      destination_place_id = place.place_id;
      route(origin_place_id, destination_place_id, travel_mode,
            directionsService, directionsDisplay);
    });

    function route(origin_place_id, destination_place_id, travel_mode,
                   directionsService, directionsDisplay) {
      if (!origin_place_id || !destination_place_id) {
        return;
      }
      directionsService.route({
        origin: {'placeId': origin_place_id},
        destination: {'placeId': destination_place_id},
        travelMode: travel_mode
      }, function(response, status) {
        if (status === google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        } else {
          window.alert('Directions request failed due to ' + status);
        }
      });
    }

}
Xomena

如@geocodezip所述,您可以从路线服务响应中检索距离信息。

代码段如下:

directionsService.route({
    origin: {'placeId': origin_place_id},
    destination: {'placeId': destination_place_id},
    travelMode: travel_mode
}, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        //Get distance
        var d = response.routes[0].legs[0].distance.text;
        //TODO: show the distance at some div
        ....
    } else {
        window.alert('Directions request failed due to ' + status);
    }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将Google Apps API密钥与距离矩阵配合使用

来自分类Dev

使用密码计算距离(谷歌地图距离矩阵 API)

来自分类Dev

使用带有UWP的Google Places API将数据绑定到ListView以显示到附近地点的距离

来自分类Dev

Javascript和异步问题(使用yandex maps API建立距离矩阵)

来自分类Dev

将交叉距离矩阵转换为距离矩阵

来自分类Dev

使用熊猫的欧几里得距离矩阵

来自分类Dev

使用带有pdist和squareform的nparray创建距离矩阵

来自分类Dev

使用Numpy和Cython加速距离矩阵计算

来自分类Dev

在代理后面使用googlemaps距离矩阵python API

来自分类Dev

在Python中使用Google距离矩阵API时出错

来自分类Dev

Java使用谷歌距离矩阵api获取请求错误

来自分类Dev

Excel 中的 Google Api 计算地点之间的行驶距离

来自分类Dev

Google API距离矩阵,距离没有值

来自分类Dev

如何存储地点之间的距离?

来自分类Dev

如何获得和矩阵而不是距离

来自分类Dev

谷歌地理编码和距离矩阵的差异

来自分类Dev

Google Maps API距离矩阵请求

来自分类Dev

谷歌的距离矩阵API是否也考虑“海拔”?

来自分类Dev

无法从 Google 距离矩阵 api 获得响应

来自分类Dev

如何使用随机距离矩阵构建图?

来自分类Dev

使用阿多尼斯比较距离矩阵

来自分类Dev

使用动态规划计算矩阵距离

来自分类Dev

使用自己的计算熊猫创建距离矩阵

来自分类Dev

如何使用精度和距离

来自分类Dev

将交叉距离矩阵从“ Crossdist”类转换为矩阵

来自分类Dev

压缩距离矩阵和冗余距离矩阵有什么区别?

来自分类Dev

创建距离矩阵?

来自分类Dev

R:使用tm和proxy计算距术语文档矩阵的余弦距离

来自分类Dev

如何使用 c 打印 Bellman Ford 的路径和最终距离矩阵?

Related 相关文章

  1. 1

    将Google Apps API密钥与距离矩阵配合使用

  2. 2

    使用密码计算距离(谷歌地图距离矩阵 API)

  3. 3

    使用带有UWP的Google Places API将数据绑定到ListView以显示到附近地点的距离

  4. 4

    Javascript和异步问题(使用yandex maps API建立距离矩阵)

  5. 5

    将交叉距离矩阵转换为距离矩阵

  6. 6

    使用熊猫的欧几里得距离矩阵

  7. 7

    使用带有pdist和squareform的nparray创建距离矩阵

  8. 8

    使用Numpy和Cython加速距离矩阵计算

  9. 9

    在代理后面使用googlemaps距离矩阵python API

  10. 10

    在Python中使用Google距离矩阵API时出错

  11. 11

    Java使用谷歌距离矩阵api获取请求错误

  12. 12

    Excel 中的 Google Api 计算地点之间的行驶距离

  13. 13

    Google API距离矩阵,距离没有值

  14. 14

    如何存储地点之间的距离?

  15. 15

    如何获得和矩阵而不是距离

  16. 16

    谷歌地理编码和距离矩阵的差异

  17. 17

    Google Maps API距离矩阵请求

  18. 18

    谷歌的距离矩阵API是否也考虑“海拔”?

  19. 19

    无法从 Google 距离矩阵 api 获得响应

  20. 20

    如何使用随机距离矩阵构建图?

  21. 21

    使用阿多尼斯比较距离矩阵

  22. 22

    使用动态规划计算矩阵距离

  23. 23

    使用自己的计算熊猫创建距离矩阵

  24. 24

    如何使用精度和距离

  25. 25

    将交叉距离矩阵从“ Crossdist”类转换为矩阵

  26. 26

    压缩距离矩阵和冗余距离矩阵有什么区别?

  27. 27

    创建距离矩阵?

  28. 28

    R:使用tm和proxy计算距术语文档矩阵的余弦距离

  29. 29

    如何使用 c 打印 Bellman Ford 的路径和最终距离矩阵?

热门标签

归档