0

我无法使用数组显示自定义标记。我的代码看起来像这样

<html>
  <head>
    <style>
    body { font-family: Helvetica; }
    .map-content h3 { margin: 0; padding: 5px 0 0 0; }
    </style>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA6VosQrKQOjoTbDSp4Kk4BHvofOJFUfZU&sensor=true"></script>
    <script>
    // Set the Map variable
        var map;
        function initialize() { 
            var myOptions = {
            zoom: 9,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var all = [
            ["Indianapolis International Airport", "7800 Col. H. Weir Cook Memorial Drive", "Indianapolis", "IN", "46241", "39.71449", "-86.29842", "airport"], 
            ["Location 2", "7205 Olmstead Dr", "Burlington", "NC", "27215", "36.069974", "-79.548101", "airport",],
            ["Location 3", "W Market St", "Graham", "NC", "27253", "36.0722225", "-79.4016207", "airport"],
            ["Location 4", "Mt Hermon Rock Creek Rd", "Graham", "NC", "27253", "35.9826328", "-79.4165216", "airport"],
            ["Location 5", "415 Spring Garden St", "Greensboro", "NC", "27401", "36.06761", "-79.794984", "airport"]
        ];                          
        var infoWindow = new google.maps.InfoWindow;
        map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
        // Set the center of the map
        var pos = new google.maps.LatLng(36.0621881, -79.5101063);
        map.setCenter(pos);
        function infoCallback(infowindow, marker) { 
            return function() {
            infowindow.open(map, marker);

        };
        
        
   }            
   function setMarkers(map, all) {  
    for (var i in all) {                                        
            var name    = all[i][0];
            var address = all[i][1];
            var city    = all[i][2];
            var state   = all[i][3];
            var zip     = all[i][4];
            var lat     = all[i][5];
            var lng     = all[i][6];
            var image   = all[i][7];
            var latlngset;
            latlngset = new google.maps.LatLng(lat, lng);
            var marker = new google.maps.Marker({  
              map: map,  title: city,  position: latlngset, icon: image,
            });
            var airport = new google.maps.MarkerImage(
            'images/airport.png',
            new google.maps.Size(32,37),
            new google.maps.Point(0,0),
            new google.maps.Point(16,37)
             );
            var content = '<div class="map-content"><h3>' + name + '</h3>' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br /><a href="http://maps.google.com/?daddr=' + address + ' ' + city + ', ' + state + ' ' + zip + '" target="_blank">Get Directions</a></div>';                   
            var infowindow = new google.maps.InfoWindow();
              infowindow.setContent(content);
              google.maps.event.addListener(
                marker, 
                'click', 
                infoCallback(infowindow, marker)
              );
          }
        }           
        // Set all markers in the all variable
        setMarkers(map, all);
      };
      // Initializes the Google Map
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map_canvas" style="height: 500px; width: 800px;"></div>
  </body>
</html>

到目前为止,当我设置 var image= airport 时,它可以工作。当我做 icon:airport 时,它可以工作,但是当我尝试获取图像时,请参考 var image 它不起作用。有人可以帮我吗?谢谢!

4

2 回答 2

1

image不是图标,它是一个字符串(而不是 URL):

var marker = new google.maps.Marker({  
  map: map,  title: city,  position: latlngset, icon: image,
});

airport是一个图标(实际上是一个已弃用的 MarkerImage,但它会起作用),但没有被使用。

var airport = new google.maps.MarkerImage(
  'images/airport.png',
  new google.maps.Size(32,37),
  new google.maps.Point(0,0),
  new google.maps.Point(16,37)
);

像这样的东西应该工作:

var icon = null;
if (image == "airport") icon = airport;
var marker = new google.maps.Marker({  
  map: map,  title: city,  position: latlngset, icon: icon
});

工作示例(使用不同的自定义图标)

于 2013-01-30T23:30:47.320 回答
0

这对我有用: https ://developers.google.com/maps/documentation/javascript/examples/icon-complex

只需将 URL 包装在地图中: var _icon = {url:'/statics/images/red-marker.png'}

然后在标记中: var marker = new google.maps.Marker({ id: args.id, map:CurrentMap, position: new google.maps.LatLng(args.lat, args.lng), icon: _icon }) ;

于 2018-04-10T07:09:00.913 回答