SYNCERのロゴ
アイキャッチ画像

LatLngBounds.toSpan()

LatLng.toSpan()はLatLngBoundsクラスのメソッドです。境界の北東(右上)と南西(左下)を結ぶ対角線の距離を取得できます。具体的には、北東の緯度から南西の緯度を引いた差を緯度、北東の経度から南西の経度を引いた差を経度としたLatLngです。

構文

toSpan()

パラメータ

パラメータはありません。

返り値

LatLng

北東の緯度から南西の緯度を引いた差を緯度、北東の経度から南西の経度を引いた差を経度、としたLatLng。

デモ

メソッドを実行すると、北東(右上)から南西(左下)までの距離を、LatLngで取得します。

var latLngBounds = new google.maps.LatLngBounds(
	new google.maps.LatLng( 35.2199, 136.9851 ) ,
	new google.maps.LatLng( 37.7892, 140.4898 )
) ;

// メソッドを実行
latLngBounds.toSpan() ;

サンプルコード

<!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 latLngBounds = new google.maps.LatLngBounds(
		new google.maps.LatLng( 33.35929104,134.93065664 ) ,
		new google.maps.LatLng( 36.95102297,141.68730977 )
	) ;

	// LatLngBoundsを可視化
	new google.maps.Rectangle( { map: map, bounds: latLngBounds, } ) ;

	// fit bounds
	map.fitBounds( latLngBounds ) ;

	// Method
	document.getElementById( "method" ).onclick = function () {
		var response = latLngBounds.toSpan() ;
		new google.maps.Marker( { map: map, position: response, } ) ;
		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>

デモページを開く

  • Twitterでシェア
  • Facebookでシェア
  • Google+でシェア
  • はてなブックマークでシェア
  • pocketに保存
  • LINEでシェア
更新履歴
2015年9月1日 (火)
コンテンツを公開しました。