【问题标题】:Changing the default marker to a specific image on google maps API将默认标记更改为谷歌地图 API 上的特定图像
【发布时间】:2013-02-21 05:39:29
【问题描述】:

这段代码的作用是,当用户输入两个坐标时,它会在地图上设置一个两个标记和一条线来显示方向强>。是否可以将两个坐标的 d*故障标记* 更改为 特定图像,假设我希望第一个坐标代表“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>

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

【问题讨论】:

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


【解决方案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
});

【讨论】:

  • 通过这个过程,它只显示两个坐标的图像,我希望它显示不同的图像,谢谢
  • 您添加了if statement 吗?
  • 我在哪里添加 if 语句,它只是在变量到标记之前??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 2014-09-26
  • 1970-01-01
  • 2014-10-11
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多