Javascript Google Maps APIv3-如何用不同的png图像覆盖多个标记

Javascript Google Maps APIv3-如何用不同的png图像覆盖多个标记,javascript,api,google-maps,Javascript,Api,Google Maps,我目前正在做一个项目,它涉及到在地图上绘制许多自定义标记,然后在单击时创建一个弹出页面。我遇到的问题是,作为javascript的新手,地图上会显示第二个标记图像。到目前为止,我有以下代码,正如你所看到的,“相机”图标可以工作。。。但需要“加德士”也能工作…: <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <script type=

我目前正在做一个项目,它涉及到在地图上绘制许多自定义标记,然后在单击时创建一个弹出页面。我遇到的问题是,作为javascript的新手,地图上会显示第二个标记图像。到目前为止,我有以下代码,正如你所看到的,“相机”图标可以工作。。。但需要“加德士”也能工作…:

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(-43.54654110763488, 172.6156002235225);
    var myOptions = {
      zoom: 11,
      center: latlng,
      navigationControl: false,
      scaleable: false,
      draggable:false,


      navigationControlOptions: {style: google.maps.NavigationControlStyle.ANDROID,position: google.maps.ControlPosition.TOP_LEFT},
      mapTypeId: google.maps.MapTypeId.ROADMAP


    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


  setMarkers(map, webcams, caltex);
  }


  /**
 * Data for the markers consisting of a name, a LatLng and a zIndex for
 * the order in which these markers should display on top of each
 * other.
 */
var webcams = [
  ['Anzac Dr ', -43.51129867148775, 172.7162349879427, 9], 
  ['QEII', -43.48499827165804, 172.6179431987025, 8], 
  ['Yaldhurst Russley', -43.52069820902269, 172.5344992587786, 7],
  ['Waimakariri Bridge', -43.47192540674842, 172.5693997383076, 6],
  ['Dyers Linwood', -43.54654110763488, 172.6956002235225, 4],
  ['Main North W', -43.4513336384715, 172.6278547889116, 5],
  ['Main North N', -43.4509480091774, 172.6386309983553, 3],
  ['Memorial Ave', -43.49318192393784, 172.5499633557023, 2],
  ['The Hub', -43.54341394837794, 172.5256827185488, 1]

];


/** Markers for caltex stations **/

var caltex = [
  ['Caltex VIC', -41.01785817829983, 174.91504669189453, 11], //Corner High & Brunswick Streets
  ['Caltex Porirua', -41.01785817829983, 174.91504669189453, 10],  //Parumoana Street
  ['Caltex Railway Avenue', -41.01785817829983, 174.91504669189453, 9], //20 Railway Avenue
  ['Olympic Service Station', -41.23076651060946, 174.8100757598877, 8], //Corner Holland Crescent & Vogel Street
  ['Caltex Rimutaka', -41.2346071181492, 174.80685710906982, 7], //1193 Fergusson Drive North
  ['Caltex Wellington Airport', -41.243384802383986, 174.81380939483643, 6], //Corner Calabar Road & Broadway
  ['Caltex Wainuiomata', -41.29338219297847, 174.78076457977295, 4], //14-16 The Strand
  ['Caltex Upper Hutt', -41.301958541159564, 174.7844123840332, 5], //749-755 Fergusson Drive
  ['Caltex Stokes Valley', -41.286771831897774, 174.77312564849854, 3], //7 Stokes Valley Road, Stokes Valley
  ['Caltex Newtown', -41.285127199004215, 174.7728681564331, 2], //224 Riddiford Street
  ['Caltex Basin Reserve', -41.30211973991373, 174.7792625427246, 1] //28 Adelaide Road

];


function setMarkers(map, locations) {
  // Add markers to the map

  // Marker sizes are expressed as a Size of X,Y
  // where the origin of the image (0,0) is located
  // in the top left of the image.

  // Origins, anchor positions and coordinates of the marker
  // increase in the X direction to the right and in
  // the Y direction down.
  var image = new google.maps.MarkerImage('camera.png',
      // This marker is 32 pixels wide by 32 pixels tall.
      new google.maps.Size(32, 32),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(0, 0));


  var shadow = new google.maps.MarkerImage('camera_shadow.png',
      // The shadow image is larger in the horizontal dimension
      // while the position and offset are the same as for the main image.
      new google.maps.Size(37, 32),
      new google.maps.Point(0,0),
      new google.maps.Point(0, 32));
      // Shapes define the clickable region of the icon.
      // The type defines an HTML <area> element 'poly' which
      // traces out a polygon as a series of X,Y points. The final
      // coordinate closes the poly by connecting to the first
      // coordinate.
  var shape = {
      coord: [1, 1, 1, 20, 18, 20, 18 , 1],
      type: 'poly'
  };



  for (var i = 0; i < locations.length; i++) {
    var cameras = locations[i];
    var myLatLng = new google.maps.LatLng(cameras[1], cameras[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        shadow: shadow,
        icon: image,
        shape: shape,
        title: cameras[0],
        zIndex: cameras[3]
    });
  }



    for (var i = 0; i < locations.length; i++) {
    var caltex = locations[i];
    var myLatLng = new google.maps.LatLng(caltex[1], caltex[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        shadow: shadow,
        icon: image,
        shape: shape,
        title: caltex[0],
        zIndex: caltex[3]
    });
  }
}

      var marker = newGMarker(LatLng,cameras)

