15

マーカーの「ドラッグ」イベントと同様に、Google マップでポリラインを編集しているときに発生するイベントを探しています。「capturing_changed」イベントを見つけましたが、ドラッグではなく、dragstart と dragend で発生するようです。現状では、マーカーをドラッグできるようにし、マーカーの位置に基づいてポリラインを更新し、基本的に編集可能なポリラインを再発明することで、車輪を再発明する必要があります。編集可能なポリラインがスムーズに行うマーカーを追加できるようにしたいのですが、ドラッグ時に検出できる必要があります。API ドキュメントや Google 検索で何も見つからないので、ここで質問しようと思いました。

4

1 に答える 1

27

変更時にイベントを追加するように変更されたドキュメントの単純な Polyline の例(insert_at、remove_at、set_at、dragond)。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Google Maps JavaScript API v3 Example: Polyline Simple</title>
    <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
var flightPath = null;

      function initialize() {
        var myLatLng = new google.maps.LatLng(0, -180);
        var mapOptions = {
          zoom: 3,
          center: myLatLng,
          mapTypeId: google.maps.MapTypeId.TERRAIN
        };

        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var flightPlanCoordinates = [
            new google.maps.LatLng(37.772323, -122.214897),
            new google.maps.LatLng(21.291982, -157.821856),
            new google.maps.LatLng(-18.142599, 178.431),
            new google.maps.LatLng(-27.46758, 153.027892)
        ];
        flightPath = new google.maps.Polyline({
          path: flightPlanCoordinates,
          strokeColor: '#FF0000',
          strokeOpacity: 1.0,
          strokeWeight: 2,
          editable: true,
          draggable: true
        });
        google.maps.event.addListener(flightPath, "dragend", getPath);
        google.maps.event.addListener(flightPath.getPath(), "insert_at", getPath);
        google.maps.event.addListener(flightPath.getPath(), "remove_at", getPath);
        google.maps.event.addListener(flightPath.getPath(), "set_at", getPath);
        flightPath.setMap(map);
      }

function getPath() {
   var path = flightPath.getPath();
   var len = path.getLength();
   var coordStr = "";
   for (var i=0; i<len; i++) {
     coordStr += path.getAt(i).toUrlValue(6)+"<br>";
   }
   document.getElementById('path').innerHTML = coordStr;
}

    </script>
  </head>
  <body onload="initialize()">
    <div id="map-canvas" style="height:500px; width:600px;"></div>
    <div id="path"></div>
  </body>
</html>

実施例

コードスニペット:

var flightPath = null;

function initialize() {
  var myLatLng = new google.maps.LatLng(0, -180);
  var mapOptions = {
    zoom: 3,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var flightPlanCoordinates = [
    new google.maps.LatLng(37.772323, -122.214897),
    new google.maps.LatLng(21.291982, -157.821856),
    new google.maps.LatLng(-18.142599, 178.431),
    new google.maps.LatLng(-27.46758, 153.027892)
  ];
  flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2,
    editable: true,
    draggable: true
  });
  google.maps.event.addListener(flightPath, "dragend", getPath);
  google.maps.event.addListener(flightPath.getPath(), "insert_at", getPath);
  google.maps.event.addListener(flightPath.getPath(), "remove_at", getPath);
  google.maps.event.addListener(flightPath.getPath(), "set_at", getPath);
  flightPath.setMap(map);
}

function getPath() {
  var path = flightPath.getPath();
  var len = path.getLength();
  var coordStr = "";
  for (var i = 0; i < len; i++) {
    coordStr += path.getAt(i).toUrlValue(6) + "<br>";
  }
  document.getElementById('path').innerHTML = coordStr;
}
google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="path"></div>
<div id="map-canvas" style="height:500px; width:600px;"></div>

于 2013-03-29T04:29:12.910 に答える