Skip to content

Commit 66864a8

Browse files
Geo-location API
1 parent 37b96a8 commit 66864a8

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

css/style.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.block{
2+
float: left;
3+
width: 100%;
4+
}
5+
.main-wrapper{
6+
/* position: relative;
7+
max-height: 100vh;
8+
min-height: 100vh;
9+
overflow: hidden; */
10+
}
11+
.map-container{
12+
min-height: 400px;
13+
}

index.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="UTF-8">
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
6+
<title>Geo location API</title>
7+
</head>
8+
<body>
9+
<section class="block main-wrapper">
10+
<div class="container">
11+
<div class="row">
12+
<div class="col s8 offset-s2 m8 offset-m2 l6 xl6 offset-l3 offset-xl3">
13+
<label for="">Address</label>
14+
<div class="input-field">
15+
<textarea id="address" rows="3" class="materialize-textarea"></textarea>
16+
</div>
17+
</div>
18+
<div class="col xl12">
19+
<h5 class="center-align">Map</h5>
20+
<div class="map-container block" id='map'>
21+
</div>
22+
</div>
23+
</div>
24+
</div>
25+
</section>
26+
27+
<!-- Compiled and minified CSS -->
28+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css">
29+
<link rel="stylesheet" href="css/style.css">
30+
<script
31+
src="https://code.jquery.com/jquery-3.3.1.min.js"
32+
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
33+
crossorigin="anonymous"></script>
34+
35+
<!-- Compiled and minified JavaScript -->
36+
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script>
37+
<script async defer
38+
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCm_GrSti6BA79AerJkEcrmCusdDhDCsko">
39+
</script>
40+
<script src="scripts/scripts.js"></script>
41+
</body>
42+
</html>

scripts/scripts.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* ===========
2+
Function to get the location coordinates using geolocation api
3+
=========== */
4+
var getPosition = (position) => {
5+
var locationObj = {};
6+
locationObj.lat = position.coords['latitude'];
7+
locationObj.lng = position.coords['longitude'];
8+
// Call the google api to create the map
9+
initMap(locationObj);
10+
// call the getHumanReadableAddress function to get the address in human readable format
11+
getHumanReadableAddress(position.coords['latitude'], position.coords['longitude']);
12+
}
13+
14+
/* ===========
15+
Function that gets triggered when we have the error while fetch the co-ordinates
16+
using HTML5 geoolocation api
17+
=========== */
18+
var locationNotReceived = (positionError) => {
19+
console.log(positionError);
20+
}
21+
22+
/* ===========
23+
Function to get the address using the coordinates
24+
=========== */
25+
var getHumanReadableAddress = (latitude, longitude) => {
26+
var tempURL = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&key=AIzaSyCm_GrSti6BA79AerJkEcrmCusdDhDCsko`;
27+
$.ajax({
28+
url: tempURL,
29+
success: function (result) {
30+
if (result.status == 'OK') {
31+
$('#address').val(result['results'][0]['formatted_address']);
32+
}
33+
}
34+
}
35+
);
36+
}
37+
38+
/* ===========
39+
Function to create the map with an id selector (target the id and generate the map)
40+
=========== */
41+
var createMapWithCoordinates = (selector, latLongObj) => {
42+
var map = new google.maps.Map(
43+
document.getElementById(selector),
44+
{
45+
zoom: 15, center: latLongObj
46+
});
47+
marker = new google.maps.Marker({
48+
position: latLongObj,
49+
map: map,
50+
title: 'Default Marker',
51+
draggable: true
52+
});
53+
}
54+
55+
/* ===========
56+
Function to initialize the map on page load
57+
=========== */
58+
var initMap = (locationObj) => {
59+
// create a map using the co-ordinates and the target id
60+
createMapWithCoordinates('map', locationObj);
61+
// The marker, positioned at the given lat and long
62+
google.maps.event.addListener(marker, 'dragend', function (event) {
63+
var tempObj = {};
64+
tempObj.lat = this.position.lat();
65+
tempObj.lng = this.position.lng();
66+
getHumanReadableAddress(this.position.lat(), this.position.lng());
67+
});
68+
}
69+
70+
/* ===========
71+
HTML5 geolocation api to get the coordinates
72+
=========== */
73+
if (navigator.geolocation) {
74+
navigator.geolocation.getCurrentPosition(getPosition, locationNotReceived);
75+
} else {
76+
alert('Geolocation is not supported in your browser');
77+
}

0 commit comments

Comments
 (0)