Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Javascript Geolocation API Project to Extract Country,State,City & Zip Code From Google Autocomplete

Posted on January 20, 2023

 

 

Welcome folks today in this blog post we will be using the geolocation api to extract the country,state,city and zip code from google autocomplete in browser. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make an index.html file and copy paste the following code

 

 

index.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=##apikey##&v=3.exp&sensor=false&libraries=places"></script>
<link
  rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
/>
<title>Extract Country,State,City & Zip Code From Location in Javascript</title>
  </head>
  <body>
<div class="container">
  <h1 class="text-center">Extract Country,State,City & Zip Code From Location in Javascript</h1>
  <br /><br />
<div class="form-group container">
  <label for="location">Enter Location: (Autocomplete)</label>
  <input type="text" id="searchTextField" class="form-control" />
</div>
<br />
<table class="table">
  <thead>
<tr>
  <th>Country</th>
  <th>State</th>
  <th>City</th>
  <th>Pin or Zip Code</th>
</tr>
  </thead>
  <tbody id="body">
  </tbody>
</table>
  </div>
</div>
  </body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script>
let places, input, address, city;
google.maps.event.addDomListener(window, "load", function () {
  var places = new google.maps.places.Autocomplete(
document.getElementById("searchTextField")
  );
  google.maps.event.addListener(places, "place_changed", function () {
var place = places.getPlace();
console.log(place)
var address = place.formatted_address;
var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();
var latlng = new google.maps.LatLng(latitude, longitude);
var geocoder = (geocoder = new google.maps.Geocoder());
geocoder.geocode({ latLng: latlng }, function (results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
  var address = results[0].formatted_address;
  var pin =
results[0].address_components[
  results[0].address_components.length - 1
].long_name;
  var country =
results[0].address_components[
  results[0].address_components.length - 2
].long_name;
  var state =
results[0].address_components[
  results[0].address_components.length - 3
].long_name;
  var city =
results[0].address_components[
  results[0].address_components.length - 4
].long_name;
console.log(country)
console.log(state)
console.log(city)
console.log(pin)
   let row = `
  
   <tr id="country">
  <td>${country}</td>
  <td>${state}</td>
   <td>${city}</td>
   <td>${pin}</td>
</tr>
  
  
   `
   $("#body").append(row)
}
  }
});
  });
});
  </script>

 

 

As you can see we are using the places api and geolocation api and here you need to replace your own api key from google cloud console and then we are displaying all these details inside the table.

 

 

 

 

 

 

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme