bitpshr
bitpshr

Reputation: 1063

Detect if Circle is within Map

I know that when zooming a Google Map, it is possible to mandate that a circle fit within the map via:

map.fitBounds(circle.getBounds());

Is it possible to simply detect if the circle is contained within the map? E.g.

This would return true:

enter image description here

This would return false:

enter image description here

Upvotes: 0

Views: 97

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117364

possible solution:

google.maps.Circle.prototype.inViewPort=function(){
  var map=this.getMap();
  if(!map){
    return null;
  }
  try{
  var mb=map.getBounds(),
      cb=this.getBounds(),
      ub=new google.maps.LatLngBounds();
      ub.union(mb);
      ub.union(cb);
      return(ub.equals(mb));
  }catch(e){return null;}
}

usage: simply call circle.inViewPort() .

It will return true when the circle is fully visible inside the map, otherwise false.

When the circle is not associated with a map or the map hasn't finished initializing(the projection isn't available yet, happens when you call it before the first bounds_changed-event fires) it will return null

How it works:

It creates a empty LatLngBounds and extends it with the bounds of the map and the bounds of the circle. When the circle is fully visible the bounds of this LatLngBounds must be equal to the bounds of the map.

Upvotes: 2

Related Questions