Map.panToBounds()
Map.panToBounds()はMapクラスのメソッドです。指定した境界がビューポートに含まれる位置まで移動します。ズーム値は変更されないため、境界がビューポートよりも大きくて収まりきらないの場合もあります。
構文
パラメータ
latLngBounds
LatLngBounds | LatLngBoundsLiteral
移動先となる境界をLatLngBounds、またはLatLngBoundsLiteralで指定する。
返り値
返り値はありません。
デモ
引数には、富士急ハイランドあたりの境界を指定しています。実行すると、それが収まる位置まで移動します。分かりやすいように、境界には色を付けてあります。
var map = new google.maps.Map( mapDiv, {
center: new google.maps.LatLng( 35.45216977054224, 138.7300826551757 ) ,
zoom: 17 ,
} ) ;
// メソッドを実行
var latLngBounds = new google.maps.LatLngBounds(
new google.maps.LatLng( 35.485901, 138.779526 ) ,
new google.maps.LatLng( 35.486801, 138.78064 )
) ;
map.panToBounds( latLngBounds ) ;
サンプルコード
<!DOCTYPE html>
<html>
<head>
<style>
#map-canvas {
width: 600px ;
height: 600px ;
}
</style>
</head>
<body>
<div id="map-canvas"></div>
<p><button id="method">メソッドを実行</button><button id="reset">リセット</button></p>
<p><textarea id="response"></textarea></p>
<script src="//maps.googleapis.com/maps/api/js?key={APIキー}"></script>
<script>
function initialize() {
var mapDiv = document.getElementById( "map-canvas" ) ;
var responseTextarea = document.getElementById( "response" ) ;
responseTextarea.value = "" ;
// Map
var map = new google.maps.Map( mapDiv, {
center: new google.maps.LatLng( 43.0686601740126, 141.35075529999995 ) ,
zoom: 15 ,
} ) ;
// LatLngBounds
var latLngBounds = new google.maps.LatLngBounds(
new google.maps.LatLng( 35.485901, 138.779526 ) ,
new google.maps.LatLng( 35.486801, 138.78064 )
) ;
new google.maps.Rectangle( { map: map, bounds: latLngBounds, } ) ;
// Method
document.getElementById( "method" ).onclick = function () {
var response = map.panToBounds( latLngBounds ) ;
try{ response = typeof response == "object" ? JSON.stringify( response ) : response ; }catch(e){}
responseTextarea.value = response ;
console.log( response ) ;
}
}
// Reset
document.getElementById( "reset" ).onclick = initialize ;
initialize() ;
</script>
</body>
</html>