LatLngBounds.union()
LatLng.union()はLatLngBoundsクラスのメソッドです。自身の境界に、指定した境界を取り込んで合成します。
構文
パラメータ
other
LatLngBounds | LatLngBoundsLiteral
合成する境界をLatLngBoundsクラス、またはLatLngBoundsLiteralオブジェクトで指定する。
返り値
LatLngBounds
2つの境界を合成した後の、LatLngBounds。
デモ
メソッドを実行すると、赤色を付けた境界に、青色を付けた境界を合成します。赤色の境界が、青色の境界を含んで拡張されるのを確認しましょう。
var latLngBounds1 = new google.maps.LatLngBounds(
new google.maps.LatLng( 35.09416700932337, 138.71003097070297 ) ,
new google.maps.LatLng( 37.66761113237895, 142.21466964257797 )
) ;
var latLngBounds2 = new google.maps.LatLngBounds(
new google.maps.LatLng( 34.534942437137694, 135.28229659570297 ) ,
new google.maps.LatLng( 37.12647676976514, 138.78693526757797 )
) ;
// メソッドを実行
latLngBounds1.union( latLngBounds2 ) ;
サンプルコード
<!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( 35, 139 ) ,
zoom: 15 ,
} ) ;
// LatLngBounds
var latLngBounds1 = new google.maps.LatLngBounds(
new google.maps.LatLng( 33.35929104,134.93065664 ) ,
new google.maps.LatLng( 36.95102297,141.68730977 )
) ;
var latLngBounds2 = new google.maps.LatLngBounds(
new google.maps.LatLng( 35.2199, 136.9851 ) ,
new google.maps.LatLng( 39.25036699,141.31926778 )
) ;
// LatLngBoundsを可視化
var rectangle1 = new google.maps.Rectangle( { map: map, bounds: latLngBounds1, fillColor: "red", } ) ;
new google.maps.Rectangle( { map: map, bounds: latLngBounds2, fillColor: "blue", } ) ;
// fit bounds
var latLngBounds3 = new google.maps.LatLngBounds() ;
latLngBounds3.union( latLngBounds1 ) ;
latLngBounds3.union( latLngBounds2 ) ;
map.fitBounds( latLngBounds3 ) ;
// Method
document.getElementById( "method" ).onclick = function () {
var response = latLngBounds1.union( latLngBounds2 ) ;
rectangle1.setBounds( latLngBounds1 ) ;
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>