Saturday, February 16, 2013

How to draw Inverse Circle in Google Map?

When i was working one of my client projects, there was a requirement to draw a Circle in Google Map. But the circle was quite different from regular Circle of Google Map Javascript API. In Google Map Javascript API the regular Circle has an option to fill the inside of circle with a color, but Requirement was inverse, means we have to fill the outside of the circle! My client wants everything outside of the circle to be filled in with the color and opacity that he wants to set.

After lots of searching in Google and reading Google Map Javascript API, when I didn't find any solution, then I started to build my own library to draw an InvertedCircle . I used google.maps.MVCObject class to give same functionality as regular Circle of  Google Map Javascript API

It has following methods and events, Following table design has been copied from https://developers.google.com/maps/documentation/javascript/reference#Circle

Methods

MethodsReturn ValueDescription
getBounds()LatLngBoundsGets the LatLngBounds of this Circle.
getCenter()LatLngReturns the center of this circle.
getEditable()booleanReturns whether this circle can be edited by the user.
getMap()MapReturns the map on which this circle is displayed.
getRadius()numberReturns the radius of this circle (in meters).
getVisible()booleanReturns whether this circle is visible on the map.
setCenter(center:LatLng)NoneSets the center of this circle.
setEditable(editable:boolean)NoneIf set to true, the user can edit this circle by dragging the control points shown at the center and around the circumference of the circle.
setMap(map:Map)NoneRenders the circle on the specified map. If map is set to null, the circle will be removed.
setRadius(radius:number)NoneSets the radius of this circle (in meters).
setVisible(visible:boolean)NoneHides this circle if set to false.

Events

EventsArgumentsDescription
center_changedNoneThis event is fired when the circle's center is changed.
radius_changedNoneThis event is fired when the circle's radius is changed.
visible_changedNoneThis event is fired when the circle's visible is changed.
I will try to improve my library so that it will be more flexible. Special thanks to Sammy Hubner for giving me some awesome codes, which helped me to solve problems and to build this library. 

You can find code in Github, url is https://github.com/raihan2006i/google-map-inverted-circle 

Here is the screenshots of InvertedCircle, You can check Demo File