【发布时间】:2015-07-10 18:09:46
【问题描述】:
我正在尝试在 Google 地图中创建一个动态 SVG 标记(基本上每个标记的 SVG 文本部分都会发生变化),但我正在努力解决它的动态方面。正如您在下面的代码中看到的,我可以使用来自静态 url 的 new google.maps.MarkerImage() 创建一个 SVG 标记。
var marker = new google.maps.Marker({
position: new google.maps.LatLng(38.4269929, -81.7921109),
icon: new google.maps.MarkerImage('https://dl.dropboxusercontent.com/u/64084190/test-marker.svg',
null, null, null, new google.maps.Size(25, 25)),
map: map
});
但我似乎无法让 google.maps.MarkerImage() 将 SVG DOM 元素的内部 html 作为其第一个参数 - 它总是在寻找一个 URL。
<svg id="svg_elem"></svg>
function createDynamicSVGMarker(id){
var svg_elem = d3.select('#svg_elem');
svg_elem.append('path')
.attr('fill', "#3875D7")
.attr('d', "M100 0h-100v100h36.768l13.431 19.876 13.431-19.876h36.37z");
svg_elem.append('text')
.attr('transform', "translate(30.979 80)")
.attr("fill", "#fff")
.attr("font-size", "36")
.text(id);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(38.4269929, -81.7921109),
icon: new google.maps.MarkerImage($('#svg_elem').html(),
null, null, null, new google.maps.Size(25, 25)),
map: map
});
$('#svg_elem').empty();
}
我应该使用MarkerImage 还是有更好的方法来解决这个问题?我曾尝试在 node.js 中执行此操作(但最终失败)-为每个 id 创建一个新的 SVG(或者甚至创建 SVG,然后另存为 PNG)-但我认为在客户端。我愿意接受使用 node.js 或客户端代码的建议。
【问题讨论】:
标签: jquery node.js google-maps d3.js leaflet