对于谷歌地图标记,可以在svg中加入多种颜色

时间:2015-10-28 08:38:25

标签: javascript html google-maps svg google-maps-markers

所以iv现在试图做两天就是。

    var arr = [
               {
                   path: "M239.68,331.38c34.206,58.142,87.12,150.48,87.12,150.48s49.609-84.922,88.56-149.76C327.52,275.94,241.84,326.34,239.68,331.38z",
                   fillColor: 'red',
                   fillOpacity: 1,
                   anchor: new google.maps.Point(0, 0),
                   strokeWeight: 0,
                   scale: .6,
               },
               {
                   path: "M150 0 L75 200 L225 200 Z",
                   fillColor: 'black',
                   fillOpacity: 1,
                   anchor: new google.maps.Point(0, 0),
                   strokeWeight: 0,
                   scale: .6,
               }
              ];
              var marker = new google.maps.Marker({
                  position: location,
                  map: map,
                  draggable: false,
                  icon: arr,
                  zIndex: -20,

              });

所以iv基本上试图通过组合两个或多个svgs并使用它作为标记来动态构建svg,这有可能吗?有人知道吗?我想要设计风格的唯一重要属性是颜色,所以我有一个标记有多种颜色。

1 个答案:

答案 0 :(得分:1)

在HTML中,你会看到你的" svg符号"转换为实际的SVG

您可以使用许多库来创建SVG更容易,或者您可以学习如何直接在javascript中创建它...它比您想象的更容易 - 但我对它进行了硬编码 - 您可以使用标准DOM getElementById等轻松更改SVG中的颜色 -

例如,查看原始颜色是红色和黑色以及行

document.getElementById('p1').setAttribute('fill','green');

将红色位设置为绿色 -



var map;
var polyLine;
var polyOptions;

function initialize() {
    var mapOptions = {
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: new google.maps.LatLng(0,0)
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    
    google.maps.event.addListener(map, 'click', function(event) {

        addPoint(event);
    });
}

function addPoint(event) {
    document.getElementById('p1').setAttribute('fill','green');
	var iconUrl = "data:image/svg+xml;charset=utf-8," + escape(document.getElementById("marker").innerHTML)
    var icon = {
        url: iconUrl,
        anchor: new google.maps.Point(0,0),
        scaledSize: new google.maps.Size(40,55)
    }

    var marker = new google.maps.Marker({
        position: event.latLng,
        map: map,
        draggable: false,
        icon: icon,
        zIndex : -20
    });
}

initialize();

<script src="http://maps.googleapis.com/maps/api/js?sensor=false&x=.js"></script>
<div id="map-canvas" style="height:400px"></div>
<div id='marker' style='display:none'><svg 
    xmlns:svg="http://www.w3.org/2000/svg" 
    xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    width="110mm" height="150mm" 
    id="svg111" 
    version="1.1">
<path id="p1"
    fill="red"
    fill-opacity="1"
    stroke-weight="0" 
    d="M239.68,331.38c34.206,58.142,87.12,150.48,87.12,150.48s49.609-84.922,88.56-149.76C327.52,275.94,241.84,326.34,239.68,331.38z" />
<path id="p2"
    fill="black"
    fill-opacity="1"
    stroke-weight="0" 
    d="M150 0 L75 200 L225 200 Z" />
    </svg></div>
&#13;
&#13;
&#13;