Reputation: 30158
I am trying to determine if a marker which is being animated is within the viewable area of my map. If it is not, I want to tell my map to pan to the marker position as a center position. How do I do this? This is my code:
var curPoint = racePath.GetPointAtDistance(curDist);
var curZoom = self.map.zoom;
var mapBounds = new google.maps.LatLngBounds(currentCenter);
var isInBounds = mapBounds.contains(curPoint);
This is not working, because mapBounds needs the southwest and northeast point of your map, but I don't know how to calculate those, without getting them from the mapBounds.getNorthEast() and mapBounds.getSouthWest() methods - documented here: https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds.
Any ideas on how to solve this?
Upvotes: 0
Views: 1887
Reputation: 3434
To determine the current bounds of the map, call getBounds() on you map variable: https://developers.google.com/maps/documentation/javascript/reference#Map
If you want to know if a point is within the the current view area, call 'contains(LatLng)' on the LatLngBounds. Like so:
var currentBounds = myMap.getBounds();
var isInSight = currentBounds.contains(myPointLatLng);
if(isInSight){
//Your point is within the current bounds
}
If you want to pan so the map is centered on your point, call 'panTo(LatLng)' on you map variable:
var currentBounds = myMap.getBounds();
var isInSight = currentBounds.contains(myPointLatLng);
if(isInSight){
myMap.panTo(myPointLatLng);
}
Upvotes: 1