Geolocation in Windows Phone HTML App not working - html

I'm developing html5 windows phone 8 application and I've run into problem: geolocation doesn't work for me. I have WebBrowser.IsGeolocationEnabled property set to true and in app manifest ID_CAP_LOCATION is also checked. Hovewer even with the geolocation code copy-pasted from various HTML5 learning portals I'm still getting an error telling that site doesn't have geolocation permission.
Thanks for any help
var watchId = navigator.geolocation.watchPosition(scrollMap, handleError);
function scrollMap(position) {
myLoc.setLatLng([position.coords.latitude, position.coords.longitude]);
}
function handleError(error) {
myLoc.setLatLng([0, 0]);
}

I had the same issue, but solved it by totally rewriting my code - simplifying it:
var lng, lat;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else { alert("Geolocation is not supported by this browser."); }
}
function showPosition(position) {
alert("Latitude: " + position.coords.latitude +
"\nLongitude: " + position.coords.longitude);
lng = position.coords.longitude;
lat = position.coords.latitude;
}

Related

HTML5 geolocation is not accurate

I was using the following code to get my current location. But the longitude and latitude generated was not at all accurate. It was showing a location about 700 Kms away from my location. How can I make it accurate?
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
Or you could try a different geocoding provider.
There are lots to choose from:
https://smartystreets.com
http://geocoder.us
http://geoservices.tamu.edu/Services/Geocode/
Disclaimer: I'm a developer at SmartyStreets.
Probably you are not using GPS. If you aren't, then navigator.geolocation.getCurrentPosition returns a position based on your ISP

HTML5 Geolocation API is not working on JSFiddle

I have below mentioned JSFidle.But it's not working.Can you say why's that ?
Note: I want to run it on JSFiddle.It should show the Latitude and Longitude.
JSFiddle Geolocation
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
Because you have your JavaScript set to run in the load event (the second drop down on the left). The function getLocation only exists within the scope of that load handler, not in the global scope you're trying to call it from.
It works fine if you change it to 'No wrap - in <body>'

How to enable geolocation in Chrome

I got the HF HTML5 book , and when i reached to the chapter about geolocation and typed the code in , it doesn't work , i tried enabling the geo-location features in preferences but still no change , any suggestions?
The JS:
window.onload = getMyLocation;
function getMyLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayLocation);
} else {
alert("Oops, no geolocation support");
}
};
function displayLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var div = document.getElementById("location");
div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
};
Problem was solved.
I found out that it's a bug from chrome , every file:// has it , you can't do it without a server like WAMP , or you can use an other browser.
Try binding event handler like this:
window.onload = new function() { getMyLocation() };
Here is the fiddle.

Phonegap not detecting accurate location

I am using HTML5 Geolocation API in phonegap to precisely detect my location. I am working from India I should ideally be able to see my location i.e. Pune, India but it doesn't do so.
It detects 'San Francisco, CA, USA' as my location. What might be the problem?
Thank you all. Help is highly appreciated.
Let me share the code :)
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is loaded and it is now safe to make calls Cordova methods
//
function onDeviceReady() {
// onSuccess Callback
// This method accepts a `Position` object, which contains
// the current GPS coordinates
//
var onSuccess = function(position) {
var lattitude = position.coords.latitude;
var longitude = position.coords.longitude;
//Ajax Call
$.ajax({
url: 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+lattitude+','+longitude+'&sensor=true',
success: function(data){
alert(data.results[0].formatted_address);
//alert(data.results[1].formatted_address);
},
error: function(data){
alert('error');
}
});//Ajax call ends
};
function onError(error) {
alert('code: '+ error.code + '\n' + 'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
If you're using the iOS simulator you will always get SF as the location as it has a static location setting. You can however use this nifty tool to emulate in browser and it will allow you to mess with location settings and more.
http://emulate.phonegap.com/

HTML5 geolocation won't work in Firefox, Chrome and Chromium

I'm trying to use the HTML5 geolocation API; but I have problems to make it work on Firefox Chrome and Chromium :
init();
function init() {;
// Get the current location
getPosition();
}
function getPosition() {
navigator.geolocation.getCurrentPosition(success, fail,{
enableHighAccuracy:true,
timeout:10000,
maximumAge:Infinity
});
}
function success(position) {
alert("Your latitude: " + position.coords.latitude + "longitude: "
+ position.coords.longitude);
}
function fail(e) {
alert("Your position cannot be found"+e.code+" => "+e.message);
}
In IE9 and Safari it works flawlessly; but :
in Firefox (v13 and V14) there is an error code 3 (timeout)
in Chrome and Chromium (v20 and v21) there is and error code 2 with the message "Network location provider at 'https://maps.googleapis.com/maps/api/browserlocation/json?browser=googlechrome&sensor=true' : Response was malformed."
I have a fresh install of Chrome (installed today on windows XP, no extensions) and I have authorized the geolocation in the browser.
You can try it there :
http://jsfiddle.net/mhj82/38/
Is there a solution to make it work on all browser supporting geolocation ?
Have you read this ?
http://code.google.com/p/chromium/issues/detail?id=41001
At the end of the thread they come to the conclusion that in order to work in Chrome, geolocation must be performed on a device with a working wifi adapter.
Was wifi enabled on your computer ?
(dunno for firefox)
I have to wait until the document is loaded to get it work in chrome
jQuery().ready(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition....
}
});
Try this tested in chrome desktop and mobile
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");
};