如何在谷歌地图上添加自定义图标?

时间:2015-03-10 09:29:41

标签: google-maps

如何在下面提到的代码中添加自定义图标以突出显示lakshadweep海滩。

由于我只有基本的java知识,所以请提供简单的方法,以便我可以编辑并应用于其他项目。

这是我的完整代码..

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>

</head>

<body>
<div id="map" style="width: 100%; height: 400px;"></div>
<script>
var locations = [
  ['lakshadweep Beach', 10.204729,72.836778, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]

];

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 3,
  center: new google.maps.LatLng(-10.90, 100.25),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}
</script>



</body>
</html>

2 个答案:

答案 0 :(得分:0)

请在代码中添加这些行。

var iconBase ='https://maps.google.com/mapfiles/kml/shapes/'; //图片路径

var marker = new google.maps.Marker({
  position: myLatLng,
  map: map,
  icon: iconBase + 'schools_maps.png'
});

这对我有用..

答案 1 :(得分:0)

在代码中更新这些行。

for (i = 0; i < locations.length; i++) { 
if(i==0)
{ 
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    icon: iconBase + 'schools_maps.png'
  });

}
else
{
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });
}