malleshtekumatla

How to Display User or Device Position on Maps using Geolocation

In this script we are going to see how to show the geopraphical location of the user or a particular device
on a google map.

When user opens a brower it will ask “This file want to” know your location Allow or block .
Allow or block

If user click on Allow button then Google map will shows the current position.

save below file as currentposition.html


<html>
<head>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (p) {
        var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
        var mapOptions = {
            center: LatLng,
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("malleshtekumatla"), mapOptions);
        var marker = new google.maps.Marker({
            position: LatLng,
            map: map,
            title: "<div style = 'height:60px;width:100%'><b>Your location:</b><br />Latitude: " + p.coords.latitude + "<br />Longitude: " + p.coords.longitude
        });
        google.maps.event.addListener(marker, "click", function (e) {
            var infoWindow = new google.maps.InfoWindow();
            infoWindow.setContent(marker.title);
            infoWindow.open(map, marker);
        });
    });
} else {
    alert('Geo Location feature is not supported in this browser.');
}
</script>
</head>

<body>

<div id="malleshtekumatla" style="width:100%; height: 500px">
</div>
</body>
</html>


2 Replies to “How to Display User or Device Position on Maps using Geolocation”

Leave a Reply

Your email address will not be published. Required fields are marked *