_google_map.js.erb
6.45 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var geocoder;
var map;
var marker;
var center;
var move = true;
var previousCenter;
var mapZoom = 15;
var delay_autocomplete = 500;
function pointToAddress(latlng) {
jQuery('#location-fields').addClass("loading");
if (latlng == null)
return;
geocoder.geocode( {'latLng': latlng}, function(results, status) {
if (status != google.maps.GeocoderStatus.OK) {
alert("<%=_("Address not found, reason:")%>" + statusErrorMessage(status));
return;
}
var place = results[0];
jQuery('#location-fields').removeClass("loading");
var position = marker.getPosition();
jQuery('#profile_data_lat').val(position.lat());
jQuery('#profile_data_lng').val(position.lng());
form = jQuery('#location-form')[0];
form.lat = marker.getPosition().lat();
form.lng = marker.getPosition().lng();
var components_len = place.address_components.size();
if (components_len < 2)
return;
var country_code = "";
var state = "";
var city = "";
var zip_code = "";
var route = "";
var number = "";
var sublocality = "";
var address = "";
var i;
var has_postal_code = false;
for (i=0; i < components_len; i++) {
type = place.address_components[i].types[0];
if (type == 'postal_code')
has_postal_code = true;
}
for (i=0; i < components_len; i++) {
type = place.address_components[i].types[0];
value = place.address_components[i];
if (type == 'country')
country_code = value.short_name;
else if (type == 'administrative_area_level_1')
state = value.long_name;
else if (type == 'locality')
city = value.long_name;
else if (type == 'postal_code')
zip_code = value.short_name;
if (has_postal_code) {
if (type == "route")
route = value.long_name;
else if (type == "street_number")
number = value.short_name;
else if (type == 'sublocality')
sublocality = value.long_name;
}
}
// build address
if (route) {
address = route;
if (number)
address = address + ', ' + number;
if (sublocality && sublocality != city)
address = address + ', ' + sublocality;
}
if (country_code)
jQuery('#profile_data_country').val(country_code);
if (state)
jQuery('#profile_data_state').val(state);
if (city)
jQuery('#profile_data_city').val(city);
if (zip_code)
jQuery('#profile_data_zip_code').val(zip_code);
if (address)
jQuery('#profile_data_address').val(address);
map.setCenter(marker.getPosition());
});
}
function addressToPoint() {
jQuery('#location-fields').addClass("loading");
var country_option = jQuery('#profile_data_country').val();
var address = jQuery('#profile_data_address').val() + ", " + jQuery('#profile_data_zip_code').val() + ", "
+ jQuery('#profile_data_city').val() + ", " + jQuery('#profile_data_state').val() + ", " + country_option;
if (geocoder) {
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker.setPosition(results[0].geometry.location);
pointToAddress(marker.getPosition());
jQuery('#profile_data_lat').val(results[0].geometry.location.lat());
jQuery('#profile_data_lng').val(results[0].geometry.location.lng());
jQuery('#location-fields').removeClass("loading");
enable_save();
} else {
jQuery('#location-fields').removeClass("loading");
alert('<%=_("Address not found, reason:")%>' + statusErrorMessage(status));
}
});
}
return false;
}
function statusErrorMessage(status)
{
var translated_status = '';
if (google.maps.GeocoderStatus.INVALID_REQUEST == status)
translated_status = '<%= _('Invalid address') %>';
else if (google.maps.GeocoderStatus.REQUEST_DENIED == status)
translated_status = '<%= _('Request denied') %>';
else if (google.maps.GeocoderStatus.OVER_QUERY_LIMIT == status)
translated_status = '<%= _('Over query limit') %>';
else if (google.maps.GeocoderStatus.ZERO_RESULTS == status)
translated_status = "<%= _('Address do not exist') %>";
return translated_status;
}
function initializeMap() {
geocoder = new google.maps.Geocoder();
var lat = <%= profile.lat || 'false' %>;
var lng = <%= profile.lng || 'false' %>;
if ( !(lat && lng) ) {
lat = -15.7605361485013;
lng = -47.933349609375;
}
var center = new google.maps.LatLng(lat,lng);;
map = new google.maps.Map(document.getElementById("location-map"), {
zoom: mapZoom,
center: center,
mapTypeId: google.maps.MapTypeId.HYBRID
});
marker = new google.maps.Marker({
position: center,
map: map,
draggable: true
});
google.maps.event.addListener(marker, "dragend", function() {
move = false;
pointToAddress(marker.getPosition());
map.setCenter(marker.getPosition());
enable_save();
});
}
jQuery(document).ready(function () {
initializeMap();
jQuery.widget( "custom.catcomplete",jQuery.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var self = this,
currentCategory = "";
jQuery.each( items, function( index, item ) {
if ( item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
currentCategory = item.category;
}
self._renderItem( ul, item );
});
}
});
jQuery("#profile_data_city").catcomplete({
source: "../maps/search_city",
minLength: 3,
delay: delay_autocomplete,
select: function( event, ui ) { jQuery('#profile_data_state').val( ui.item ? ui.item.category : this.value ); }
});
jQuery("#profile_data_state").autocomplete({
source: "../maps/search_state",
minLength: 3,
delay: delay_autocomplete
});
jQuery("#profile_data_city").keyup(function(){
disable_save();
});
jQuery("#profile_data_state").keyup(function(){
disable_save();
});
jQuery("#profile_data_country").change(function(){
disable_save();
});
});
function disable_save()
{
jQuery('input[type="submit"]').attr("disabled", "true");
jQuery('input[type="submit"]').val('<%=_("Localize before save")%>');
jQuery('input[type="submit"]').addClass('disabled');
}
function enable_save()
{
jQuery('input[type="submit"]').removeAttr("disabled");
jQuery('input[type="submit"]').val('<%=_("Save")%>');
jQuery('input[type="submit"]').removeClass('disabled');
}