|
| 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