将默认标记更改为谷歌地图API上的特定图像

Changing the default marker to a specific image on google maps API

本文关键字:API 图像 谷歌地图 默认      更新时间:2023-09-26

这段代码的作用是,当用户输入两个坐标时,它用两个标记和一条显示方向的线来设置地图。是否可以将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>

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

您需要执行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
});