Last active
March 13, 2016 05:54
-
-
Save tareq89/1f0442400a9101425a2a to your computer and use it in GitHub Desktop.
Simple Map application, click on the map, it creates a marker and print the Geo Coordinate and address of that marker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <link type="javascript" href="https://code.jquery.com/jquery-2.1.4.min.js"> | |
| <style type="text/css"> | |
| html, body { height: 100%; margin: 0; padding: 0; } | |
| #map { height: 80%; } | |
| #latlongList { width: 80%; height: 60%; } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="map"></div> | |
| <div> | |
| <button onclick="reset()">Reset</button> | |
| </div> | |
| <div> | |
| <ol id="latlongList"></ol> | |
| </div> | |
| <script type="text/javascript"> | |
| var i = 0; | |
| var map; | |
| function initMap() { | |
| map = new google.maps.Map(document.getElementById('map'), { | |
| center: {lat: 23.778634, lng: 90.398083}, | |
| zoom: 15 | |
| }); | |
| google.maps.event.addListener(map, 'click', function(event) { | |
| placeMarker(event.latLng); | |
| console.log(event.latLng); | |
| var address = geocodePosition(event.latLng); | |
| }); | |
| function writeOnHtmlCallback(latLng, address) { | |
| var latlong = (latLng.lat().toString() + " , " + latLng.lng().toString()) + " , " + address; | |
| var node = document.createElement("li"); | |
| var textNode = document.createTextNode(latlong); | |
| node.appendChild(textNode); | |
| document.getElementById("latlongList").appendChild(node); | |
| }; | |
| function placeMarker(location) { | |
| new google.maps.Size(42,68); | |
| var marker = new google.maps.Marker({ | |
| position: location, | |
| map: map, | |
| label: (i++).toString() | |
| }); | |
| }; | |
| var geocoder = new google.maps.Geocoder(); | |
| function geocodePosition(pos) { | |
| geocoder.geocode({ | |
| latLng: pos | |
| }, function(responses) { | |
| if (responses && responses.length > 0) { | |
| var address = responses[0].formatted_address; | |
| writeOnHtmlCallback(pos, address) | |
| } else { | |
| return "no address"; | |
| } | |
| }); | |
| } | |
| } | |
| function reset () { | |
| placeMarker(null); | |
| $("#latlongList").empty(); | |
| } | |
| </script> | |
| <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment