1
1
function initialize ( ) {
2
2
3
- var mapOptions , map , marker , searchBox ,
3
+ var mapOptions , map , marker , searchBox , city ,
4
4
infoWindow = '' ,
5
5
addressEl = document . querySelector ( '#map-search' ) ,
6
6
latEl = document . querySelector ( '.latitude' ) ,
7
7
longEl = document . querySelector ( '.longitude' ) ,
8
- element = document . getElementById ( 'map-canvas' ) ,
9
- city = document . querySelector ( '.reg-input-city' ) ;
8
+ element = document . getElementById ( 'map-canvas' ) ;
9
+ city = document . querySelector ( '.reg-input-city' ) ;
10
10
11
11
mapOptions = {
12
12
// How far the maps zooms in.
@@ -56,7 +56,7 @@ function initialize() {
56
56
google . maps . event . addListener ( searchBox , 'places_changed' , function ( ) {
57
57
var places = searchBox . getPlaces ( ) ,
58
58
bounds = new google . maps . LatLngBounds ( ) ,
59
- i , place , lat , long ,
59
+ i , place , lat , long , resultArray ,
60
60
addresss = places [ 0 ] . formatted_address ;
61
61
62
62
for ( i = 0 ; place = places [ i ] ; i ++ ) {
@@ -67,11 +67,22 @@ function initialize() {
67
67
map . fitBounds ( bounds ) ; // Fit to the bound
68
68
map . setZoom ( 15 ) ; // This function sets the zoom to 15, meaning zooms to level 15.
69
69
// console.log( map.getZoom() );
70
+
70
71
lat = marker . getPosition ( ) . lat ( ) ;
71
72
long = marker . getPosition ( ) . lng ( ) ;
72
73
latEl . value = lat ;
73
74
longEl . value = long ;
74
75
76
+ resultArray = places [ 0 ] . address_components ;
77
+
78
+ // Get the city and set the city input value to the one selected
79
+ for ( var i = 0 ; i < resultArray . length ; i ++ ) {
80
+ if ( resultArray [ i ] . types [ 0 ] && 'administrative_area_level_2' === resultArray [ i ] . types [ 0 ] ) {
81
+ citi = resultArray [ i ] . long_name ;
82
+ city . value = citi ;
83
+ }
84
+ }
85
+
75
86
// Closes the previous info window if it already exists
76
87
if ( infoWindow ) {
77
88
infoWindow . close ( ) ;
0 commit comments