谷歌地图中间的多义线(质心?) [英] google maps middle of a polyline (centroid?)

查看:127
本文介绍了谷歌地图中间的多义线(质心?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多段线列表,就像谷歌地图当我点击多段线时,我想要一个infowindow只显示在我点击的位置,并且它对这个函数正常工作

  function mapsInfoWindow(polyline,content){
google.maps.event.addListener(polyline,'click',function(event){
infowindow.content = content;
infowindow.position = event.latLng;
infowindow.open(map);
});
}

当我点击列表时(使用相同的函数) ),事件显然没有 latLng ,但我希望infowindow无论如何都显示在折线的中间,就像它在点击时一样google地图中的链接链接我之前提到过



试过LatLngBounds();但是这给了多边形创建区域的实际中心,而不是我需要的中间区域。



任何想法如何操作?
<解决方案

解决方案

解决方案

/www.geocodezip.com/scripts/v3_epoly.jsrel =nofollow> http://www.geocodezip.com/scripts/v3_epoly.js 库,然后计算你折线的总长度(各种方法),将它分成两半,然后调用epoly的.GetPointsAtDistance()函数。

这应该返回LatLng点,但它有时会显得有点奇怪,返回两点或者甚至将该点以某种方式破碎。因此,您可以做的最安全的事情可能是这样的:

  var pointInHalf = polyline.GetPointsAtDistance(polylineLength); 
var pointCoordinate = new google.maps.LatLng(pointInHalf [0] .lat(),pointInHalf [0] .lng());

好吧,比没有好。


I have a list of polylines, just like google maps does when I click on the polyline I want an infowindow to show up just where I clicked, and it works just fine with this function

function mapsInfoWindow(polyline, content) {
    google.maps.event.addListener(polyline, 'click', function(event) {            
        infowindow.content = content;
        infowindow.position = event.latLng;
        infowindow.open(map);
    });
}

the problem comes when I click on the list(using the same function for that), event obviously doesn't have the latLng, but I'd like infowindow to show up in the middle of the polyline anyway, just like it does when you click on the list in the google maps link I mentioned before.

Tried LatLngBounds(); but that gives the actuall center of the area the polylines create, not the middle I need.

Any idea how to do it?

解决方案

So this is the(bit hacky) solution.

Use http://www.geocodezip.com/scripts/v3_epoly.js library, then count the total length of you polyline(various ways), divide it in half and call epoly's .GetPointsAtDistance() function upon it.

This should return LatLng point, but it acts a bit weird sometimes, returning two points or even turning that point somehow "broken". So the most secure thing you can do is probably this:

var pointInHalf = polyline.GetPointsAtDistance(polylineLength);
var pointCoordinate = new google.maps.LatLng(pointInHalf[0].lat(), pointInHalf[0].lng());

Well, better than nothing.

这篇关于谷歌地图中间的多义线(质心?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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