0

複数パスを持つポリゴンの変形イベントが取得できません。

// ポリゴンの作成
var path1 = [ // 外側ポリゴン
    new google.maps.LatLng(36.66841891894785,138.636474609375),
    new google.maps.LatLng(36.90597988519295,139.3780517578125),
    new google.maps.LatLng(36.756490329505176,139.7076416015625),
    new google.maps.LatLng(36.38149043210595,140.0701904296875),
    new google.maps.LatLng(35.92019610057511,140.020751953125),
    new google.maps.LatLng(35.60371874069731,139.59228515625),
];
var path2 = [ // 内側ポリゴン
    new google.maps.LatLng(36.421282443649496,139.19677734375),
    new google.maps.LatLng(36.06686213257888,138.8671875),
    new google.maps.LatLng(35.817813158696616,139.1748046875),
    new google.maps.LatLng(35.71083783530009,139.98779296875)
];
var polygonOptions = {
    paths: [path1, path2],
    geodesic: true,
    editable: true,
    clickable:true
};
var polygon = new google.maps.Polygon(polygonOptions);

// 変形イベント取得
var polygonPaths = polygon.getPaths();
for(var i=0;i<=polygonPaths.length-1;i++){
    google.maps.event.addListener(value, 'set_at', function(){
           console.log("aa");
      });
}

polygon.getPath()で取得したパスにイベント付与をすると
外側ポリゴンの変形イベントはキャッチできました。

1 件の回答 1

1

ご提示のコードにはvalueの定義がありませんが、おそらくこのvalueを取得する部分で躓いているのでしょう。

Polygon.prototype.getPaths()の戻り値はMVCArrayオブジェクトです。
単なる配列ではないのでpolygonPaths[i]のように添字でアクセスすることはできません。
for文内でMVCArray.prototype.getAt()メソッドを呼ぶか、MVCArray.prototype.forEach()メソッドを利用してください。

var polygonPaths = polygon.getPaths();
for(var i = 0; i < polygonPaths.length; i++){
  var polygonPath = polygonPaths.getAt(i);
  google.maps.event.addListener(polygonPath, 'set_at', function(){
    console.log("aa");
  });

  /* 下記のようにも書けます。
  polygonPath.addListener('set_at', function(){
    console.log("aa");
  });
  */
}

またはこっち

var polygonPaths = polygon.getPaths();
polygonPaths.forEach(function(polygonPath) {
  google.maps.event.addListener(polygonPath, 'set_at', function(){
    console.log("aa");
  });
});

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。