</script>
</head>
<body onload="initialize()">
  <div id="map_canvas" style="width:450px; height:500px"></div>
</body>
</html>

函数初始化(){
var-latlng=new google.maps.latlng(-43.54654110763488172.6156002325);
变量myOptions={
缩放:11,
中心:拉特林,
导航控制:错误,
可缩放:错误,
可拖动:错误,
导航控制选项:{style:google.maps.NavigationControlStyle.ANDROID,位置:google.maps.ControlPosition.TOP_LEFT},
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
设置标记(地图、网络摄像头、加德士);
}
/**
*标记的数据由名称、板条和zIndex组成
*这些标记在每个标记顶部的显示顺序
*其他的。
*/
var网络摄像头=[
[Anzac Dr',-43.51129867148775172.7162349879427,9],
[QEII',-43.48499827165804172.6179431987025,8],
[Yaldhurst Russley',-43.52069820902172.5344992587786,7],
['Waimakariri桥',-43.47192540674842,172.5693997383076,6],
[Dyers Linwood',-43.54654110763488172.6956002235225,4],
[Main North W',-43.4513336384715,172.6278547889116,5],
[Main North N',-43.4509480091774172.6386309983553,3],
[纪念大道-43.49318192393784172.5499633557023,2],
[‘枢纽’,-43.54341394837794172.5256827185488,1]
];
/**加德士站的标志**/
var caltex=[
['Caltex VIC',-41.01785817829983,174.91504669189453,11],//拐角高街和布伦瑞克街
['Caltex Porirua',-41.01785817829983,174.91504669189453,10],//Parumoana街
['Caltex铁路大道',-41.01785817829983,174.91504669189453,9],//20铁路大道
[Olympic Service Station',-41.23076651060946,174.8100757598877,8],//荷兰新月街和沃格尔街拐角处
['Caltex Rimutaka',-41.2346071181492,174.80685710906982,7],//1193 Ferguson大道北
['Caltex-Wellington机场',-41.24338480283986,174.81380939483643,6],//卡拉巴路和百老汇拐角处
['Caltex Wainuiomata',-41.29338219297847174.7807645797295,4],//14-16股
['Caltex Upper Hutt',-41.3019585411595641174.7844123840332,5],//749-755 Ferguson Drive
['Caltex Stokes Valley',-41.286771831897774174.77312564849854,3],//7 Stokes Valley路,Stokes Valley
['Caltex Newtown',-41.285127199004215,174.7728681564331,2],//Riddiford街224号
['Caltex盆地保护区',-41.30211973991373,174.7792625427246,1]//阿德莱德路28号
];
功能设置标记(地图、位置){
//向地图添加标记
//标记大小表示为X,Y的大小
//图像(0,0)的原点所在的位置
//在图像的左上角。
//标记的原点、锚定位置和坐标
//沿X方向向右和向内增加
//Y方向向下。
var image=new google.maps.MarkerImage('camera.png',
//此标记宽32像素,高32像素。
新google.maps.Size(32,32),
//此图像的原点为0,0。
新google.maps.Point(0,0),
//此图像的锚定位于旗杆底部0,32处。
新的google.maps.Point(0,0));
var shadow=new google.maps.MarkerImage('camera\u shadow.png',
//阴影图像在水平维度上更大
//而位置和偏移量与主图像相同。
新谷歌地图大小(37,32),
新google.maps.Point(0,0),
新的google.maps.Point(0,32));
//形状定义图标的可单击区域。
//该类型定义了一个HTML元素“poly”,该元素
//将多边形绘制为一系列X、Y点。最终
//坐标通过连接到第一个多边形来关闭多边形
//协调。
变量形状={
坐标:[1,1,1,20,18,20,18,1],
类型:“poly”
};
对于(变量i=0;i
看起来您将标记逻辑封装到了setMarkers()中,这样就可以为每种类型的标记调用一次。然而,你只能叫它一次。你需要

setMarkers(map, webcams);
setMarkers(map, caltex);