Skip to content

Commit 3069dcc

Browse files
committed
New feature. Custom controls
1 parent a3bee8c commit 3069dcc

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Visit the examples in [hpneo.github.com/gmaps](http://hpneo.github.com/gmaps/)
88
Changelog
99
---------
1010

11+
0.1.10
12+
-----------------------
13+
* New feature: **Custom controls**
14+
1115
0.1.9
1216
-----------------------
1317
* New feature: **Static maps**

examples/map_controls.html

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>GMaps.js &mdash; Map events</title>
6+
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
7+
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
8+
<script type="text/javascript" src="../gmaps.js"></script>
9+
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css" />
10+
<link rel="stylesheet" type="text/css" href="examples.css" />
11+
<script type="text/javascript">
12+
var map;
13+
$(document).ready(function(){
14+
map = new GMaps({
15+
div: '#map',
16+
zoom: 16,
17+
lat: -12.043333,
18+
lng: -77.028333
19+
});
20+
map.addControl({
21+
position: 'top_right',
22+
text: 'Geolocate',
23+
style: {
24+
margin: '5px',
25+
padding: '1px 6px',
26+
border: 'solid 1px #717B87',
27+
background: '#fff'
28+
},
29+
events: {
30+
click: function(){
31+
GMaps.geolocate({
32+
success: function(position){
33+
map.setCenter(position.coords.latitude, position.coords.longitude);
34+
},
35+
error: function(error){
36+
alert('Geolocation failed: ' + error.message);
37+
},
38+
not_supported: function(){
39+
alert("Your browser does not support geolocation");
40+
}
41+
});
42+
}
43+
}
44+
});
45+
});
46+
</script>
47+
</head>
48+
<body>
49+
<h1>GMaps.js &mdash; Map events</h1>
50+
<div class="row">
51+
<div class="span11">
52+
<div id="map"></div>
53+
</div>
54+
<div class="span6">
55+
<p>GMaps.js allows to add custom controls:</p>
56+
<pre>map.addControl({
57+
position: 'top_right',
58+
text: 'Geolocate',
59+
style: {
60+
margin: '5px',
61+
padding: '1px 6px',
62+
border: 'solid 1px #717B87',
63+
background: '#fff'
64+
},
65+
events: {
66+
click: function(){
67+
console.log(this);
68+
}
69+
}
70+
});</pre>
71+
<p>
72+
<span class="label notice">Note</span> You can use the following positions:
73+
<ul>
74+
<li>top_center</li>
75+
<li>top_left</li>
76+
<li>top_right</li>
77+
<li>left_top</li>
78+
<li>right_top</li>
79+
<li>left_center</li>
80+
<li>right_center</li>
81+
<li>left_bottom</li>
82+
<li>right_bottom</li>
83+
<li>bottom_center</li>
84+
<li>bottom_left</li>
85+
<li>bottom_right</li>
86+
</ul>
87+
You can learn more of custom controls <a href="https://developers.google.com/maps/documentation/javascript/controls#ControlPositioning">here</a>.</p>
88+
</div>
89+
</div>
90+
</body>
91+
</html>

gmaps.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var GMaps = (function($) {
88
window.context_menu = {};
99

1010
this.div = $(options.div)[0];
11+
this.controls = [];
1112
this.overlays = [];
1213
this.markers = [];
1314
this.polylines = [];
@@ -241,6 +242,41 @@ var GMaps = (function($) {
241242
this.map.setZoom(this.map.getZoom() - value);
242243
};
243244

245+
this.createControl = function(options) {
246+
options.style.cursor = 'pointer';
247+
options.style.fontFamily = 'Arial, sans-serif';
248+
options.style.fontSize = '13px';
249+
options.style.boxShadow = 'rgba(0, 0, 0, 0.398438) 0px 2px 4px';
250+
251+
var controlDiv = $('<div></div>');
252+
controlDiv.css(options.style);
253+
controlDiv.text(options.text);
254+
255+
var control = controlDiv[0];
256+
257+
for(var e in options.events) {
258+
google.maps.event.addDomListener(control, e, function() {
259+
options.events[e].apply(this, [this]);
260+
});
261+
}
262+
263+
control.index = 1;
264+
265+
return control;
266+
};
267+
268+
this.addControl = function(options) {
269+
var position = google.maps.ControlPosition[options.position.toUpperCase()];
270+
271+
delete options.position;
272+
273+
var control = this.createControl(options);
274+
this.controls.push(control);
275+
this.map.controls[position].push(control);
276+
277+
return control;
278+
};
279+
244280
// Markers
245281
this.createMarker = function(options) {
246282
if (options.lat && options.lng) {

0 commit comments

Comments
 (0)