This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathjquery.auto-geocoder.js
138 lines (111 loc) · 3.84 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
132
133
134
135
136
137
138
(function($) {
var geocoder = new google.maps.Geocoder();
var autoGeocoder = $.fn.autoGeocoder = function(options) {
var options = $.extend(true, {}, autoGeocoder.defaults, options || {}),
setup = options.setup || autoGeocoder.base;
for (var property in setup) {
var methods = setup[property];
for (var i = 0, length = methods.length; i < length; i++) {
methods[i].call(this, options);
}
}
return this.trigger("auto-geocoder.initialize");
};
autoGeocoder.base = {
initialize: [function(options) {
options.initial.center = new google.maps.LatLng(
options.initial.center[0],
options.initial.center[1]
);
this.on("auto-geocoder.initialize", function() {
$(this)
.trigger("auto-geocoder.createMap")
.trigger("auto-geocoder.onKeyUp");
});
}],
createMap: [function(options) {
this.on("auto-geocoder.createMap", function() {
var element = $(this),
wrapper = $("<div>", { "class" : options.className }),
position = options.position;
if (position === "before" || position === "after") {
element[position](wrapper);
} else {
$(position).append(wrapper);
}
element.on("keyup.auto-geocoder", function() {
element.trigger("auto-geocoder.onKeyUp");
});
this.map = new google.maps.Map(wrapper[0], options.initial);
});
}],
onKeyUp: [function(options) {
this.on("auto-geocoder.onKeyUp", function() {
var element = $(this),
address = $.trim(element.val()).replace(/\s+/g, " ").toLowerCase(),
timeout = this.timeout,
previous = this.previousAddress;
clearTimeout(timeout);
if (previous && previous === address) {
return;
}
if (address === "") {
element.trigger("auto-geocoder.onGeocodeResult", [[], ""]);
return;
}
this.timeout = setTimeout($.proxy(function() {
this.previousAddress = address;
geocoder.geocode({ address: address }, function(results, status) {
element.trigger("auto-geocoder.onGeocodeResult", [results, status]);
});
}, this), options.delay);
});
}],
onGeocodeResult: [function(options) {
this.on("auto-geocoder.onGeocodeResult", function(event, results, status) {
var map = this.map,
marker = this.marker = this.marker || new google.maps.Marker();
if (status === google.maps.GeocoderStatus.OK) {
var geometry = results[0].geometry,
position = geometry.location;
if (options.success.zoom === "auto") {
map.fitBounds(geometry.viewport);
} else {
map.setZoom(options.success.zoom);
map.setCenter(position);
}
marker.setPosition(position);
marker.setMap(map);
$(this).trigger("auto-geocoder.onGeocodeSuccess", [results, status]);
} else {
var initial = options.initial;
if (marker) {
marker.setMap(null);
}
map.setZoom(initial.zoom);
map.setCenter(initial.center);
$(this).trigger("auto-geocoder.onGeocodeFailure", [results, status]);
}
});
}],
onGeocodeSuccess: [],
onGeocodeFailure: []
};
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,
scrollwheel : false,
disableDefaultUI : true,
disableDoubleClickZoom : true
}
};
})(jQuery);