-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.locationpicker.js
209 lines (173 loc) · 7.89 KB
/
jquery.locationpicker.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
*
*/
(function($){
//Attach this new method to jQuery
$.fn.extend({
//This is where you write your plugin's name
locationPicker: function() {
var options = {
width: "300px",
height: "200px",
backgroundColor: '#fff',
border: '1px solid #ccc',
borderRadius: 10,
padding: 10,
defaultLat: 51.500152,
defaultLng: -0.126236
};
function RoundDecimal(num, decimals){
var mag = Math.pow(10, decimals);
return Math.round(num * mag)/mag;
};
var geocoder = new google.maps.Geocoder();
//var mapsScript = document.createElement( 'script' );
//mapsScript.type = 'text/javascript';
//mapsScript.src = "http://maps.google.com/maps/api/js?sensor=false&callback=lptoremove";
//$(this).before( mapsScript );
//Iterate over the current set of matched elements
return this.each(function() {
var that = this;
var setPosition = function(latLng, viewport){
var lat = RoundDecimal(latLng.lat(), 6);
var lng = RoundDecimal(latLng.lng(), 6);
marker.setPosition(latLng);
if(viewport){
map.fitBounds(viewport);
map.setZoom(map.getZoom() + 2);
}else{
map.panTo(latLng);
}
$(that).val(lat + "," + lng);
}
var id = $(this).attr('id');
var searchButton = $("<input class='picker-search-button' type='button' value='Search'/>");
$(this).after(searchButton);
var picker = $("<div id='" + id + "-picker' class='pickermap'></div>").css({
width: options.width,
backgroundColor: options.backgroundColor,
border: options.border,
padding: options.padding,
borderRadius: options.borderRadius,
position: "absolute",
display: "none",
zIndex: 9999
});
$(this).after(picker);
var mapDiv = $("<div class='picker-map'>Loading</div>").css({
height: options.height
});
picker.append(mapDiv);
var myLatlng = new google.maps.LatLng(options.defaultLat, options.defaultLng);
var myOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.HYBRID,
mapTypeControl: false,
disableDoubleClickZoom: true,
streetViewControl: false
}
var map = new google.maps.Map(mapDiv.get(0), myOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "Drag Me",
draggable: true
});
google.maps.event.addListener(map, 'dblclick', function(event) {
setPosition(event.latLng);
});
google.maps.event.addListener(marker, 'dragend', function(event) {
setPosition(marker.position);
});
function getCurrentPosition(){
var posStr = $(that).val();
if(posStr != ""){
var posArr = posStr.split(",");
if(posArr.length == 2){
var lat = $.trim(posArr[0]);
var lng = $.trim(posArr[1]);
var latlng = new google.maps.LatLng(lat, lng);
setPosition(latlng);
return;
}
$(that).val("Invalid Position");
}
}
function showPicker(){
picker.fadeIn('fast');
google.maps.event.trigger(map, 'resize');
getCurrentPosition();
map.setCenter(marker.position);
}
$(this).focus(function(){
var address = $(that).val();
if(isLngLat(address)){
showPicker();
}
});
$(":input").focus(function(){
if($(this).attr('id') != $(that).attr('id')){
if($(picker).children(this).length == 0){
picker.fadeOut('fast');
}
}
});
function isLngLat(val){
var lngLatArr = val.split(",");
if(lngLatArr.length == 2){
if(isNaN(lngLatArr[0]) || isNaN(lngLatArr[1])){
return false;
}else{
return true;
}
}
return false;
}
function findAddress(){
var address = $(that).val();
if(address == ""){
alert("Please enter an address or Lng/Lat position.");
}else{
if(isLngLat(address)){
showPicker();
}else{
geocoder.geocode( {'address': address, 'region': 'uk'}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
setPosition(
results[0].geometry.location,
results[0].geometry.viewport
);
showPicker();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
$(that).focus();
}
}
$(searchButton).click(function(event){
findAddress();
event.stopPropagation();
});
$(that).keydown(function(event) {
if (event.keyCode == '13') { // enter
findAddress();
event.preventDefault();
}
});
$('html').click(function() {
picker.fadeOut('fast');
});
$(picker).click(function(event){
event.stopPropagation();
//$(that).focus();
});
$(this).click(function(event){
event.stopPropagation();
});
});
}
});
})(jQuery);