forked from tristandunn/jquery-auto-geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.auto-geocoder.js
131 lines (107 loc) · 3.77 KB
/
jquery.auto-geocoder.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(function($) {
var geocoder = new google.maps.Geocoder();
$.fn.autoGeocoder = function(options) {
var autoGeocoder = $.fn.autoGeocoder;
var options = $.extend(true, {}, autoGeocoder.defaults, options || {});
var setup = options.setup || autoGeocoder.base;
for (property in setup) {
var length = setup[property].length;
for (var i = 0; i < length; i++) {
setup[property][i].call(this, options);
}
}
return this.trigger('auto-geocoder.initialize');
};
$.fn.autoGeocoder.base = {
initialize: [function(options) {
options.initial.center = google.maps.LatLng.call(this, options.initial.center);
this.bind('auto-geocoder.initialize', function() {
$(this)
.trigger('auto-geocoder.createMap')
.trigger('auto-geocoder.onKeyUp');
});
}],
createMap: [function(options) {
this.bind('auto-geocoder.createMap', function() {
var element = $('<div>', { 'class' : options.className });
if (options.position == 'before' || options.position == 'after') {
$(this)[options.position](element);
} else {
$(options.position).append(element);
}
// fixes the issue related to people selecting cached text input
$(this).bind('blur.auto-geocoder', function() {
$(this).trigger('auto-geocoder.onKeyUp');
});
$(this).bind('keyup.auto-geocoder', function() {
$(this).trigger('auto-geocoder.onKeyUp');
});
this.map = new google.maps.Map(element[0], options.initial);
});
}],
onKeyUp: [function(options) {
this.bind('auto-geocoder.onKeyUp', function() {
var self = this;
var element = $(self);
var address = $.trim(element.val()).replace(/\s+/g, ' ').toLowerCase();
if (this.timeout) {
clearTimeout(this.timeout);
}
if (this.previousAddress &&
this.previousAddress == address) {
return;
}
if (address == '') {
element.trigger('auto-geocoder.onGeocodeResult', [[], '']);
return;
}
this.timeout = setTimeout(function() {
self.previousAddress = address;
geocoder.geocode({ address: address }, function(results, status) {
element.trigger('auto-geocoder.onGeocodeResult', [results, status]);
});
}, options.delay);
});
}],
onGeocodeResult: [function(options) {
this.get(0).marker = new google.maps.Marker();
this.bind('auto-geocoder.onGeocodeResult', function(e, results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (options.success.zoom == 'auto') {
this.map.fitBounds(results[0].geometry.viewport);
} else {
this.map.setZoom(options.success.zoom);
this.map.setCenter(results[0].geometry.location);
}
this.marker.setPosition(results[0].geometry.location);
this.marker.setMap(this.map);
$(this).trigger('auto-geocoder.onGeocodeSuccess', [results, status]);
} else {
if (this.marker) {
this.marker.setMap(null);
}
this.map.setZoom(options.initial.zoom);
this.map.setCenter(options.initial.center);
$(this).trigger('auto-geocoder.onGeocodeFailure', [results, status]);
}
});
}],
onGeocodeSuccess: [],
onGeocodeFailure: []
};
$.fn.autoGeocoder.defaults = {
className : 'jquery-auto-geocoder-map',
position : 'after',
delay : 500,
success : {
zoom : 'auto'
},
initial : {
zoom : 1,
center : [34, 0],
draggable : false,
mapTypeId : google.maps.MapTypeId.ROADMAP,
mapTypeControl : false
}
};
})(jQuery);