Skip to content

Instantly share code, notes, and snippets.

@laxman954
Last active March 17, 2017 12:49
Show Gist options
  • Save laxman954/baae8f8bd88c1df34d98677b856a348e to your computer and use it in GitHub Desktop.
Save laxman954/baae8f8bd88c1df34d98677b856a348e to your computer and use it in GitHub Desktop.

Revisions

  1. laxman954 revised this gist Mar 17, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions current_location.html
    Original file line number Diff line number Diff line change
    @@ -49,6 +49,7 @@
    }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
    if (results[1]) {
    document.getElementById('location_id').innerHTML = "<h1>"+results[1].formatted_address+"<h1>";
    var popOpts = {
    content : results[1].formatted_address,
    position : googlePos
    @@ -95,6 +96,7 @@

    </head>
    <body onload="geoloc()">
    <div id="location_id"></div>
    <p id = 'mapdiv'></p>
    <button onclick="stopWatch()">
    Stop
  2. laxman954 created this gist Mar 17, 2017.
    103 changes: 103 additions & 0 deletions current_location.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>Sample Map</title>
    <style>
    #mapdiv {
    margin: 0;
    padding: 0;
    width: 500px;
    height: 500px;
    }
    </style>

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
    <script>
    var watchId = null;
    function geoloc() {
    if (navigator.geolocation) {
    var optn = {
    enableHighAccuracy : true,
    timeout : Infinity,
    maximumAge : 0
    };
    watchId = navigator.geolocation.watchPosition(showPosition, showError, optn);
    } else {
    alert('Geolocation is not supported in your browser');
    }
    }

    function showPosition(position) {
    var googlePos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    var mapOptions = {
    zoom : 12,
    center : googlePos,
    mapTypeId : google.maps.MapTypeId.ROADMAP
    };
    var mapObj = document.getElementById('mapdiv');
    var googleMap = new google.maps.Map(mapObj, mapOptions);
    var markerOpt = {
    map : googleMap,
    position : googlePos,
    title : 'Hi , I am here',
    animation : google.maps.Animation.DROP
    };
    var googleMarker = new google.maps.Marker(markerOpt);
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
    'latLng' : googlePos
    }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
    if (results[1]) {
    var popOpts = {
    content : results[1].formatted_address,
    position : googlePos
    };
    var popup = new google.maps.InfoWindow(popOpts);
    google.maps.event.addListener(googleMarker, 'click', function() {
    popup.open(googleMap);
    });
    } else {
    alert('No results found');
    }
    } else {
    alert('Geocoder failed due to: ' + status);
    }
    });
    }

    function stopWatch() {
    if (watchId) {
    navigator.geolocation.clearWatch(watchId);
    watchId = null;

    }
    }

    function showError(error) {
    var err = document.getElementById('mapdiv');
    switch(error.code) {
    case error.PERMISSION_DENIED:
    err.innerHTML = "User denied the request for Geolocation."
    break;
    case error.POSITION_UNAVAILABLE:
    err.innerHTML = "Location information is unavailable."
    break;
    case error.TIMEOUT:
    err.innerHTML = "The request to get user location timed out."
    break;
    case error.UNKNOWN_ERROR:
    err.innerHTML = "An unknown error occurred."
    break;
    }
    }
    </script>

    </head>
    <body onload="geoloc()">
    <p id = 'mapdiv'></p>
    <button onclick="stopWatch()">
    Stop
    </button>
    </body>
    </html>