【问题标题】:Google Maps Directions using 1-2-3 instead of A-B-CGoogle Maps Directions 使用 1-2-3 而不是 A-B-C
【发布时间】:2016-10-14 22:39:45
【问题描述】:

我在 Google 地图上使用 Google Maps API。

问题是,它在整个路径中显示了几个站点,将它们标记为 A-B-C-D...Z,但我需要将其更改为 1-2-3-4-..99,

这是我的代码;

directionsService.route({
    origin: $( "#input-directions-start-point" ).val(),
    destination: $( "#input-directions-end-point" ).val(),
    waypoints: waypts, //other duration points
    optimizeWaypoints: true,
    travelMode: google.maps.TravelMode.DRIVING
  }, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      console.log(response);
    } else {
      vts.alertDialog( window.localization.error,
        window.localization.error_direction_calculate + ": " + status,
        BootstrapDialog.TYPE_DANGER);
    }
  });

这是屏幕截图;

提前致谢

【问题讨论】:

  • 你试过什么?您的代码未显示替换默认标记的尝试。
  • 我没有任何想法要解决。 API 不支持该功能
  • DirectionsRendererOptions 有一个suppressMarkers 属性。见developers.google.com/maps/documentation/javascript/…。如果您有不同的航点坐标,我想您可以放置​​自己的标记而不是默认标记。

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


【解决方案1】:

google.maps.DirectionsRendererOptions 有属性suppressMarkers 当你设置为true 时,只会渲染路径而不是标记。

然后您可以使用例如google.maps.Marker 自己渲染标记,它具有label 属性,您可以使用该属性指定标记内的标签,例如可以是数字(您也可以创建自己的自定义通过扩展google.maps.OverlayView 类来标记,或使用RichMarker library)。路线上标记的位置可以从directionsServiceresponse对象解析。

更多内容请关注docs

工作示例:

function init(){
    directionsService = new google.maps.DirectionsService();

    var pos = new google.maps.LatLng(41.218624, -73.748358);
    var myOptions = {
        zoom: 15,
        center: pos,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map'), myOptions);
    directionsDisplay = new google.maps.DirectionsRenderer({map: map, suppressMarkers: true});

    directionsService.route({
        origin: "New York",
        destination: "Chicago",
        waypoints: [{location:"Philadelphia"}, {location:"Boston"}], //other duration points
        optimizeWaypoints: true,
        travelMode: google.maps.TravelMode.DRIVING
    }, function(response, status) {
        if (status === google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
			var my_route = response.routes[0];
            for (var i = 0; i < my_route.legs.length; i++) {
                var marker = new google.maps.Marker({
                    position: my_route.legs[i].start_location,
                    label: ""+(i+1),
                    map: map
                });
            }
            var marker = new google.maps.Marker({
                position: my_route.legs[i-1].end_location,
                label: ""+(i+1),
                map: map
            });
        } else {
            vts.alertDialog( window.localization.error,
                window.localization.error_direction_calculate + ": " + status,
                BootstrapDialog.TYPE_DANGER);
        }
    });

}    
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("maps", "3",{other_params:"sensor=false"});
</script>
<body style="margin:0px; padding:0px;" onload="init()">
	 <div id="map" style="height:400px; width:500px;"></div>
</body>

【讨论】:

  • 如果您所做的只是将我的评论变成答案,那么至少要正确。
  • @MrUpsidown 我从来没有看到你的评论。我花了至少20分钟来写这个答案,你一定是在那段时间写的。为什么你认为你是唯一能想出这个的人?我在我的项目中多次这样做。仅仅为此而投反对票真的很丑陋。请至少说明您对我的回答有什么问题,如果有的话,不要仅仅因为您的偏见而投反对票。
  • 我不认为我是唯一能想出这个的人。但是您答案的第一部分看起来几乎是我评论的副本/粘贴,因此,是的,我投了反对票,不是因为您将其作为答案发布,而是因为您的答案中没有提及我的评论。现在,如果你说你没有看到它,让我们假设这是一个巧合。你的积分回来了。
  • 感谢你们。我还有一件事,当我改变固定点的位置时,方向不会改变。你有什么想法吗?谢谢
  • 我不确定你的意思。如何改变位置,“方向不变”是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 2013-06-13
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多