谷歌地图api v3:在点击折线事件上的两个现有点之间的多段线上添加点 [英] google maps api v3: add point on polyline between two existing points on click polyline event

查看:91
本文介绍了谷歌地图api v3:在点击折线事件上的两个现有点之间的多段线上添加点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在点击折线事件上的两个现有点之间的多段线上添加点?
谢谢!

How can I add point on a polyline between two existing points on a click polyline event? Thank you!

推荐答案

如果您只是在讨论 Polyline 只有2分,您可以使用的中心 LatLngBounds 包含 Polyline 。不过,Google maps api v3并未实现 Polyline.getBounds()函数。因此,您可以扩展 Polyline 类以包含 getBounds 函数:

If you are just talking about a Polyline with only 2 points, you could use the center of a LatLngBounds containing the Polyline. Google maps api v3 doesn't implement the Polyline.getBounds() function, though. So you can extend the Polyline class to include a getBounds function:

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点以上,则中心不会落在所点击的线的中心,而是包含所有点的边界的中心。如果你想在这个函数中使用多段多段线,需要更多的数学计算哪个段被点击。

Polyline.getBounds() on a line with only 2 points will contain the area to include this line. The center of this bounds should be the exact center of your line. If the Polyline includes more than 2 points, the center will not fall on the center of the line clicked, but the center of the bounds that includes all points. If you want to use mutli-segment Polylines with this function, it will take more math to calculate which segment was clicked.

这是一个使用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>

这篇关于谷歌地图api v3:在点击折线事件上的两个现有点之间的多段线上添加点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