'containsLocation not working
I'm trying to retrieve a boolean result using 'google.maps.geometry.poly.containsLocation', the problem is than isnt returning nothing, I don't know if I need to called it inside of a eventListener, but when I try to called it starts saying that 'pos' is undefined, then when I intialize pos, it says that "lng" is not defined, something is wrong with my code or I'm not doing it the right way.
JS:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 41.185083, lng: -8.698814},
zoom: 12,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('You are Here.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
// Define the LatLng coordinates for Zone.
var zone_Coords = [
{lat: 41.194975, lng: -8.683102},
{lat: 41.183693, lng: -8.703740},
{lat: 41.205572, lng: -8.717563},
{lat: 41.203185, lng: -8.690135},
{lat: 41.194975, lng: -8.683102}
];
// Construct the Zone.
var zone = new google.maps.Polygon({
paths: zone_Coords,
strokeColor: '#023bca',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#0748eb',
fillOpacity: 0.35
});
zone3.setMap(map);
var isWithinPolygon = google.maps.geometry.poly.containsLocation(pos, zone);
console.log(isWithinPolygon);
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
