Skip to content

Commit 730be9f

Browse files
committed
Merge pull request BetterBrief#19 from BetterBrief/pulls/jq-fn-sig
Introduce gmapfield jQuery fn signature
2 parents ee7396c + 98aa7e6 commit 730be9f

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

javascript/GoogleMapField.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88

99
// Run this code for every googlemapfield
1010
function initField() {
11-
var field = $(this),
12-
fieldID = field.attr('data-field-id'), // identify its settings
13-
options = JSON.parse(field.attr('data-settings')),
14-
centre = new google.maps.LatLng(options.coords[0], options.coords[1]),
15-
options = {
11+
var field = $(this);
12+
if(field.data('gmapfield-inited') === true) {
13+
return;
14+
}
15+
field.data('gmapfield-inited', true);
16+
var settings = JSON.parse(field.attr('data-settings')),
17+
centre = new google.maps.LatLng(settings.coords[0], settings.coords[1]),
18+
mapSettings = {
1619
streetViewControl: false,
17-
zoom: options.map.zoom * 1,
20+
zoom: settings.map.zoom * 1,
1821
center: centre,
19-
mapTypeId: google.maps.MapTypeId[options.map.mapTypeId]
22+
mapTypeId: google.maps.MapTypeId[settings.map.mapTypeId]
2023
},
2124
mapElement = field.find('.googlemapfield-map'),
22-
map = new google.maps.Map(mapElement[0], options),
25+
map = new google.maps.Map(mapElement[0], mapSettings),
2326
marker = new google.maps.Marker({
2427
position: map.getCenter(),
2528
map: map,
@@ -36,7 +39,7 @@
3639
var latCoord = latLng.lat(),
3740
lngCoord = latLng.lng();
3841

39-
options.coords = [latCoord, lngCoord];
42+
mapSettings.coords = [latCoord, lngCoord];
4043

4144
latField.val(latCoord);
4245
lngField.val(lngCoord);
@@ -103,8 +106,14 @@
103106

104107
}
105108

109+
$.fn.gmapfield = function() {
110+
return this.each(function() {
111+
initField.call(this);
112+
});
113+
}
114+
106115
function init() {
107-
var mapFields = $('.googlemapfield');
116+
var mapFields = $('.googlemapfield:visible').gmapfield();
108117
mapFields.each(initField);
109118
}
110119

0 commit comments

Comments
 (0)