-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjquery.geolocation.js
48 lines (38 loc) · 1.14 KB
/
jquery.geolocation.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
(function ($) {
$.fn.geolocate = function (address, options) {
var defaults = {
googleMaps: {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
};
var options = $.extend(defaults, options);
var mapContainer = $(this);
var geocoder = new google.maps.Geocoder();
var googleMapsElement = document.getElementById(mapContainer.attr('id'));
var map = new google.maps.Map(googleMapsElement, options.googleMaps);
if ($.isArray(address)){
for (i=0;i<address.length;i++){
geocoder.geocode({'address': address[i]}, applyLocationToMap);
}
}
else{
geocoder.geocode({'address': address}, applyLocationToMap);
}
function applyLocationToMap(results, status) {
var location = results[0].geometry.location;
if (status == google.maps.GeocoderStatus.OK) {
google.maps.event.trigger(map, 'resize');
setMarker(location);
map.setCenter(location);
}
}
function setMarker(position){
new google.maps.Marker({
map: map,
position: position
});
}
return this;
};
})(jQuery);