4

クリック ポリライン イベントの 2 つの既存のポイントの間にポリライン上のポイントを追加するにはどうすればよいですか? ありがとうございました!

4

2 に答える 2

6

Polyline2 点だけの について話している場合は、LatLngBoundsを含む の中心を使用できますPolyline。ただし、Google maps api v3 はこの機能を実装していませんPolyline.getBounds()Polylineしたがって、クラスを拡張してgetBounds関数を含めることができます。

google.maps.Polyline.prototype.getBounds = function() {
  var bounds = new google.maps.LatLngBounds();
  this.getPath().forEach(function(e) {
    bounds.extend(e);
  });
  return bounds;
};

Polyline.getBounds()2 点のみの線上には、この線を含む領域が含まれます。この境界の中心は、線の正確な中心でなければなりません。Polylineに 2 つ以上のポイントが含まれる場合、中心はクリックされた線の中心ではなく、すべてのポイントを含む境界の中心になります。この関数で複数セグメントのポリラインを使用する場合、どのセグメントがクリックされたかを計算するために、より多くの計算が必要になります。

2 ポイントを使用した小さな例を次に示しますPolyline

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Right in Two</title>

    <style type="text/css">
      #map-canvas {
        height: 500px;
      }
    </style>

    <script type="text/javascript"
        src="http://www.google.com/jsapi?autoload={'modules':[{name:'maps',version:3,other_params:'sensor=false'}]}"></script>
    <script type="text/javascript">

      function init() {
        var mapDiv = document.getElementById('map-canvas');
        var map = new google.maps.Map(mapDiv, {
          center: new google.maps.LatLng(37.790234970864, -122.39031314844),
          zoom: 5,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var points = [
                  new google.maps.LatLng(40.785533,-124.16748),
                  new google.maps.LatLng(32.700413,-115.469971)
        ];

        var line = new google.maps.Polyline({
          map: map,
          path: points,
          strokeColor: "#FF0000",
          strokeWeight: 2,
          strokeOpacity: 1.0
        });

        google.maps.Polyline.prototype.getBounds = function() {
          var bounds = new google.maps.LatLngBounds();
          this.getPath().forEach(function(e) {
            bounds.extend(e);
          });
          return bounds;
        };

        google.maps.event.addListener(line, 'click', function(e){
          var marker = new google.maps.Marker({
            map: map,
            position: line.getBounds().getCenter()
          });
        });

      };

      google.maps.event.addDomListener(window, 'load', init);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>
于 2010-08-23T15:58:43.447 に答える
0

方位と距離の 50% を計算するだけで済みます - そこに頂点を追加します。

上の例は境界の中心です - これは同一です。

前と次の頂点 latlang によって境界を拡張する一時的な境界オブジェクトが必要になる場合があります。

于 2011-04-17T00:00:26.410 に答える