Polygon.getPaths()
Polygon.getPaths()はPolygonクラスのメソッドです。ポリゴンの二次元配列のパスを取得します。一次元配列が設定してあっても、それを二次元配列に変換して取得できます。
構文
getPaths()
パラメータ
パラメータはありません。
返り値
MVCArray<MVCArray<LatLng>>
パスを含んだMVCArrayクラスの配列。一次元配列が設定してある場合は二次元配列に変換して取得する。
デモ
メソッドを実行すると、ポリゴンの二次元配列のパスを取得します。
var polygon = new google.maps.Polygon( {
map: map ,
paths: [
[
new google.maps.LatLng( 35.71130001178331, 139.80993825263977 ) ,
new google.maps.LatLng( 35.71065535994499, 139.8088117248535 ) ,
new google.maps.LatLng( 35.709174383271474, 139.8088439113617 ) ,
new google.maps.LatLng( 35.70886947289342, 139.8107751018524 ) ,
new google.maps.LatLng( 35.710097819014884, 139.81207329101562 ) ,
new google.maps.LatLng( 35.70939217568423, 139.81357532806396 ) ,
new google.maps.LatLng( 35.711639758625964, 139.8128994113922 ) ,
new google.maps.LatLng( 35.71099510953576, 139.81208401985168 ) ,
] ,
[
new google.maps.LatLng( 35.7147651, 139.79665529999997 ) ,
new google.maps.LatLng( 35.71525291995882, 139.7858406332519 ) ,
new google.maps.LatLng( 35.72277890657972, 139.79227793488764 ) ,
] ,
] ,
} ) ;
// メソッドを実行
polygon.getPaths() ;
サンプルコード
<!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.0611, 141.3564 ) ,
zoom: 7 ,
} ) ;
// Polygon
var polygon = new google.maps.Polygon( {
map: map ,
paths: [
[
new google.maps.LatLng( 35.71130001178331, 139.80993825263977 ) ,
new google.maps.LatLng( 35.71065535994499, 139.8088117248535 ) ,
new google.maps.LatLng( 35.709174383271474, 139.8088439113617 ) ,
new google.maps.LatLng( 35.70886947289342, 139.8107751018524 ) ,
new google.maps.LatLng( 35.710097819014884, 139.81207329101562 ) ,
new google.maps.LatLng( 35.70939217568423, 139.81357532806396 ) ,
new google.maps.LatLng( 35.711639758625964, 139.8128994113922 ) ,
new google.maps.LatLng( 35.71099510953576, 139.81208401985168 ) ,
] ,
[
new google.maps.LatLng( 35.7147651, 139.79665529999997 ) ,
new google.maps.LatLng( 35.71525291995882, 139.7858406332519 ) ,
new google.maps.LatLng( 35.72277890657972, 139.79227793488764 ) ,
] ,
] ,
} ) ;
// fit bounds
var latLngBounds = new google.maps.LatLngBounds() ;
polygon.getPaths().forEach( function ( latLngs ) {
latLngs.forEach( function ( latLng ) {
latLngBounds.extend( latLng ) ;
} ) ;
} ) ;
map.fitBounds( latLngBounds ) ;
// Method
document.getElementById( "method" ).onclick = function () {
var response = polygon.getPaths() ;
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>