Cyzanfar
Cyzanfar

Reputation: 7136

navigator.Geolocation GetCurrentpositon isn't working on Chrome and Firefox

I am following this tutorial online that teaches you how to use the new built in features of HTML5 and google map api to get your current location (Geolocation).

˚<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Navigator</title>
</head>
<script src="js/jquery.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>

<script>

x = navigator.geolocation;

x.getCurrentPosition(success, failure);

function success(position) {
    var mylat = position.coords.latitude;
    var mylong = position.coords.longitude;
    $('#lat').htm(mylat);
    $('#long').html(mylong);
}

function failure() {
    $('#lat').html("<h3> No co-ordinates available!</h3>");
}
</script>

<body>
<!--map placeholder -->
<div id="map">

</div>

<div id="lat"></div>
<div id="long"></div>
</body>



</html>

I tried to run this code on both google and firefox. Google is blocking me from seeing my current position. I get the fail message "No co-ordinates available!" As for firefox, it works so great!

What is the issue here with Chrome? A friend suggested that i should try getting my location using my IP address. What to do? And is there a way around Chrome blocking my current location?

Upvotes: 0

Views: 402

Answers (1)

fuzzybear
fuzzybear

Reputation: 2405

I use this code works in IE/Chrome/FF mobile browsers almost everything

    if (navigator.geolocation) {
    var latitude = null;
    var longitude = null;
    navigator.geolocation.getCurrentPosition(function (position) {
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
   });  

 } else {
    alert("Geolocation API is not supported in your browser");
 };

Upvotes: 1

Related Questions