-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.google.circleoverlay.js
96 lines (80 loc) · 5.83 KB
/
maps.google.circleoverlay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Circle overlay extension for Google Maps
// App Delegate Inc <http://appdelegateinc.com> 2010
// This file adds a new CircleOverlay to GMaps2 to draw a circle on a map with stroke and fill
// If you include the library at http://appdelegateinc.com/blog/2010/05/16/point-in-polygon-checking/ you can also check to see if a point resides within the circle.
// Constructor
var CircleOverlay = function(map, latLng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity, numPoints) {
this.map = map;
this.setMap(map);
this.latLng = latLng;
this.radius = radius;
this.strokeColor = strokeColor;
this.strokeWidth = strokeWidth;
this.strokeOpacity = strokeOpacity;
this.fillColor = fillColor;
this.fillOpacity = fillOpacity;
// Set resolution of polygon
if (typeof(numPoints) == 'undefined') {
this.numPoints = 45;
} else {
this.numPoints = numPoints;
}
}
// Inherit from GOverlay
CircleOverlay.prototype = new google.maps.OverlayView();
// Reset overlay
CircleOverlay.prototype.clear = function() {
if(this.polygon != null && this.map != null) {
this.polygon.setMap(null);
}
}
CircleOverlay.prototype.visible = function() {
if (this.polygon != null && this.map != null) {
this.polygon.setMap(this.map);
}
}
// Calculate all the points of the circle and draw them
CircleOverlay.prototype.draw = function(force) {
var d2r = Math.PI / 180;
circleLatLngs = new Array();
// Convert statute miles into degrees latitude
var circleLat = this.radius * 0.014483;
var circleLng = circleLat / Math.cos(this.latLng.lat() * d2r);
// Create polygon points (extra point to close polygon)
for (var i = 0; i < this.numPoints + 1; i++) {
// Convert degrees to radians
var theta = Math.PI * (i / (this.numPoints / 2));
var vertexLat = this.latLng.lat() + (circleLat * Math.sin(theta));
var vertexLng = this.latLng.lng() + (circleLng * Math.cos(theta));
var vertextLatLng = new google.maps.LatLng(vertexLat, vertexLng);
circleLatLngs.push(vertextLatLng);
}
this.clear();
this.polygon = new google.maps.Polygon({
paths: circleLatLngs,
strokeColor: this.strokeColor,
strokeWeight: this.strokeWidth,
strokeOpacity: this.strokeOpacity,
fillColor: this.fillColor,
fillOpacity: this.fillOpacity
});
this.polygon.setMap(this.map);
}
// Remove circle method
CircleOverlay.prototype.remove = function() {
this.clear();
}
// Can use this method if the library at is included at http://appdelegateinc.com/blog/2010/05/16/point-in-polygon-checking/
CircleOverlay.prototype.containsLatLng = function(latLng) {
if(this.polygon.containsLatLng) {
return this.polygon.containsLatLng(latLng);
}
}
// Set radius of circle
CircleOverlay.prototype.setRadius = function(radius) {
this.radius = radius;
}
// Set center of circle
CircleOverlay.prototype.setLatLng = function(latLng) {
this.latLng = latLng;
}