@@ -66,11 +66,19 @@ var Codeweek = window.Codeweek || {};
66
66
map = new google . maps . Map ( document . getElementById ( 'view-event-map' ) , mapOptions ) ;
67
67
}
68
68
69
- function updateAddress ( new_position ) {
69
+ function updateCountrySelection ( country ) {
70
+ var choice = document . getElementById ( 'id_country' ) ;
71
+ selectItemByValue ( choice , country ) ;
72
+ }
73
+
74
+ function updateAddress ( new_latLng ) {
70
75
geocoder = new google . maps . Geocoder ( ) ;
71
- geocoder . geocode ( { 'latLng' : new_position } , function ( results , status ) {
76
+ geocoder . geocode ( { 'latLng' : new_latLng } , function ( results , status ) {
72
77
if ( status === google . maps . GeocoderStatus . OK ) {
73
78
document . getElementById ( "autocomplete" ) . value = results [ 0 ] . formatted_address ;
79
+ // the last item in the geocoder for latLng results array is the country
80
+ var country = results [ results . length - 1 ] . address_components [ 0 ] . short_name
81
+ updateCountrySelection ( country ) ;
74
82
}
75
83
} ) ;
76
84
}
@@ -107,30 +115,22 @@ var Codeweek = window.Codeweek || {};
107
115
}
108
116
}
109
117
110
- function fillInAddress ( ) {
111
- var i ,
112
- component ,
113
- choice ,
114
- place = autocomplete . getPlace ( ) ,
115
- components = place . address_components ,
116
- country = null ,
117
- output = autocomplete . getPlace ( ) . geometry . location ,
118
- outputLat = output . lat ( ) ,
119
- outputLng = output . lng ( ) ,
120
- locLatlng = new google . maps . LatLng ( outputLat , outputLng ) ;
121
-
122
- document . getElementById ( "id_geoposition_0" ) . value = outputLat ;
123
- document . getElementById ( "id_geoposition_1" ) . value = outputLng ;
124
-
125
- createMarker ( locLatlng ) ;
126
- for ( i = 0 ; component = components [ i ] ; i = i + 1 ) {
127
- if ( component . types [ 0 ] === 'country' ) {
128
- country = component . short_name ;
129
- choice = document . getElementById ( 'id_country' ) ;
130
- selectItemByValue ( choice , country ) ;
118
+ function listenForPastedAddress ( ) {
119
+ var address_field = document . getElementById ( 'autocomplete' ) ;
120
+ address_field . addEventListener ( 'focusout' , function ( ) {
121
+ var address_value = address_field . value ;
122
+ if ( address_value ) {
123
+ getAddress ( address_value ) ;
131
124
}
132
- }
125
+ } , true ) ;
126
+ }
133
127
128
+ function fillInAddress ( ) {
129
+ // geoLatLng contains the Google Maps geocoded latitude, longitude
130
+ var geoLatLng = autocomplete . getPlace ( ) . geometry . location ;
131
+
132
+ createMarker ( geoLatLng ) ;
133
+ updateAddress ( geoLatLng ) ;
134
134
}
135
135
136
136
function auto_complete ( ) {
@@ -155,6 +155,7 @@ var Codeweek = window.Codeweek || {};
155
155
var updated_location = results [ 0 ] . geometry . location ;
156
156
createMarker ( updated_location ) ;
157
157
updateLatLng ( updated_location . lat ( ) , updated_location . lng ( ) ) ;
158
+ updateAddress ( updated_location ) ;
158
159
}
159
160
} ) ;
160
161
}
@@ -165,7 +166,7 @@ var Codeweek = window.Codeweek || {};
165
166
createMap ( initialCenter , 4 ) ;
166
167
auto_complete ( ) ;
167
168
getAddress ( address ) ;
168
-
169
+ listenForPastedAddress ( ) ;
169
170
}
170
171
171
172
var add = function ( address ) {
0 commit comments