0

5 グループのマーカーでマップを作成しています。すべての座標が配置され、マーカーがマップに表示されますが、各グループの画像を変更する方法がわかりません。私は他の例を見てきましたが、うまくいきませんでした。座標のグループまたは個々の座標のいずれかの画像を変更する方法があれば、助けていただければ幸いです。

これが私がこれまでに持っているものです:

<script type="text/javascript">  function initialize() {

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

    //add locations
    var cottages = [
        ['Savannah Cottage<br />412 S.Pearl Street', 31.55600224874313, -91.4073497056961] ];

    var restaurants = [
    ['Cotton Alley Cafe<br />208 Main Street<br />(601)442-7452<br /><a href="http://www.cottonalleycafe.com" target= "_blank">Website</a>', 31.561075,-91.40503100000001]
    ];

    var bars = [
    ['Under-the-Hill Saloon', 31.559589, -91.41074700000001]
    ];

    var tours = [
    ['Auburn Antebullum Home<br />400 Duncan Avenue<br />(601)442-5981', 31.5457833, -91.39274319999998]
    ];

    var spas = [
    ['Anruss &amp; Co Salon and Spa<br />212 North Commerce Street<br />(601) 445-2007<br /><a href="https://www.facebook.com/anruss.salon" target= "_blank">Website</a>', 31.561061,-91.40116799999998]
       ];


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

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

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

        //click function to marker, pops up infowindow
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(cottages[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

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

        //click function to marker, pops up infowindow
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(restaurants[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

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

        //click function to marker, pops up infowindow
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(bars[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

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

        //click function to marker, pops up infowindow
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(tours[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

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

        //click function to marker, pops up infowindow
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(spas[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }


}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
4

1 に答える 1

0

あなたが何を考えているのか正確にはわかりませんが、役立つコードがいくつかあります:

function initialize() {
    //add map, the type of map
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 13,
        center: new google.maps.LatLng(31.5603, -91.4031),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

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

    //add locations (as in the question)
    var cottages = [...];
    var restaurants = [...];
    var bars = [...];
    var tours = [...];
    var spas = [...];

    //Make an array that lists all the locations arrays.
    var allLocations = [cottages, restaurants, bars, tours, spas];

    //Define a generalized click handler for all markers.
    //ie. one handler, not one per marker.
    function clickMarker() {
        var data = this.data;
        infowindow.setContent(data.category[data.index][0]);
        infowindow.open(map, this);
        //HERE, you can loop through `data.category.markers` and 
        //do whatever is necessary to each marker in the category
        //eg change their icons.
        for(var i=0; i<data.category.markers.length; i++) {
            var url = ...;//expression that determines which marker icon to use
            data.category.markers[i].setIcon(url);
        }
    }

    //Now loop through all the markers arrays and add markers to the map
    for (var i=0; i<allLocations.length; i++) {
        var arr = allLocations[i];

        //Create an associated array in which to store references to category's markers
        arr.markers = [];

        for (var marker, j=0; j<arr.length; j++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(arr[j][1], arr[j][2]),
                map: map
            });

            arr.markers[j] = marker;

            //This allows the click hander to be generalized,
            //and for each marker to have a reference back
            //to its category array, and its own index.
            marker.data = {
                category: arr,
                index: j
            };
            google.maps.event.addListener(marker, 'click', clickMarker);
        }
    }
}
google.maps.event.addDomListener(window, 'load', initialize);

コード内のコメントを参照してください。

于 2013-07-09T04:27:16.180 に答える