Skip to content

Commit beb9775

Browse files
author
corvis
committed
Fixed #27, added grunt build, fixed #25
1 parent 7c1baa8 commit beb9775

File tree

8 files changed

+404
-13
lines changed

8 files changed

+404
-13
lines changed

Gruntfile.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
module.exports = function (grunt) {
4+
5+
// Load grunt tasks automatically
6+
require('load-grunt-tasks')(grunt);
7+
8+
// Configurable paths for the application
9+
var appConfig = {
10+
app: 'src',
11+
dist: 'dist'
12+
};
13+
14+
grunt.initConfig({
15+
// Project settings
16+
pkg: grunt.file.readJSON('package.json'),
17+
appConfig: appConfig,
18+
// Empties folders to start fresh
19+
clean: {
20+
dist: {
21+
files: [{
22+
dot: true,
23+
src: [
24+
'<%= appConfig.dist %>/{,*/}*'
25+
]
26+
}]
27+
}
28+
},
29+
uglify: {
30+
options: {
31+
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
32+
'<%= grunt.template.today("yyyy-mm-dd") %> */\n'
33+
},
34+
minimize: {
35+
options: {
36+
beautify: false,
37+
compress: true,
38+
sourceMap: true
39+
},
40+
files: {
41+
'<%= appConfig.dist %>/locationpicker.jquery.min.js': ['<%= appConfig.app %>/*.js']
42+
}
43+
},
44+
beautify: {
45+
options: {
46+
beautify: {
47+
width: 80,
48+
beautify: true
49+
},
50+
compress: false,
51+
sourceMap: false,
52+
mangle: false
53+
},
54+
files: {
55+
'<%= appConfig.dist %>/locationpicker.jquery.js': ['<%= appConfig.app %>/*.js']
56+
}
57+
}
58+
}
59+
});
60+
61+
grunt.registerTask('build', [
62+
'clean:dist',
63+
'uglify:minimize',
64+
'uglify:beautify'
65+
]);
66+
67+
};

