Skip to content

Commit f32216f

Browse files
committed
Add option to display city in a separate input field so that it can be saved in the database separately
1 parent 5e4c189 commit f32216f

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
# google-maps-javascript-api
2+
3+
# Step-1-GET API KEY
4+
https://console.cloud.google.com/apis/dashboard
5+
https://developers.google.com/maps/documentation/javascript/tutorial
6+
https://developers.google.com/maps/documentation/javascript/get-api-key
7+
8+
# Step-2 Create Project
9+
When logged in to your google api dashboard Click on Credentials and Create Credentials ( Create Credentials > Api key ) for the above API
10+
get the API key
11+
12+
#Step-3 ENABLE APIS:
13+
Go to Api Library on https://console.cloud.google.com/apis/library and active the below API libraries:
14+
1-Places API
15+
2-Geocoding API
16+
3-Google Maps API
17+
18+
You can watch the demo on https://www.youtube.com/watch?v=yhhkNtdg5x0&list=PLD8nQCAhR3tT9dU8JKLpG3av-WMQGPPFP
19+
For any help please contact me on http://imransayed.com

index.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<label for="">Address: <input id="map-search" class="controls" type="text" placeholder="Search Box" size="104"></label><br>
1313
<label for="">Lat: <input type="text" class="latitude"></label>
1414
<label for="">Long: <input type="text" class="longitude"></label>
15+
<label for="">City <input type="text" class="reg-input-city" placeholder="City"></label>
1516
1617
<div id="map-canvas"></div>
1718

javascript.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
function initialize() {
22

33
var mapOptions, map, marker, searchBox,
4-
infoWindow = '';
4+
infoWindow = '',
55
addressEl = document.querySelector( '#map-search' ),
66
latEl = document.querySelector( '.latitude' ),
77
longEl = document.querySelector( '.longitude' ),
8-
element = document.getElementById( 'map-canvas' );
8+
element = document.getElementById( 'map-canvas' ),
9+
city = document.querySelector( '.reg-input-city' );
910

1011
mapOptions = {
1112
// How far the maps zooms in.
1213
zoom: 8,
1314
// Current Lat and Long position of the pin/
14-
center: new google.maps.LatLng( -34.397, 150.644 ),
15+
center: new google.maps.LatLng( 18.5204, 73.8567 ),
1516
// center : {
1617
// lat: -34.397,
1718
// lng: 150.644
@@ -86,11 +87,11 @@ function initialize() {
8687
} );
8788

8889

89-
/**
90+
/**
9091
* Finds the new position of the marker when the marker is dragged.
9192
*/
9293
google.maps.event.addListener( marker, "dragend", function ( event ) {
93-
var lat, long, address;
94+
var lat, long, address, resultArray, citi;
9495

9596
console.log( 'i am dragged' );
9697
lat = marker.getPosition().lat();
@@ -100,6 +101,16 @@ function initialize() {
100101
geocoder.geocode( { latLng: marker.getPosition() }, function ( result, status ) {
101102
if ( 'OK' === status ) { // This line can also be written like if ( status == google.maps.GeocoderStatus.OK ) {
102103
address = result[0].formatted_address;
104+
resultArray = result[0].address_components;
105+
106+
// Get the city and set the city input value to the one selected
107+
for( var i = 0; i < resultArray.length; i++ ) {
108+
if ( resultArray[ i ].types[0] && 'administrative_area_level_2' === resultArray[ i ].types[0] ) {
109+
citi = resultArray[ i ].long_name;
110+
console.log( citi );
111+
city.value = citi;
112+
}
113+
}
103114
addressEl.value = address;
104115
latEl.value = lat;
105116
longEl.value = long;
@@ -125,4 +136,4 @@ function initialize() {
125136
});
126137

127138

128-
}
139+
}

0 commit comments

Comments
 (0)