Polygon.getPath()
Polygon.getPath()はPolygonクラスのメソッドです。ポリゴンの一次元配列のパスを取得します。二次元配列のパスを指定したポリゴンに実行した場合、取得できるのは1つ目のポリゴンのパスとなります。一次元配列にも二次元配列にも対応した処理を行ないたい場合、getPaths()の方を利用するのをオススメします。
構文
getPath()
パラメータ
パラメータはありません。
返り値
MVCArray<LatLng>
パスを含んだMVCArrayクラスの配列。二次元配列が設定してある場合は、1つ目のポリゴンのパス。
デモ
メソッドを実行すると、ポリゴンの一次元配列のパスを取得します。
var polygon = new google.maps.Polygon( {
map: map ,
paths: [
new google.maps.LatLng( 35.7113 , 139.8099 ) ,
new google.maps.LatLng( 35.7106 , 139.8088 ) ,
new google.maps.LatLng( 35.7091 , 139.8088 ) ,
new google.maps.LatLng( 35.7088 , 139.8107 ) ,
new google.maps.LatLng( 35.7100 , 139.8120 ) ,
new google.maps.LatLng( 35.7093 , 139.8135 ) ,
new google.maps.LatLng( 35.7116 , 139.8128 ) ,
new google.maps.LatLng( 35.7109 , 139.8120 ) ,
] ,
} ) ;
// メソッドを実行
polygon.getPath() ;
サンプルコード
<!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.7113 , 139.8099 ) ,
new google.maps.LatLng( 35.7106 , 139.8088 ) ,
new google.maps.LatLng( 35.7091 , 139.8088 ) ,
new google.maps.LatLng( 35.7088 , 139.8107 ) ,
new google.maps.LatLng( 35.7100 , 139.8120 ) ,
new google.maps.LatLng( 35.7093 , 139.8135 ) ,
new google.maps.LatLng( 35.7116 , 139.8128 ) ,
new google.maps.LatLng( 35.7109 , 139.8120 ) ,
] ,
} ) ;
// 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.getPath() ;
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>