-1

ここで同様の質問をたくさん見てきましたが、私の質問に答えるものは見つかりませんでした。私は JS と GoogleMaps API v3 が初めてで、チュートリアルに従ってここまでたどり着きました。ただし、クリックされたマーカーに基づいてカスタム コンテンツを含む情報ウィンドウを表示したいのですが、これを行う方法がわかりません。また、約100個のマーカーでこれを可能にしたいので、面倒にならずにこれを行うための最良の方法を知りたい. 明確にするために、アイコンには 3 種類ありますが、最終的には各アイコンに関連付けられた多くのマーカーが存在するため、各「機能」にリンクされたコンテンツが必要になります。うまくいけば、ここで良いスタートを切って、ベースから外れていないことを願っています. ページのコードを含めました。

<!DOCTYPE html>
<html>
  <head>
    <style>
      #map_canvas {
        width: 800px;
        height: 500px;
        background-color:#CCC;
      }
       #legend {
        font-family: Arial, sans-serif;
        background: #fff;
        padding: 10px;
        margin: 10px;
        border: 3px solid #000;
      }
      #legend img {
        vertical-align: middle;
      }
    </style>
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script>
      function initialize() {
        var map_canvas = document.getElementById('map_canvas');
        var map_options = {
          center: new google.maps.LatLng(33.137551,-0.703125),
          zoom: 2,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(map_canvas, map_options);
        map.set('styles', [
        {
            featureType: 'road',
            elementType: 'geometry',
            stylers: [
            { color: '#888888' },
            { weight: 1.0 }
            ]
        }, {
            featureType: 'landscape',
            elementType: 'geometry.fill',
            stylers: [
            { hue: '#008f11' },
            { gamma: 1.0 },
            { saturation: 0 },
            { lightness: -10 }
            ]       
        }, {
            featureType: 'water',
            elementType: 'geometry.fill',
            stylers: [
            { hue: '#054d8fd' },
            { gamma: 1.0 },
            { saturation: 0 },
            { lightness: -10 }
            ]       
        }, {
            featureType: 'poi',
            elementType: 'geometry',
            stylers: [
                { visibility: 'off' }
            ]
            }
        ]);
        var iconBase = 'http://i1318.photobucket.com/albums/t658/greatergoodorg/';
        var icons = {
            people: {
                name: 'People',
                icon: iconBase + 'people_zps26d13009.png',
                shadow: iconBase + 'shadow-people_zps4b39eced.png'
            },
            pets: {
                name: 'Pets',
                icon: iconBase + 'pets_zps15f549f2.png',
                shadow: iconBase + 'shadow-pets_zps361068aa.png'
            },
            planet: {
                name: 'Planet',
                icon: iconBase + 'planet_zps2a8572ce.png',
                shadow: iconBase + 'shadow-planet_zps9912e26b.png',
            }
        };
        var data = ["This is the first one", "This is the second one", "This is the third one"];
        function addMarker(feature) {
            var marker = new google.maps.Marker({
                position: feature.position,
                icon: icons[feature.type].icon,  
                shadow: {
                    url: icons[feature.type].shadow,
                    anchor: new google.maps.Point(21, 32)
                },
                animation: google.maps.Animation.DROP,
                map: map
            });
            /*...
            google.maps.event.addListener(marker, "click", function () {
                 infowindow.open(map,marker);
            });
            ...*/
        /*...
        google.maps.event.addListener(marker, 'click', function() {
        map.setZoom(8);
        map.setCenter(marker.getPosition());
        });
        ...*/
        }
        var features = [
          {
            position: new google.maps.LatLng(33.137551,-0.703125),
            type: 'planet'
          },
          {
            position: new google.maps.LatLng(44.234234,-0.232233),
            type: 'pets'
          },
          {
            position: new google.maps.LatLng(54.234234,-0.032233),
            type: 'people'
          }
          ];
        for (var i = 0, feature; feature = features[i]; i++) {
          addMarker(feature);
          } 
        var legend = document.getElementById('legend');
        for (var key in icons) {
          var type = icons[key];
          var name = type.name;
          var icon = type.icon;
          var div = document.createElement('div');
          div.innerHTML = '<img src="' + icon + '"> ' + name;
          legend.appendChild(div);
        }
        map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(
        document.getElementById('legend'));
    }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>        
  </head>
  <body> 
    <div id="map_canvas"></div>
    <div id="legend"><strong>Project Types</strong></div>
  </body>
</html>
4

1 に答える 1

0

これにより、情報ウィンドウが開き、「タイプ」文字列が表示されます

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

function addMarker(feature) {
    var marker = new google.maps.Marker({
        position: feature.position,
        icon: icons[feature.type].icon,  
        shadow: {
           url: icons[feature.type].shadow,
           anchor: new google.maps.Point(21, 32)
        },
        animation: google.maps.Animation.DROP,
        map: map
    });
    google.maps.event.addListener(marker, "click", function () {
     infowindow.setContent(feature.type);
         infowindow.open(map,marker);
    });
}
于 2013-06-12T02:56:30.370 に答える