dist/locationpicker.jquery.js

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
/*! locationpicker.jquery - v0.11.0 - 2015-01-03 */
2+
(function($) {
3+
function GMapContext(domElement, options) {
4+
var _map = new google.maps.Map(domElement, options);
5+
var _marker = new google.maps.Marker({
6+
position: new google.maps.LatLng(54.19335, -3.92695),
7+
map: _map,
8+
title: "Drag Me",
9+
draggable: options.draggable
10+
});
11+
return {
12+
map: _map,
13+
marker: _marker,
14+
circle: null,
15+
location: _marker.position,
16+
radius: options.radius,
17+
locationName: options.locationName,
18+
addressComponents: {
19+
formatted_address: null,
20+
addressLine1: null,
21+
addressLine2: null,
22+
streetName: null,
23+
streetNumber: null,
24+
city: null,
25+
district: null,
26+
state: null,
27+
stateOrProvince: null
28+
},
29+
settings: options.settings,
30+
domContainer: domElement,
31+
geodecoder: new google.maps.Geocoder()
32+
};
33+
}
34+
var GmUtility = {
35+
drawCircle: function(gmapContext, center, radius, options) {
36+
if (gmapContext.circle != null) {
37+
gmapContext.circle.setMap(null);
38+
}
39+
if (radius > 0) {
40+
radius *= 1;
41+
options = $.extend({
42+
strokeColor: "#0000FF",
43+
strokeOpacity: .35,
44+
strokeWeight: 2,
45+
fillColor: "#0000FF",
46+
fillOpacity: .2
47+
}, options);
48+
options.map = gmapContext.map;
49+
options.radius = radius;
50+
options.center = center;
51+
gmapContext.circle = new google.maps.Circle(options);
52+
return gmapContext.circle;
53+
}
54+
return null;
55+
},
56+
setPosition: function(gMapContext, location, callback) {
57+
gMapContext.location = location;
58+
gMapContext.marker.setPosition(location);
59+
gMapContext.map.panTo(location);
60+
this.drawCircle(gMapContext, location, gMapContext.radius, {});
61+
if (gMapContext.settings.enableReverseGeocode) {
62+
gMapContext.geodecoder.geocode({
63+
latLng: gMapContext.location
64+
}, function(results, status) {
65+
if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
66+
gMapContext.locationName = results[0].formatted_address;
67+
gMapContext.addressComponents = GmUtility.address_component_from_google_geocode(results[0].address_components);
68+
}
69+
if (callback) {
70+
callback.call(this, gMapContext);
71+
}
72+
});
73+
} else {
74+
if (callback) {
75+
callback.call(this, gMapContext);
76+
}
77+
}
78+
},
79+
locationFromLatLng: function(lnlg) {
80+
return {
81+
latitude: lnlg.lat(),
82+
longitude: lnlg.lng()
83+
};
84+
},
85+
address_component_from_google_geocode: function(address_components) {
86+
var result = {};
87+
for (var i = address_components.length - 1; i >= 0; i--) {
88+
var component = address_components[i];
89+
if (component.types.indexOf("postal_code") >= 0) {
90+
result.postalCode = component.short_name;
91+
} else if (component.types.indexOf("street_number") >= 0) {
92+
result.streetNumber = component.short_name;
93+
} else if (component.types.indexOf("route") >= 0) {
94+
result.streetName = component.short_name;
95+
} else if (component.types.indexOf("locality") >= 0) {
96+
result.city = component.short_name;
97+
} else if (component.types.indexOf("sublocality") >= 0) {
98+
result.district = component.short_name;
99+
} else if (component.types.indexOf("administrative_area_level_1") >= 0) {
100+
result.stateOrProvince = component.short_name;
101+
} else if (component.types.indexOf("country") >= 0) {
102+
result.country = component.short_name;
103+
}
104+
}
105+
result.addressLine1 = [ result.streetNumber, result.streetName ].join(" ").trim();
106+
result.addressLine2 = "";
107+
return result;
108+
}
109+
};
110+
function isPluginApplied(domObj) {
111+
return getContextForElement(domObj) != undefined;
112+
}
113+
function getContextForElement(domObj) {
114+
return $(domObj).data("locationpicker");
115+
}
116+
function updateInputValues(inputBinding, gmapContext) {
117+
if (!inputBinding) return;
118+
var currentLocation = GmUtility.locationFromLatLng(gmapContext.location);
119+
if (inputBinding.latitudeInput) {
120+
inputBinding.latitudeInput.val(currentLocation.latitude).change();
121+
}
122+
if (inputBinding.longitudeInput) {
123+
inputBinding.longitudeInput.val(currentLocation.longitude).change();
124+
}
125+
if (inputBinding.radiusInput) {
126+
inputBinding.radiusInput.val(gmapContext.radius).change();
127+
}
128+
if (inputBinding.locationNameInput) {
129+
inputBinding.locationNameInput.val(gmapContext.locationName).change();
130+
}
131+
}
132+
function setupInputListenersInput(inputBinding, gmapContext) {
133+
if (inputBinding) {
134+
if (inputBinding.radiusInput) {
135+
inputBinding.radiusInput.on("change", function() {
136+
gmapContext.radius = $(this).val();
137+
GmUtility.setPosition(gmapContext, gmapContext.location, function(context) {
138+
context.settings.onchanged.apply(gmapContext.domContainer, [ GmUtility.locationFromLatLng(context.location), context.radius, false ]);
139+
});
140+
});
141+
}
142+
if (inputBinding.locationNameInput && gmapContext.settings.enableAutocomplete) {
143+
gmapContext.autocomplete = new google.maps.places.Autocomplete(inputBinding.locationNameInput.get(0));
144+
google.maps.event.addListener(gmapContext.autocomplete, "place_changed", function() {
145+
var place = gmapContext.autocomplete.getPlace();
146+
if (!place.geometry) {
147+
gmapContext.settings.onlocationnotfound(place.name);
148+
return;
149+
}
150+
GmUtility.setPosition(gmapContext, place.geometry.location, function(context) {
151+
updateInputValues(inputBinding, context);
152+
context.settings.onchanged.apply(gmapContext.domContainer, [ GmUtility.locationFromLatLng(context.location), context.radius, false ]);
153+
});
154+
});
155+
}
156+
if (inputBinding.latitudeInput) {
157+
inputBinding.latitudeInput.on("change", function() {
158+
GmUtility.setPosition(gmapContext, new google.maps.LatLng($(this).val(), gmapContext.location.lng()), function(context) {
159+
context.settings.onchanged.apply(gmapContext.domContainer, [ GmUtility.locationFromLatLng(context.location), context.radius, false ]);
160+
});
161+
});
162+
}
163+
if (inputBinding.longitudeInput) {
164+
inputBinding.longitudeInput.on("change", function() {
165+
GmUtility.setPosition(gmapContext, new google.maps.LatLng(gmapContext.location.lat(), $(this).val()), function(context) {
166+
context.settings.onchanged.apply(gmapContext.domContainer, [ GmUtility.locationFromLatLng(context.location), context.radius, false ]);
167+
});
168+
});
169+
}
170+
}
171+
}
172+
$.fn.locationpicker = function(options, params) {
173+
if (typeof options == "string") {
174+
var _targetDomElement = this.get(0);
175+
if (!isPluginApplied(_targetDomElement)) return;
176+
var gmapContext = getContextForElement(_targetDomElement);
177+
switch (options) {
178+
case "location":
179+
if (params == undefined) {
180+
var location = GmUtility.locationFromLatLng(gmapContext.location);
181+
location.radius = gmapContext.radius;
182+
location.name = gmapContext.locationName;
183+
return location;
184+
} else {
185+
if (params.radius) {
186+
gmapContext.radius = params.radius;
187+
}
188+
GmUtility.setPosition(gmapContext, new google.maps.LatLng(params.latitude, params.longitude), function(gmapContext) {
189+
updateInputValues(gmapContext.settings.inputBinding, gmapContext);
190+
});
191+
}
192+
break;
193+
194+
case "subscribe":
195+
if (params == undefined) {
196+
return null;
197+
} else {
198+
var event = params.event;
199+
var callback = params.callback;
200+
if (!event || !callback) {
201+
console.error('LocationPicker: Invalid arguments for method "subscribe"');
202+
return null;
203+
}
204+
google.maps.event.addListener(gmapContext.map, event, callback);
205+
}
206+
break;
207+
208+
case "map":
209+
if (params == undefined) {
210+
var locationObj = GmUtility.locationFromLatLng(gmapContext.location);
211+
locationObj.formattedAddress = gmapContext.locationName;
212+
locationObj.addressComponents = gmapContext.addressComponents;
213+
return {
214+
map: gmapContext.map,
215+
marker: gmapContext.marker,
216+
location: locationObj
217+
};
218+
} else {
219+
return null;
220+
}
221+
}
222+
return null;
223+
}
224+
return this.each(function() {
225+
var $target = $(this);
226+
if (isPluginApplied(this)) return;
227+
var settings = $.extend({}, $.fn.locationpicker.defaults, options);
228+
var gmapContext = new GMapContext(this, {
229+
zoom: settings.zoom,
230+
center: new google.maps.LatLng(settings.location.latitude, settings.location.longitude),
231+
mapTypeId: google.maps.MapTypeId.ROADMAP,
232+
mapTypeControl: false,
233+
disableDoubleClickZoom: false,
234+
scrollwheel: settings.scrollwheel,
235+
streetViewControl: false,
236+
radius: settings.radius,
237+
locationName: settings.locationName,
238+
settings: settings,
239+
draggable: settings.draggable
240+
});
241+
$target.data("locationpicker", gmapContext);
242+
google.maps.event.addListener(gmapContext.marker, "dragend", function(event) {
243+
GmUtility.setPosition(gmapContext, gmapContext.marker.position, function(context) {
244+
var currentLocation = GmUtility.locationFromLatLng(gmapContext.location);
245+
context.settings.onchanged.apply(gmapContext.domContainer, [ currentLocation, context.radius, true ]);
246+
updateInputValues(gmapContext.settings.inputBinding, gmapContext);
247+
});
248+
});
249+
GmUtility.setPosition(gmapContext, new google.maps.LatLng(settings.location.latitude, settings.location.longitude), function(context) {
250+
updateInputValues(settings.inputBinding, gmapContext);
251+
context.settings.oninitialized($target);
252+
var currentLocation = GmUtility.locationFromLatLng(gmapContext.location);
253+
});
254+
setupInputListenersInput(settings.inputBinding, gmapContext);
255+
});
256+
};
257+
$.fn.locationpicker.defaults = {
258+
location: {
259+
latitude: 40.7324319,
260+
longitude: -73.82480799999996
261+
},
262+
locationName: "",
263+
radius: 500,
264+
zoom: 15,
265+
scrollwheel: true,
266+
inputBinding: {
267+
latitudeInput: null,
268+
longitudeInput: null,
269+
radiusInput: null,
270+
locationNameInput: null
271+
},
272+
enableAutocomplete: false,
273+
enableReverseGeocode: true,
274+
draggable: true,
275+
onchanged: function(currentLocation, radius, isMarkerDropped) {},
276+
onlocationnotfound: function(locationName) {},
277+
oninitialized: function(component) {}
278+
};
279+
})(jQuery);

0 commit comments

Comments
 (0)