在Google地图API上显示每个航点销的标签 [英] Display Label for Each Waypoint Pin on Google Map API

查看:307
本文介绍了在Google地图API上显示每个航点销的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为路线上的每个航点添加一个标签,但我不太清楚应该如何处理。在做了一些研究之后,我明白你可以添加一个带有标签的自定义引脚,但那是我手动引脚删除每个引脚的时候。

解决方案

如果您想从DirectionsRenderer请求获取需要黑客入侵的标记,因为那里是不是谷歌地图API做官方的方式。



有一个办法,这里是我做的一个例子:


I'm trying to add a label for each waypoint along the route, but I'm not quite sure how I should approach it. After doing some research, I understand that you can add a custom pin with label, but that's when I drop each pin manually. How can I do this for direction?

解决方案

If you want to get access to markers from DirectionsRenderer request that need a hack because there is not official way to do this from google map api.

There is a way around, here is an example I made: https://jsfiddle.net/TomKarachristos/cna78jbw/

google.maps.event.addListener(directionsDisplay, 'directions_changed', 
    function() {
        var self = this;
        markersArray = []; //empty the array
        setTimeout(function() { //wait until markers render
          for (var k in self) { //search everything in directionsDisplay
            if (typeof self[k].markers != 'undefined') { //  its that the markers?
              var markers = self[k].markers;
              for (var i = 0; i < markers.length; ++i) {
                markersArray.push(markers[i]);
                markersArray[i].setLabel({ //lets change the label!
                  color: "white",
                  fontSize: "16px",
                  text: i.toString()
                });
              }
            }
          }
          console.log(markersArray); // now markersArray have all the markers 
        }, 0);
      });

But this is not a good solution if you want full control of markers. If you supress the markers you have many possibilities, you can use markers from libraries like markerLabel or RichMarker(use a dom element for marker!)

Here an example with markerLabel, you click anywhere in the map and a marker appear with a label with the distance from the center marker. https://jsfiddle.net/TomKarachristos/x1zg503m/

[...]
 markerArray[1] = new MarkerWithLabel({
    position: location,
    map: map,
    animation: google.maps.Animation.DROP, //just for fun
    labelContent: "",
    labelClass: "marker-label"
  });
[...]
if (status === google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      route = directionsDisplay.getDirections().routes[0];
      markerArray[1].set('labelContent', route.legs[0].distance.value / 1000)
[...]

more easy to make, more nice to see, more fun!

这篇关于在Google地图API上显示每个航点销的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