Skip to content

Commit 7d47686

Browse files
committed
Add option to display city when address is search in the search input
1 parent 30de2a0 commit 7d47686

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

javascript.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
function initialize() {
22

3-
var mapOptions, map, marker, searchBox,
3+
var mapOptions, map, marker, searchBox, city,
44
infoWindow = '',
55
addressEl = document.querySelector( '#map-search' ),
66
latEl = document.querySelector( '.latitude' ),
77
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' );
1010

1111
mapOptions = {
1212
// How far the maps zooms in.
@@ -56,7 +56,7 @@ function initialize() {
5656
google.maps.event.addListener( searchBox, 'places_changed', function () {
5757
var places = searchBox.getPlaces(),
5858
bounds = new google.maps.LatLngBounds(),
59-
i, place, lat, long,
59+
i, place, lat, long, resultArray,
6060
addresss = places[0].formatted_address;
6161

6262
for( i = 0; place = places[i]; i++ ) {
@@ -67,11 +67,22 @@ function initialize() {
6767
map.fitBounds( bounds ); // Fit to the bound
6868
map.setZoom( 15 ); // This function sets the zoom to 15, meaning zooms to level 15.
6969
// console.log( map.getZoom() );
70+
7071
lat = marker.getPosition().lat();
7172
long = marker.getPosition().lng();
7273
latEl.value = lat;
7374
longEl.value = long;
7475

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+
7586
// Closes the previous info window if it already exists
7687
if ( infoWindow ) {
7788
infoWindow.close();

0 commit comments

Comments
 (0)