Commit 503c6fd9c3b8794a81ba52c31bac240723ffc9b3
1 parent
dd08c013
Exists in
master
and in
29 other branches
Don't replace address when there is no postal code
Also cleanup and refactor code
Showing
2 changed files
with
108 additions
and
113 deletions
Show diff stats
app/views/maps/_google_map.js.erb
@@ -5,27 +5,113 @@ var marker; | @@ -5,27 +5,113 @@ var marker; | ||
5 | var center; | 5 | var center; |
6 | var move = true; | 6 | var move = true; |
7 | var previousCenter; | 7 | var previousCenter; |
8 | +var mapZoom = 15; | ||
9 | +var delay_autocomplete = 500; | ||
8 | 10 | ||
9 | -function getAddress(latlng) { | 11 | +function pointToAddress(latlng) { |
10 | $('location-fields').addClassName("loading"); | 12 | $('location-fields').addClassName("loading"); |
11 | 13 | ||
12 | - if (latlng != null) { | ||
13 | - geocoder.geocode( {'latLng': latlng}, showAddress); | ||
14 | - } | 14 | + if (latlng == null) |
15 | + return; | ||
16 | + | ||
17 | + geocoder.geocode( {'latLng': latlng}, function(results, status) { | ||
18 | + if (status != google.maps.GeocoderStatus.OK) { | ||
19 | + alert("<%=_("Address not found, reason:")%>" + statusErrorMessage(status)); | ||
20 | + return; | ||
21 | + } | ||
22 | + | ||
23 | + var place = results[0]; | ||
24 | + | ||
25 | + $('location-fields').removeClassName("loading"); | ||
26 | + | ||
27 | + var position = marker.getPosition(); | ||
28 | + $('profile_data_lat').value = position.lat(); | ||
29 | + $('profile_data_lng').value = position.lng(); | ||
30 | + | ||
31 | + form = jQuery('#location-form')[0]; | ||
32 | + form.lat = marker.getPosition().lat(); | ||
33 | + form.lng = marker.getPosition().lng(); | ||
34 | + | ||
35 | + var components_len = place.address_components.size(); | ||
36 | + if (components_len < 2) | ||
37 | + return; | ||
38 | + | ||
39 | + var country_code = ""; | ||
40 | + var state = ""; | ||
41 | + var city = ""; | ||
42 | + var zip_code = ""; | ||
43 | + var route = ""; | ||
44 | + var number = ""; | ||
45 | + var sublocality = ""; | ||
46 | + var address = ""; | ||
47 | + | ||
48 | + var i; | ||
49 | + var has_postal_code = false; | ||
50 | + for (i=0; i < components_len; i++) { | ||
51 | + type = place.address_components[i].types[0]; | ||
52 | + if (type == 'postal_code') | ||
53 | + has_postal_code = true; | ||
54 | + } | ||
55 | + | ||
56 | + for (i=0; i < components_len; i++) { | ||
57 | + type = place.address_components[i].types[0]; | ||
58 | + value = place.address_components[i]; | ||
59 | + | ||
60 | + if (type == 'country') | ||
61 | + country_code = value.short_name; | ||
62 | + else if (type == 'administrative_area_level_1') | ||
63 | + state = value.long_name; | ||
64 | + else if (type == 'locality') | ||
65 | + city = value.long_name; | ||
66 | + else if (type == 'postal_code') | ||
67 | + zip_code = value.short_name; | ||
68 | + if (has_postal_code) { | ||
69 | + if (type == "route") | ||
70 | + route = value.long_name; | ||
71 | + else if (type == "street_number") | ||
72 | + number = value.short_name; | ||
73 | + else if (type == 'sublocality') | ||
74 | + sublocality = value.long_name; | ||
75 | + } | ||
76 | + } | ||
77 | + | ||
78 | + // build address | ||
79 | + if (route) { | ||
80 | + address = route; | ||
81 | + if (number) | ||
82 | + address = address + ', ' + number; | ||
83 | + if (sublocality && sublocality != city) | ||
84 | + address = address + ', ' + sublocality; | ||
85 | + } | ||
86 | + | ||
87 | + if (country_code) | ||
88 | + $('profile_data_country').value = country_code; | ||
89 | + if (state) | ||
90 | + $('profile_data_state').value = state; | ||
91 | + if (city) | ||
92 | + $('profile_data_city').value = city; | ||
93 | + if (zip_code) | ||
94 | + $('profile_data_zip_code').value = zip_code; | ||
95 | + if (address) | ||
96 | + $('profile_data_address').value = address; | ||
97 | + | ||
98 | + map.setCenter(marker.getPosition()); | ||
99 | + }); | ||
15 | } | 100 | } |
16 | 101 | ||
17 | -function codeAddress() { | 102 | +function addressToPoint() { |
18 | $('location-fields').addClassName("loading"); | 103 | $('location-fields').addClassName("loading"); |
19 | 104 | ||
20 | var country_option = $('profile_data_country').value; | 105 | var country_option = $('profile_data_country').value; |
21 | - var address = $('profile_data_address').value + "-" + $('profile_data_zip_code').value + "," + $('profile_data_city').value+ "-" + $('profile_data_state').value + "," + country_option; | 106 | + var address = $('profile_data_address').value + ", " + $('profile_data_zip_code').value + ", " |
107 | + + $('profile_data_city').value+ ", " + $('profile_data_state').value + ", " + country_option; | ||
22 | 108 | ||
23 | if (geocoder) { | 109 | if (geocoder) { |
24 | geocoder.geocode({ 'address': address}, function(results, status) { | 110 | geocoder.geocode({ 'address': address}, function(results, status) { |
25 | if (status == google.maps.GeocoderStatus.OK) { | 111 | if (status == google.maps.GeocoderStatus.OK) { |
26 | map.setCenter(results[0].geometry.location); | 112 | map.setCenter(results[0].geometry.location); |
27 | marker.setPosition(results[0].geometry.location); | 113 | marker.setPosition(results[0].geometry.location); |
28 | - getAddress(marker.getPosition()); | 114 | + pointToAddress(marker.getPosition()); |
29 | 115 | ||
30 | $('profile_data_lat').value = results[0].geometry.location.lat(); | 116 | $('profile_data_lat').value = results[0].geometry.location.lat(); |
31 | $('profile_data_lng').value = results[0].geometry.location.lng(); | 117 | $('profile_data_lng').value = results[0].geometry.location.lng(); |
@@ -33,17 +119,15 @@ function codeAddress() { | @@ -33,17 +119,15 @@ function codeAddress() { | ||
33 | enable_save(); | 119 | enable_save(); |
34 | } else { | 120 | } else { |
35 | $('location-fields').removeClassName("loading"); | 121 | $('location-fields').removeClassName("loading"); |
36 | - alert('<%=_("Address not found, reason:")%>' + translate_status(status)); | 122 | + alert('<%=_("Address not found, reason:")%>' + statusErrorMessage(status)); |
37 | } | 123 | } |
38 | }); | 124 | }); |
39 | } | 125 | } |
40 | 126 | ||
41 | - map.setZoom(11); | ||
42 | - | ||
43 | return false; | 127 | return false; |
44 | } | 128 | } |
45 | 129 | ||
46 | -function translate_status(status) | 130 | +function statusErrorMessage(status) |
47 | { | 131 | { |
48 | var translated_status = ''; | 132 | var translated_status = ''; |
49 | 133 | ||
@@ -59,132 +143,43 @@ function translate_status(status) | @@ -59,132 +143,43 @@ function translate_status(status) | ||
59 | return translated_status; | 143 | return translated_status; |
60 | } | 144 | } |
61 | 145 | ||
62 | -function getAddressData() { | ||
63 | - var text = ''; | ||
64 | - var fields = [ | ||
65 | - 'profile_data_country', | ||
66 | - 'profile_data_state', | ||
67 | - 'profile_data_city', | ||
68 | - 'profile_data_address', | ||
69 | - 'profile_data_zip_code' | ||
70 | - ]; | ||
71 | - for (var i = 0; i < fields.length; i++) { | ||
72 | - var field = fields[i]; | ||
73 | - if ($(field)) { | ||
74 | - text += $(field).value + " "; | ||
75 | - } | ||
76 | - } | ||
77 | - return text; | ||
78 | -} | ||
79 | - | ||
80 | -function showAddress(results, status) { | ||
81 | - if (status == google.maps.GeocoderStatus.OK) { | ||
82 | - updateFields(results[0]); | ||
83 | - map.setCenter(marker.getPosition()); | ||
84 | - } else { | ||
85 | - alert("<%=_("Address not found, reason:")%>" + translate_status(status)); | ||
86 | - } | ||
87 | -} | ||
88 | - | ||
89 | -function updateFields(place) { | ||
90 | - $('location-fields').removeClassName("loading"); | ||
91 | - | ||
92 | - var position = marker.getPosition(); | ||
93 | - $('profile_data_lat').value = position.lat(); | ||
94 | - $('profile_data_lng').value = position.lng(); | ||
95 | - | ||
96 | - form = jQuery('#location-form')[0]; | ||
97 | - form.lat = marker.getPosition().lat(); | ||
98 | - form.lng = marker.getPosition().lng(); | ||
99 | - | ||
100 | - var components_len = place.address_components.size(); | ||
101 | - if (components_len < 2) | ||
102 | - return; | ||
103 | - | ||
104 | - var address = ""; | ||
105 | - var zip_code = ""; | ||
106 | - var city = ""; | ||
107 | - var state = ""; | ||
108 | - var country_code = ""; | ||
109 | - | ||
110 | - var i; | ||
111 | - for(i=0; i < components_len; i++) { | ||
112 | - type = place.address_components[i].types[0]; | ||
113 | - value = place.address_components[i]; | ||
114 | - if (type == 'country') | ||
115 | - country_code = value.short_name; | ||
116 | - else if (type == 'administrative_area_level_1') | ||
117 | - state = value.long_name; | ||
118 | - else if (type == 'locality') | ||
119 | - city = value.long_name; | ||
120 | - else if (type == 'sublocality') | ||
121 | - address = value.long_name + "-" + address; | ||
122 | - else if (type == "route") | ||
123 | - address = value.long_name + address; | ||
124 | - else if (type == "street_number") | ||
125 | - address = address + "," + value.short_name; | ||
126 | - else if (type == 'postal_code') | ||
127 | - zip_code = value.short_name; | ||
128 | - } | ||
129 | - | ||
130 | - if (country_code) | ||
131 | - $('profile_data_country').value = country_code; | ||
132 | - if (state) | ||
133 | - $('profile_data_state').value = state; | ||
134 | - if (address) | ||
135 | - $('profile_data_address').value = address; | ||
136 | - if (city) | ||
137 | - $('profile_data_city').value = city; | ||
138 | - if (zip_code) | ||
139 | - $('profile_data_zip_code').value = zip_code; | ||
140 | -} | ||
141 | - | ||
142 | - | ||
143 | -function initialize_map() { | 146 | +function initializeMap() { |
144 | geocoder = new google.maps.Geocoder(); | 147 | geocoder = new google.maps.Geocoder(); |
145 | 148 | ||
146 | var lat = <%= profile.lat || 'false' %>; | 149 | var lat = <%= profile.lat || 'false' %>; |
147 | var lng = <%= profile.lng || 'false' %>; | 150 | var lng = <%= profile.lng || 'false' %>; |
148 | - | ||
149 | if ( !(lat && lng) ) { | 151 | if ( !(lat && lng) ) { |
150 | lat = -15.7605361485013; | 152 | lat = -15.7605361485013; |
151 | lng = -47.933349609375; | 153 | lng = -47.933349609375; |
152 | } | 154 | } |
153 | 155 | ||
154 | - var latlng = new google.maps.LatLng(lat,lng); | ||
155 | - | ||
156 | - var myOptions = { | ||
157 | - zoom: 8, | ||
158 | - center: latlng, | 156 | + var center = new google.maps.LatLng(lat,lng);; |
157 | + map = new google.maps.Map(document.getElementById("location-map"), { | ||
158 | + zoom: mapZoom, | ||
159 | + center: center, | ||
159 | mapTypeId: google.maps.MapTypeId.HYBRID | 160 | mapTypeId: google.maps.MapTypeId.HYBRID |
160 | - } | ||
161 | - | ||
162 | - center = latlng; | ||
163 | - | ||
164 | - map = new google.maps.Map(document.getElementById("location-map"), myOptions); | 161 | + }); |
165 | 162 | ||
166 | marker = new google.maps.Marker({ | 163 | marker = new google.maps.Marker({ |
167 | - position: center, | ||
168 | - map: map, | ||
169 | - draggable: true | 164 | + position: center, |
165 | + map: map, | ||
166 | + draggable: true | ||
170 | }); | 167 | }); |
171 | 168 | ||
172 | google.maps.event.addListener(marker, "dragend", function() { | 169 | google.maps.event.addListener(marker, "dragend", function() { |
173 | move = false; | 170 | move = false; |
174 | - getAddress(marker.getPosition()); | 171 | + pointToAddress(marker.getPosition()); |
175 | map.setCenter(marker.getPosition()); | 172 | map.setCenter(marker.getPosition()); |
176 | enable_save(); | 173 | enable_save(); |
177 | }); | 174 | }); |
178 | 175 | ||
179 | } | 176 | } |
180 | 177 | ||
181 | -window.onload = initialize_map; | ||
182 | - | ||
183 | -var delay_autocomplete = 500; | ||
184 | - | ||
185 | jQuery.noConflict(); | 178 | jQuery.noConflict(); |
186 | jQuery(document).ready(function () { | 179 | jQuery(document).ready(function () { |
187 | 180 | ||
181 | + initializeMap(); | ||
182 | + | ||
188 | jQuery.widget( "custom.catcomplete",jQuery.ui.autocomplete, { | 183 | jQuery.widget( "custom.catcomplete",jQuery.ui.autocomplete, { |
189 | _renderMenu: function( ul, items ) { | 184 | _renderMenu: function( ul, items ) { |
190 | var self = this, | 185 | var self = this, |
app/views/maps/edit_location.rhtml
@@ -12,7 +12,7 @@ | @@ -12,7 +12,7 @@ | ||
12 | <%= labelled_form_field _('ZIP code'), text_field(:profile_data, :zip_code) %> | 12 | <%= labelled_form_field _('ZIP code'), text_field(:profile_data, :zip_code) %> |
13 | <%= labelled_form_field _('Address (street and number)'), text_field(:profile_data, :address) %> | 13 | <%= labelled_form_field _('Address (street and number)'), text_field(:profile_data, :address) %> |
14 | <% button_bar do %> | 14 | <% button_bar do %> |
15 | - <%= button_to_function :search, _('Locate in the map'), "codeAddress()", :title => _("Locate the address informed above in the map below (note that you'll probably need to adjust the marker to get a precise position)") %> | 15 | + <%= button_to_function :search, _('Locate in the map'), "addressToPoint()", :title => _("Locate the address informed above in the map below (note that you'll probably need to adjust the marker to get a precise position)") %> |
16 | <%= submit_button 'save', _('Save') %> | 16 | <%= submit_button 'save', _('Save') %> |
17 | <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %> | 17 | <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %> |
18 | <% end %> | 18 | <% end %> |