Shinu Thomas
Shinu Thomas

Reputation: 5316

How i can find the current longitude and latitude using html5

How i can find the current longitude and latitude of user using html5 from a iphone browser

Upvotes: 0

Views: 3939

Answers (2)

Erdinç
Erdinç

Reputation: 267

<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.1&sensor=true"></script>
<script type="text/javascript">
    var r = confirm("We need your permission to get your location!");
    if (r == true) {
        function onPositionUpdate(position) {
            document.getElementById("txtlati").value = position.coords.latitude;
            document.getElementById("txtlongi").value = position.coords.longitude;
        }
        if (navigator.geolocation)
            navigator.geolocation.getCurrentPosition(onPositionUpdate);
        else
            alert("navigator.geolocation is not available");
    }
    else {
        x = "Sorry! You will be redirecting";
        location.href("Default.aspx");
    }
    function initialize() {
        var map = new google.maps.Map(document.getElementById("map_area"));
        var myLatlng = new google.maps.LatLng(document.forms[0].txtlati.value, document.forms[0].txtlongi.value);
        var myOptions = { zoom: 15, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP }
        var map = new google.maps.Map(document.getElementById("map_area"), myOptions);
        var marker = new google.maps.Marker({ position: myLatlng, draggable: true, flat: false, map: map, title: "Move pin to get latitude and longitude." });
        google.maps.event.addListener(marker, "drag", function () {
            var latlng = marker.getPosition();
            document.forms[0].txtlati.value = latlng.lat();
            document.forms[0].txtlongi.value = latlng.lng();
        });
    }
</script>


<div id="map_area" style="width: 700px; height: 500px">
</div>

and i have textboxes in div.thats it.

Upvotes: 0

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83768

Geolocation is available via Javascript geolocation API:

https://developer.mozilla.org/En/Using_geolocation

The API is exposed via navigator

         navigator.geolocation

The API is the same for all HTML5 enabled browsers and there is nothing iPhone specific in it.

Upvotes: 2

Related Questions