Reputation: 606
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);
Upvotes: 4
Views: 3579
Reputation: 21
containsLocation works only in the fiddle by davidkonrad if your listener is for clicks on the polyline. If your listener is on the map instead you will get 'true' anywhere in the triangle formed by the 3 LatLngs. If the user can't click the map itself then this is fine, but it may not work for your specific requirement.
Sadly, I can't see why the vertical line isn't working either (but it isn't just vertical lines, it doesn't seem to be working on any line that isn't horizontal).
Upvotes: 0
Reputation: 85518
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".
Upvotes: 3