2011-01-25 12 views
2

をプレイしていないが、私はマップがズームチェックとセットが動作しませんTHA問題があるfitBoundsとgetZoomがいい

function showAddress(address) { 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.fitBounds(results[0].geometry.bounds); 

     if (map.getZoom() < 12) { 
      map.setZoom(12); 
     } 
     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    } 

を初期化した後、私はほとんどすぐに実行している次のコードを持っています。私はチェックしてマップが開始されていますが、map.getZoom()関数は現時点では定義されていません。

fitboundsが設定され、ズームレベルがわかってからズームを適切に制御できるようになるまで何かできることはありますか?

+0

同様の質問:http://stackoverflow.com/questions/3506035/google-maps-map-getbounds直後のコール・ツー・マップ・フィットバンド – TMS

答えて

3
// Define the Bounds Changed event handler to do a "once-only" zoom level check 
    google.maps.event.addListener(map, 'bounds_changed', function() { 
    InitialZoomCheck() 
    }); 

geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    map.fitBounds(results[0].geometry.bounds); 




function InitialZoomCheck() { 

    // Re-define the event handler so that this routine is called only once 
    google.maps.event.addListener(map, 'bounds_changed', function() { 
    document.posicion.z.value=map.getZoom() 
    }); 

    if (map.getZoom() > 12) { 
    map.setZoom (12); 
    } 
} 
3

が、私はそれは古い質問ですけど、私はよりエレガントなこの解決策を見つけた:

google.maps.event.addListenerOnce(map, 'bounds_changed', function() { 
    if(map.getZoom() > 16) map.setZoom(16); 
}); 
map.fitBounds(latlngbounds); 
関連する問題