1

以下のコードは、ページに 4 つのマップを配置し、住所に基づいてそれぞれにマーカーを追加します。しかし、マーカーを中心にマッピングする必要があります。

<script type="text/javascript">
         var geocoder;
         var map0,map1,map2,map3;
         var markers = [];
          function initialize() {
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var mapOptions = {
              zoom: 14,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map0 = new google.maps.Map(document.getElementById("map_canvas1"), mapOptions);
            addPostCode('@address1',map0);
            map1 = new google.maps.Map(document.getElementById("map_canvas2"), mapOptions);
            addPostCode('@address2',map1);
            map2 = new google.maps.Map(document.getElementById("map_canvas3"), mapOptions);
            addPostCode('@address3',map2);
            map3 = new google.maps.Map(document.getElementById("map_canvas4"), mapOptions);
            addPostCode('@address4',map3);
          }

          function addPostCode(address,map) {
                geocoder.geocode( { 'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    center: results[0].geometry.location,
                    name: address
                });
                markers.push(marker);
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
                });
            }

そのマップのマーカーのどこを中心にすればよいか完全にはわかりません。

4

1 に答える 1

1

さて、私はあなたがここから見ることができる実用的な例をプログラムしました: JS fiddle

私が変更したことcenter: results[0].geometry.locationは、マーカーから削除することでした (マーカーは を使用して制御される場所とマップにのみ追加されるためmap.setCenter();) また、クラスの名前を少し変更したので、好きなように変更してください。乾杯!

于 2013-03-14T16:51:02.763 に答える