Dro*_*kkl 4 polyline google-maps-api-3
如何在点击折线事件的两个现有点之间的折线上添加点?谢谢!
如果你只是谈论一个Polyline只有2分,你可以使用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;
};
Run Code Online (Sandbox Code Playgroud)
Polyline.getBounds()在仅有2个点的线上将包含包含此线的区域.这个边界的中心应该是你的线的确切中心.如果Polyline包含超过2个点,则中心不会落在单击线的中心,而是包含所有点的边界的中心.如果要将mutli-segment Polylines与此函数一起使用,则需要更多的数学计算来计算单击的段.
这是一个使用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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8626 次 |
| 最近记录: |