I am trying to extract coordinates from a custom Google Maps overlay to check whether a location is within the drawn geofence. Unfortunately, since it is not a polygon object, I cannot simply call the containsLocation method. I tried calling getSource() on the DrawingManager object, but am getting an error that the method is not defined… And have been trying whatever possible to get the coordinates from the overlay to call containsLocation() on creating a new polygon object to access the method.
If anyone has advice on how I am calling the methods incorrectly to be receiving an error that getSource() is not a valid function for drawingManager and/or e.
let _myPolygon, coords;
/*
* other code goes here
*/
//add option inside to create button listener for draw geofence button.
drawGeofenceButton.addEventListener("click", () =>{
//ensure we can get rid of the old geofence
if(tri != null && tri != undefined){
tri.setMap(null);
}
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: true,
drawingControlOptions: {
drawingModes: [
google.maps.drawing.OverlayType.POLYGON,
]
},
circleOptions: {
fillColor: '#FF0000',
fillOpacity: 0.6,
strokeWeight: 3,
clickable: true,
editable: true,
zIndex: 1
},
polygonOptions: {
fillColor: '#FF0000',
fillOpacity: 0.6,
strokeWeight: 3,
clickable: true,
editable: true,
zIndex: 1
},
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
_myPolygon = e.overlay;
//get coordinates of the overlay and store them
//have tried drawingManager.getPath()
coords = (e.getPath().getArray());
});
/**
* Listener for whether a point is inside the geofence
*/
google.maps.event.addListener(map, "click", (e) => {
//create a polygon object to compare to
let my_geofence = new google.maps.Polygon({
paths: coords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: "#FF0000",
fillOpacity: 0.35,
editable: true
});
//check if the point clicked is inside the geofence
const isInside = google.maps.geometry.poly.containsLocation(
e.latLng,
my_geofence
) ? true
: false;
if(isInside){
alert("Point inside geofence.");
} else {
alert("Point not inside geofence.");
}
});
});