javascript - 订购路线服务 - 谷歌地图

标签 javascript google-maps google-maps-api-3 maps google-maps-markers

我对临近订单有疑问。 我有一个对象数组:

waypoints = [
 location:{
 lat: -8.116597,
 lng: -79.0347417
},
location:{
 lat: -8.120997,
 lng: -79.038355
},
location:{
 lat: -8.120151,
 lng: -79.037014
},
location:{
 lat: -8.119195,
 lng: -79.036657
},
]

通过这种方式我发送到谷歌地图:

this.$refs.mapa.$mapCreated.then(() => {
          this.directionsService = new google.maps.DirectionsService();
          this.directionsDisplay.setMap(this.$refs.mapa.$mapObject);
          this.directionsService.route({
            origin: { lat: -8.0828174, lng: -79.0953881 },
            destination: this.waypoints[this.waypoints.length - 1].location,
            waypoints: this.waypoints,
            travelMode: 'DRIVING',
            optimizeWaypoints: true,
          }, (r, status) => {
              if (status === 'OK') {
                this.directionsDisplay.setDirections(r);
                console.log(r.routes[0].waypoint_order);
                }
            });
        });

其中应该给我带来最接近原点的点,如果是的话。当我更改对象数组的发送顺序时,就会出现问题。例如,如果我订购,我会打印以下订单:

[0, 1, 2, 4, 3, 5]

但是如果我改变排列顺序然后再排序,打印另一个订单:

[1, 2, 3, 4, 0, 5]

我不知道为什么会发生这种情况,因为如果不是位置(纬度,经度),您不必导入装运单

是不是少了什么东西?

最佳答案

起点目的地在请求中是固定的。 optimizeWaypoints 仅优化航点的顺序。

您的代码正在使用“路径点”之一作为目的地。

相关问题:

关于javascript - 订购路线服务 - 谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49905501/

相关文章:

javascript - 在 Google map 上显示多个多边形并切换每个多边形的可见性

css - 为什么我的 google map api 不能正确显示气球?

java - RCP 和外部浏览器包含

javascript - GraphQL:更新数组

javascript - 如何使用 ElasticSearch 索引源代码

javascript - javascript 中的多个函数

php - 如何将codeigniter View 文件gmap存储到redis中,让 View 显示更快?

JavaScript 运行时错误 : "object doesn' t support property or method"in Internet Explorer

javascript - Google Maps API V3 - 防止 ImageMapType 换行

javascript - 加载 Google map 时打开信息窗口