javascript - Google map api 和自定义标记

标签 javascript google-maps

我正在使用谷歌地图API并想要创建一些自定义标记,除了颜色之外它们都是相同的,我不想像这样重复代码

 // Add a custom marker
var marker1 = {
    path: 'M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z',
    fillColor: '#ff61b4',
    fillOpacity: 0.95,
    scale: 2,
    strokeColor: '#fff',
    strokeWeight: 3,
    anchor: new google.maps.Point(12, 24)
};

var marker2 = {
    path: 'M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z',
    fillColor: '#05950a',
    fillOpacity: 0.95,
    scale: 2,
    strokeColor: '#fff',
    strokeWeight: 3,
    anchor: new google.maps.Point(12, 24)
};

当我想使用标记时

 // Markers
var marker = new google.maps.Marker({
    map: map,
    icon: marker, // can i override the fillColor here ?
    position: new google.maps.LatLng(51.489401, -3.203586),
    title: 'title'
});

我希望能够声明一个标记,然后覆盖 fillColor,我该怎么做?

谢谢

最佳答案

一种选择是使用函数来生成图标(即 createIcon 函数),该函数将颜色作为参数并返回图标匿名对象:

function createIcon(color) {
  return {
    path: 'M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z',
    fillColor: color,
    fillOpacity: 0.95,
    scale: 2,
    strokeColor: '#fff',
    strokeWeight: 3,
    anchor: new google.maps.Point(12, 24)
  };
}

然后在创建标记时使用它:

var marker1 = new google.maps.Marker({
    map: map,
    position: {
      lat: 37.448,
      lng: -122.143
    },
    icon: createIcon('#ff61b4')
  });

proof of concept fiddle

代码片段:

function createIcon(color) {
  return {
    path: 'M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z',
    fillColor: color,
    fillOpacity: 0.95,
    scale: 2,
    strokeColor: '#fff',
    strokeWeight: 3,
    anchor: new google.maps.Point(12, 24)
  };
}

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var marker = new google.maps.Marker({
    map: map,
    position: map.getCenter(),
    icon: createIcon('blue')
  });
  var marker1 = new google.maps.Marker({
    map: map,
    position: {
      lat: 37.448,
      lng: -122.143
    },
    icon: createIcon('#ff61b4')
  })
  var marker2 = new google.maps.Marker({
    map: map,
    position: {
      lat: 37.44,
      lng: -122.148
    },
    icon: createIcon('#05950a')
  });

}
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>

关于javascript - Google map api 和自定义标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42098957/

相关文章:

javascript - 从模块内部访问 Express mountpath

PHP/Google Maps API - 验证街道地址和标准化/防止重复的最简单方法?

android - Google Places API 配额超出

javascript - Open Layers 无法读取未定义的属性 'add Layer'

javascript - 根据另一个不同长度数组中的值对数组进行排序

javascript - Ajax 返回未定义

javascript - 如何将 XML View 传输到 JavaScript View /SAPUI5

javascript - 如何防止浏览器从内存中选择选择框中的选项

安卓.view.InflateException : Binary XML file line #45: Error inflating class fragment

ruby-on-rails - 带有 Rails 3.0 的 Google map API