How to use Google Maps API for GPS coordinate inputs
For a recent project I had to create a web interface for users to input and store GPS coordinates. To make the process more user friendly, I thought of integrating the Google Maps API to allow the users to directly pinpoint the location instead of making them manually input the latitude and longitude values.
The full code and a detailed breakdown of the code is given below.
LIVE DEMOThe Code
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 |
<div id="wrapper"> <label for="lat">Latitude</label> <input name="lat" id="lat-input" type="number" value="3.080410" placeholder="Latitude" step="0.000001" required> <label for="lng">Longitude</label> <input name="lng" id="lng-input" type="number" value="101.532111" placeholder="Longitude" step="0.000001" required> <!-- map would be loaded here --> <div id="map-canvas"></div> </div> <!-- The JavaScript --> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> <script text="type/javascript"> //function to validate a coordinate //http://stackoverflow.com/questions/11475146/javascript-regex-to-validate-gps-coordinates function check_lat_lon(lat, lon){ ck_lat = /^(-?[1-8]?\d(?:\.\d{1,18})?|90(?:\.0{1,18})?)$/; ck_lon = /^(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,18})?|180(?:\.0{1,18})?)$/; var validLat = ck_lat.test(lat); var validLon = ck_lon.test(lon); return (validLat && validLon); } var myMap; //map variable var myMarker; //the position selection marker //setting up map function initialize() { //get default values var lat = document.getElementById('lat-input').value; var lng = document.getElementById('lng-input').value; //if invalid values given default to 0,0 if (!check_lat_lon(lat, lng)) { lat = 0.0; lng = 0.0; } var myLatlng = new google.maps.LatLng(lat, lng); //initial position var mapOptions = { zoom: 13, //set zoom level center: myLatlng, //center map to initial position disableDefaultUI: false, //display the map UI streetViewControl: false, //hide street view panControl: true, //show pan controls }; //initialize the map inside #map-canvas div myMap = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); //initialize the marker to select the location myMarker = new google.maps.Marker({ animation: google.maps.Animation.DROP, //animate the marker in position: myLatlng, //set the marker to initialize position draggable: true, //make the marker draggable map: myMap, //load the marker in the map }); //update the input fields after the marker has been dragged google.maps.event.addListener(myMarker, 'dragend', function(evt){ document.getElementById('lat-input').value = evt.latLng.lat().toFixed(6); document.getElementById('lng-input').value = evt.latLng.lng().toFixed(6); }); //move the marker to new location when input fields updated document.getElementById('lat-input').addEventListener('input', moveMarker); document.getElementById('lng-input').addEventListener('input', moveMarker); } //move marker when user changes text //function to move the maker function moveMarker() { //get the specified coordinate values var lat = document.getElementById('lat-input').value; var lng = document.getElementById('lng-input').value; //move the marker only if the values are valid if ( check_lat_lon(lat, lng) ) { myMarker.setPosition( new google.maps.LatLng( lat, lng ) ); myMap.panTo( new google.maps.LatLng( lat, lng ) ); } } //initialize the map after the DOM has been loaded google.maps.event.addDomListener(window, 'load', initialize); </script> |
The Breakdown
1 2 3 4 5 |
<label for="lat">Latitude</label> <input name="lat" id="lat-input" type="number" value="3.080410" placeholder="Latitude" step="0.000001" required> <label for="lng">Longitude</label> <input name="lng" id="lng-input" type="number" value="101.532111" placeholder="Longitude" step="0.000001" required> |
1 |
<div id="map-canvas"></div> |
1 |
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> |
1 2 3 4 5 6 7 |
function check_lat_lon(lat, lon){ ck_lat = /^(-?[1-8]?\d(?:\.\d{1,18})?|90(?:\.0{1,18})?)$/; ck_lon = /^(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,18})?|180(?:\.0{1,18})?)$/; var validLat = ck_lat.test(lat); var validLon = ck_lon.test(lon); return (validLat && validLon); } |
1 2 |
var myMap; //map variable var myMarker; //the position selection marker |
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 |
function initialize() { //get default values var lat = document.getElementById('lat-input').value; var lng = document.getElementById('lng-input').value; //if invalid values given default to 0,0 if (!check_lat_lon(lat, lng)) { lat = 0.0; lng = 0.0; } var myLatlng = new google.maps.LatLng(lat, lng); //initial position var mapOptions = { zoom: 13, //set zoom level center: myLatlng, //center map to initial position disableDefaultUI: false, //display the map UI streetViewControl: false, //hide street view panControl: true, //show pan controls }; //initialize the map inside #map-canvas div myMap = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); //initialize the marker to select the location myMarker = new google.maps.Marker({ animation: google.maps.Animation.DROP, //animate the marker in position: myLatlng, //set the marker to initialize position draggable: true, //make the marker draggable map: myMap, //load the marker in the map }); //update the input fields after the marker has been dragged google.maps.event.addListener(myMarker, 'dragend', function(evt){ document.getElementById('lat-input').value = evt.latLng.lat().toFixed(6); document.getElementById('lng-input').value = evt.latLng.lng().toFixed(6); }); //move the marker to new location when input fields updated document.getElementById('lat-input').addEventListener('input', moveMarker); document.getElementById('lng-input').addEventListener('input', moveMarker); } |
1 2 3 4 5 6 7 8 9 10 11 |
//get default values var lat = document.getElementById('lat-input').value; var lng = document.getElementById('lng-input').value; //if invalid values given default to 0,0 if (!check_lat_lon(lat, lng)) { lat = 0.0; lng = 0.0; } var myLatlng = new google.maps.LatLng(lat, lng); //initial position |
1 2 3 4 5 6 7 8 9 10 |
var mapOptions = { zoom: 13, //set zoom level center: myLatlng, //center map to initial position disableDefaultUI: false, //display the map UI streetViewControl: false, //hide street view panControl: true, //show pan controls }; //initialize the map inside #map-canvas div myMap = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); |
1 2 3 4 5 6 7 |
//initialize the marker to select the location myMarker = new google.maps.Marker({ animation: google.maps.Animation.DROP, //animate the marker in position: myLatlng, //set the marker to initialize position draggable: true, //make the marker draggable map: myMap, //load the marker in the map }); |
1 2 3 4 5 |
//update the input fields after the marker has been dragged google.maps.event.addListener(myMarker, 'dragend', function(evt){ document.getElementById('lat-input').value = evt.latLng.lat().toFixed(6); document.getElementById('lng-input').value = evt.latLng.lng().toFixed(6); }); |
1 2 3 |
//move the marker to new location when input fields updated document.getElementById('lat-input').addEventListener('input', moveMarker); document.getElementById('lng-input').addEventListener('input', moveMarker); |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//move marker when user changes text //function to move the maker function moveMarker() { //get the specified coordinate values var lat = document.getElementById('lat-input').value; var lng = document.getElementById('lng-input').value; //move the marker only if the values are valid if ( check_lat_lon(lat, lng) ) { myMarker.setPosition( new google.maps.LatLng( lat, lng ) ); myMap.panTo( new google.maps.LatLng( lat, lng ) ); } } |
1 2 |
//initialize the map after the DOM has been loaded google.maps.event.addDomListener(window, 'load', initialize); |
Lastly if you notice any weird glitches in the map UI, add this to your css.
1 2 3 |
#map-canvas img { max-width: none; } |
I hope you find this useful. Let me know in the comments below if you have any issues or questions.