0

Google マップ アプリケーションで Web ページを開発していますが、少し問題があります。現状では、Web ページには機能マップ (レイヤーなし) と検索バーがあります。私はプログラミングが初めてなので、見逃している簡単な修正があることを願っています。住所を検索すると、地図上に配置された目印があります。ただし、地図は目印にズームインしません。検索した住所ごとに地図を拡大するにはどうすればよいですか?

  <script type="text/javascript">
    var geocoder;
    var map; 
  function initialize() {
    geocoder = new google.maps.Geocoder ();
    var latlng = new google.maps.LatLng (55.1667, -114.4000);
    var myOptions = {
      zoom: 5,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
     map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
        }
    function codeAddress () {
        var address = document.getElementById ("address").value;
        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
            });
            } 
        else {
            alert("Geocode was not successful for the following reason: " + status);
                }
    }); 
                            }
</script>

ありがとうございました

4

1 に答える 1

0

私は2つの関数を呼び出します:

(1)地図をマーカーの中央に配置します

すでにLatLngを持っているか、から取得してmarker.getPosition()、を呼び出しますmap.setCenter(myLatLng)

(2)地図のズームを設定します

map.setZoom(zoomLevel)、12〜16程度に固定することも、マーカーの種類(番地または都市の概要)に応じて変数にすることもできます。この情報は、マーカー変数に格納できますmarker.zoomLevel

function codeAddress () { 
  var address = document.getElementById ("address").value;
  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 });
      map.setZoom(16);
    } 
    else { 
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
于 2012-05-28T19:39:57.377 に答える