1

I have a polygon in google maps. And I want the line to stay within the polygon's borders. My current code works, but only if the polygon has a simple shape, no sharp (concave) edges. When there are sharp edges, the line will go over the borders:

here is the code:

var polyOptions = {
    strokeColor: 'red',
    strokeOpacity: 1.0,
    strokeWeight: 3,
    editable: true,
    zIndex:2
  };
  poly = new google.maps.Polyline(polyOptions);
  poly.setMap(map);
  bermudaTriangle.setMap(map);

  google.maps.event.addListener(map, 'click', addLatLng);
  google.maps.event.addListener(map, 'rightclick', drawPolygon);
}



  var bermudaTriangle = new google.maps.Polygon({
    strokeColor: 'orange',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: 'yellow',
    fillOpacity: 0.35,
    zIndex:1,
    clickable:false,
    editable:true

  });



function addLatLng(event) {

  var path = poly.getPath();

if(google.maps.geometry.poly.containsLocation(event.latLng, bermudaTriangle)){
  path.push(event.latLng);
  console.log(path);
}

}

function drawPolygon(event) {

  var path = bermudaTriangle.getPath();
  path.push(event.latLng);

}
Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
yamahamm
  • 103
  • 1
  • 10
  • 2
    Try reading this: http://stackoverflow.com/questions/12222700/determine-if-line-segment-is-inside-polygon – Al Lelopath Sep 02 '14 at 15:45
  • checked it, the answer is about a point, not a line – yamahamm Sep 02 '14 at 16:49
  • How about this: http://stackoverflow.com/questions/19617038/finding-whether-a-line-segment-is-completely-inside-the-polygon-or-not – geocodezip Sep 02 '14 at 16:53
  • this may be helpful if it was in javascript or some theory how to make it... – yamahamm Sep 02 '14 at 16:56
  • 2
    How about testing for line-line intersection for each line in the polygon against the line in question? http://en.wikipedia.org/wiki/Line%E2%80%93line_intersection Also this might help: http://forum.sa-mp.com/showthread.php?t=314635 – Al Lelopath Sep 02 '14 at 19:40
  • Well this may be the solution what i am looking for, thanks – yamahamm Sep 02 '14 at 19:45

0 Answers0