LatLngBounds.toString()
LatLng.toString()はLatLngBoundsクラスのメソッドです。境界の位置座標を文字列で取得します。文字列は南西の緯度、経度、北東の緯度、経度の順で両端は括弧で括られています。括弧がいらない場合はtoUrlValue()を利用して下さい。
構文
toString()
パラメータ
パラメータはありません。
返り値
string
境界の北東(右上)と南西(左下)を文字列に変換した値。
デモ
メソッドを実行すると、境界の北東と南西の位置座標を文字列に変換します。
var latLngBounds = new google.maps.LatLngBounds(
new google.maps.LatLng( 35.2199, 136.9851 ) ,
new google.maps.LatLng( 37.7892, 140.4898 )
) ;
// メソッドを実行
latLngBounds.toString() ;
サンプルコード
<!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.toString() ;
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>