在Google Maps API上将默认标记更改为特定图像

时间:2013-03-06 12:20:08

标签: php javascript html google-maps google-maps-api-3

此代码的作用是,当用户输入两个坐标时,会使用两个标记设置地图,并使用一行显示方向。是否可以将d * efault标记 *更改为特定图像对于两个cordinate都不同,假设我想让第一个cordinate代表“ image1.jpeg “和第二个代表” image2.jpeg “。

<html>
  <head>
    <meta charset="utf-8">
    <title>Google Maps JavaScript API v3 Example: Marker Animations</title>
    <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
    //  var center = new google.maps.LatLng(52.12, -3.2);                   //center of the map
    var center = new google.maps.LatLng(<?php echo $_Mylat?>, <?php echo $_Mylon?>);
      var a = [
         new google.maps.LatLng(<?php echo $_Mylat?>, <?php echo $_Mylon?>),
         new google.maps.LatLng(<?php echo $_Mylat2?>, <?php echo $_Mylon2?>)
        ];
      var marker;
      var map;

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

        map = new google.maps.Map(document.getElementById('map_canvas'),
                mapOptions);

    for (var i=0; i<a.length;i++)
    {
        marker = new google.maps.Marker({
          map:map,
          draggable:true,
          animation: google.maps.Animation.DROP,
          position: a[i]
      });

    }   
     var flightPlanCoordinates = [
            new google.maps.LatLng(<?php echo $_Mylat?>, <?php echo $_Mylon?>),
            new google.maps.LatLng(<?php echo $_Mylat2?>, <?php echo $_Mylon2?>)
        ];

       var flightPath = new google.maps.Polyline({
          path: flightPlanCoordinates,
          strokeColor: '#FF0000',
          strokeOpacity: 1.0,
          strokeWeight: 2
        });

        flightPath.setMap(map);

      }


    </script>

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

在此代码中我可以将标记更改为我的特定图像,谢谢

1 个答案:

答案 0 :(得分:1)

您需要执行IF语句来确定标记应加载的图像。

if(i == 0) iconImage = "image1.jpeg";
else if(i == 1)  iconImage = "image2.jpeg";

然后只需将自定义图标和变量添加到标记:

marker = new google.maps.Marker({
          map:map,
          draggable:true,
          animation: google.maps.Animation.DROP,
          position: a[i],
          icon: iconImage
});
相关问题