谷歌地图多个自定义标记

7

我正在尝试学习如何制作谷歌地图。我对JavaScript的理解很少,但正在这里努力学习。

我参考了在线代码,并且我已经到达了一定程度,可以理解如何添加位置、标记和信息窗口,但我正在尝试弄清如何为每个标记添加多个自定义图标。

谢谢帮助。

function initialize() {

        //add map, the type of map
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 6,
            center: new google.maps.LatLng(37.7749295, -122.4194155),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        //add locations
        var locations = [
            ['San Francisco: Power Outage', 37.7749295, -122.4194155],
            ['Sausalito', 37.8590937, -122.4852507],
            ['Sacramento', 38.5815719, -121.4943996],
            ['Soledad', 36.424687, -121.3263187],
            ['Shingletown', 40.4923784, -121.8891586]
        ];

        //declare marker call it 'i'
        var marker, i;

        //declare infowindow
        var infowindow = new google.maps.InfoWindow();

        //add marker to each locations
        for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map,
            });

            //click function to marker, pops up infowindow
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][0]);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
    }
    google.maps.event.addDomListener(window, 'load', initialize);
3个回答

15

有多种方法,其中最简单的方法是将标记(marker)的图标属性(icon-property)设置为您想要作为标记出现的图片的URL。

示例:

//modified array with icon-URLs
var locations = [
            ['San Francisco: Power Outage', 37.7749295, -122.4194155,'http://labs.google.com/ridefinder/images/mm_20_purple.png'],
            ['Sausalito', 37.8590937, -122.4852507,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
            ['Sacramento', 38.5815719, -121.4943996,'http://labs.google.com/ridefinder/images/mm_20_green.png'],
            ['Soledad', 36.424687, -121.3263187,'http://labs.google.com/ridefinder/images/mm_20_blue.png'],
            ['Shingletown', 40.4923784, -121.8891586,'http://labs.google.com/ridefinder/images/mm_20_yellow.png']
        ];



//inside the loop
marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map,
                icon: locations[i][3]
            });

0
    <div id="map"></div>

  <script type="text/javascript">          
     function initialize() {
         //add map, the type of map
           var mapOptions = {
               zoom: 5,
               draggable: true,
               animation: google.maps.Animation.DROP,
               mapTypeId: google.maps.MapTypeId.ROADMAP,
               center: new google.maps.LatLng(51.4964302,-0.1331412), // area location
               styles:[{"stylers":[{"saturation":-100},{"gamma":1}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"off"}]},{"featureType":"poi.business","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"poi.business","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"poi.place_of_worship","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"poi.place_of_worship","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"geometry","stylers":[{"visibility":"simplified"}]},{"featureType":"water","stylers":[{"visibility":"on"},{"saturation":50},{"gamma":0},{"hue":"#50a5d1"}]},{"featureType":"administrative.neighborhood","elementType":"labels.text.fill","stylers":[{"color":"#333333"}]},{"featureType":"road.local","elementType":"labels.text","stylers":[{"weight":0.5},{"color":"#333333"}]},{"featureType":"transit.station","elementType":"labels.icon","stylers":[{"gamma":1},{"saturation":50}]}]
           };
           var mapElement = document.getElementById('map');
           var map = new google.maps.Map(mapElement, mapOptions);

         //add locations
             var locations = [
                 ['London office', 51.4964302,-0.1331412,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
                 ['Sausalito', 37.8590937, -122.4852507,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
                 ['Sacramento', 38.5815719, -121.4943996,'http://labs.google.com/ridefinder/images/mm_20_green.png'],
                 ['Soledad', 36.424687, -121.3263187,'http://labs.google.com/ridefinder/images/mm_20_blue.png'],
                 ['Shingletown', 40.4923784, -121.8891586,'http://labs.google.com/ridefinder/images/mm_20_yellow.png']
             ];
             //declare marker call it 'i'
             var marker, i;
             //declare infowindow
             var infowindow = new google.maps.InfoWindow();
             //add marker to each locations
             for (i = 0; i < locations.length; i++) {
                 marker = new google.maps.Marker({
                     position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                     map: map,
                     icon: locations[i][3]
             });
             //click function to marker, pops up infowindow
             google.maps.event.addListener(marker, 'click', (function(marker, i) {
                 return function() {
                     infowindow.setContent(locations[i][0]);
                     infowindow.open(map, marker);
                 }
             })(marker, i));
         }
     }
     google.maps.event.addDomListener(window, 'load', initialize);
  </script>

0
    var locations = [
        ['San Francisco: Power Outage', 37.7749295, -122.4194155],
        ['Sausalito', 37.8590937, -122.4852507],
        ['Sacramento', 38.5815719, -121.4943996],
        ['Soledad', 36.424687, -121.3263187],
        ['Shingletown', 40.4923784, -121.8891586]
    ];

位置表示一个地区数组,后面跟着它们的纬度和经度,它们将被定位在谷歌地图上,不一定总是以这种方式发送纬度和经度,因此可以接收数据库或 JSON。希望我有帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接