Google地图:如何更改标记默认红色并添加标签

时间:2016-07-03 13:17:17

标签: google-maps google-maps-markers markers

我认为这起初应该相当容易。但是,如果没有解决方案,我需要花费很多时间在互联网上搜索。

我可以在带有标签的Google地图上添加标记,如下所示:

// Adding a marker to the map 
    var marker1 = new google.maps.Marker({
    position: new google.maps.LatLng(22.48, 114.20),
    map: map,
    title: 'My House',
    label: {
        text: 'A',
        color: '#79E149'
    }
});

默认颜色为红色。

我的问题: 如何将标记的红色更改为其他颜色,例如绿色?

我注意到大多数答案建议使用外部图标,例如:

// Adding a marker to the map 
    var marker1 = new google.maps.Marker({
    position: new google.maps.LatLng(22.48, 114.20),
    map: map,
    title: 'My House',
    **icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',**
    label: {
        text: 'A',
        color: '#79E149'
    }
});

然而: 1.使用此类图标时不会出现标签。

  1. 实际上我不想使用外部图像,因为以后可能会更改URL。事实上,许多图标似乎已经消失,例如: http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png
  2. 谢谢和最好的问候

    亚历

2 个答案:

答案 0 :(得分:1)

  1. 您的标签文字为绿色(#79E149),因此它与标记
  2. 融为一体
  3. 你可能不想要一个" dot"如果您想在标记上添加标签(将标记图标的名称更改为"绿色"来自"绿点")。
  4. 该特定标记/标签组合的图标的LabelOrigin错误
  5. proof of concept fiddle

    代码段

    
    
    var geocoder;
    var map;
    
    function initialize() {
      var map = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: new google.maps.LatLng(22.48, 114.20),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      // Adding a marker to the map 
      var marker1 = new google.maps.Marker({
        position: new google.maps.LatLng(22.48, 114.20),
        map: map,
        title: 'My House',
        icon: {
          url: 'http://maps.google.com/mapfiles/ms/icons/green.png',
          labelOrigin: new google.maps.Point(15, 10)
        },
        label: {
          text: 'A',
          color: 'black'
        }
      });
      var marker2 = new google.maps.Marker({
        position: new google.maps.LatLng(22.49194, 114.204426),
        map: map,
        title: 'My Other House',
        icon: {
          url: 'http://maps.google.com/mapfiles/ms/icons/orange.png',
          labelOrigin: new google.maps.Point(15, 10)
        },
        label: {
          text: 'B',
          color: 'black'
        }
      });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map_canvas"></div>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

试试这个

scala> implicit class PimpedInt(x: Int) {
     |   def +(i: Int, s: String): Int = {
     |     println(s)
     |     x + i
     |   }
     | }
defined class PimpedInt

scala> 1 + (1, "hello")
hello
res8: Int = 2