Google地图API V3 - isLocationOnEdge()不起作用 [英] Google maps API V3 - isLocationOnEdge() doesn't work

查看:155
本文介绍了Google地图API V3 - isLocationOnEdge()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对google.maps.geometry.poly.isLocationOnEdge()方法有问题。在小提琴中,尝试点击水平线,像预期的那样返回true,但点击垂直线返回false。为什么?



谢谢!

这里是 fiddle ,这里是代码:

  function initialize(){
var mapOptions = {
zoom:8,
center:new google.maps.LatLng(0.5,0.5),
mapTypeId:google.maps.MapTypeId.TERRAIN
};

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

var poly = new google.maps.Polyline({
path:[
new google.maps.LatLng(0,0),
new google.maps .LatLng(0,1),
new google.maps.LatLng(1,1)
],
map:map
});

google.maps.event.addListener(poly,'click',function(event){
alert(google.maps.geometry.poly.isLocationOnEdge(event.latLng,this), 0.00001);
});
}

google.maps.event.addDomListener(window,'load',initialize);


解决方案

我认为您最好使用 containsLocation()



docs


要查找给定点是否落入多边形,点
和多边形为google.maps.geometry.poly.containsLocation()。
函数返回true,如果该点位于多边形内或其
边缘。


vs p>


要确定点是否落在多边形边线上或附近,或者在多边形边缘附近或
上,折线/多边形和
(可选)以度为单位的公差值为
google.maps.geometry.poly.isLocationOnEdge()。

如果您使用 containsLocation 它可以工作

  alert(google.maps.geometry.poly.containsLocation(event.latLng,poly)); 

分叉小提琴 http://jsfiddle.net/VL7Rx/



我真的无法解释为什么它不能与 isLocationOnEdge 。这似乎是 100%垂直多段线的一个问题,并且对于您的0,0,0,1等,Lng始终为零(0)(单击垂直) - 就好像线不有任何宽度,地图。

I have a problem with google.maps.geometry.poly.isLocationOnEdge() method. In fiddle, try to click on horizontal line, returns true like expected, but clicking on vertical line returns false. Why?

Thanks!

Here is fiddle and here is code:

function initialize() {
    var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(0.5, 0.5),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

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

  var poly = new google.maps.Polyline({
    path: [
      new google.maps.LatLng(0, 0),
      new google.maps.LatLng(0, 1),
      new google.maps.LatLng(1, 1)
    ],
      map: map
  });

    google.maps.event.addListener(poly, 'click', function(event){
        alert(google.maps.geometry.poly.isLocationOnEdge(event.latLng, this), 0.00001);
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

解决方案

I think you are better off using containsLocation()

From the docs :

To find whether a given point falls within a polygon, pass the point and the polygon to google.maps.geometry.poly.containsLocation(). The functions returns true if the point is within the polygon or on its edge.

vs

To determine whether a point falls on or near a polyline, or on or near the edge of a polygon, pass the point, the polyline/polygon, and optionally a tolerance value in degrees to google.maps.geometry.poly.isLocationOnEdge().

If you use containsLocation it works

alert(google.maps.geometry.poly.containsLocation(event.latLng, poly));

forked fiddle http://jsfiddle.net/VL7Rx/

I really cant explain exactly why it does not working with isLocationOnEdge. It seems to be an issue on 100% vertical polylines only, and with your 0,0,0,1 etc the Lng always is zero (0) (clicking vertical) - as if the line does not have any "width" at all, "mapwise".

这篇关于Google地图API V3 - isLocationOnEdge()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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