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

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");
};

Related

Mackbook safari location ask again and again even I allowed the permission

I am using location detection code at my website, it works correctly for chrome and firefox but when I checked it on MacBook safari and reload the page it's asking again and again even I allowed the location. When I reload the page, all the time it's asking for the location. This is my Code:-
$( document ).ready(function() {
getLocation();
});
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
// Success function
showPosition,
// Error function
null,
// Options. See MDN for details.
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
} else {
console.log("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude);
}
It's not about your code. Change Safari Preference to prompt location service for each website only one time.

Wrong coordinates using HTML Geolocation API

I am trying to use HTML5 Geolocation API on Windows 10 using Chrome as a browser.
I am having problems with getting right longitude and latitude. I have tried also using some web applications such as where-am-i.me but it giving the same result. Only Google gives the right location. How I can fix this, can we use some Google API for this?
enter code here
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
$(".latitude").text(position.coords.latitude);
$(".longitude").text(position.coords.longitude);
showMap(position.coords.latitude, position.coords.longitude);
}

Device location path error for navigation of screen in iOS 9

In my Cordova based iOS app, when I am trying to navigate to next screen from home page ,It is remaining in Home page in iOS 9 where as navigation working fine with iOS 8.4 and below.
Here is the path in iOS 8.4(working fine)
file:///var/mobile/Containers/Bundle/Application/EABC-4728-97BF-466B/MyApp.app/www/index-telugu.html#publicinterface
Here is the path in iOS 9.0 which is different from supposed path
file:///var/mobile/Containers/Bundle/Application/47CF-A77E-97ACED384A/MyApp.app/www/index-telugu.html#main
If anyone facing the similar issue Please suggest me the way to solve this
Here is my code:
$('#publicinterface_main_id').click(function()
{
if (!checkConnection())
{
navigator.notification.alert('Please Check Your Internet Connection');
}
else if (!navigator.geolocation)
{
navigator.notification.alert('Please switch on location settings on your mobile');
}
else
{
window.location.href = "index-telugu.html#"+$(this).attr('reloadIndex');
console.log("Path for navigation: : " + window.location.href );
location.reload();
navigator.geolocation.getCurrentPosition(function (p)
{
getAddress(p.coords.latitude,p.coords.longitude);
$('#pub_HgeoLocation').val(p.coords.latitude+","+p.coords.longitude);
});
var places = new google.maps.places.Autocomplete(document.getElementById('pub_geoLocation'));
google.maps.event.addListener(places, 'place_changed', function ()
{
var place = places.getPlace();
var address = place.formatted_address;
var longitude = place.geometry.location.lng();
var latitude = place.geometry.location.lat();
$('#pub_HgeoLocation').val(latitude+","+longitude)
});
}
});
A bug/"feature" of the iOS 9.0 UIWebView (used by Cordova/Phonegap) is that setting of window.location.hash is asynchronous - see this bug report for details. Note that Safari on iOS 8+ uses WKWebView not UIWebView, so this issue is not evident in the Safari browser on iOS 9.0
console.log(window.location.hash); // -> "#bar"
window.location.hash = '#foo';
console.log(window.location.hash);
// -> "#bar" // iOS 9.0 UIWevView
// -> "#foo" // iOS 9.0 WKWebView (Safari) and all other known browsers except
// in all other known browsers at this point window.location.hash will read '#foo'. In iOS9 UIWebView it won't.
if(window.location.hash !== '#foo') {
// bang: iOS 9 webview
} else {
// ok: any other browser
}
As a workaround, you can try using window.setTimeout to make operations following setting the value window.location.hash asynchronous, allowing for the value to be applied before you use it. So using your code above, try something like:
window.location.href = "index-telugu.html#"+$(this).attr('reloadIndex');
window.setTimeout(function(){
console.log("Path for navigation: : " + window.location.href );
location.reload();
},0);

Geolocation in Windows Phone HTML App not working

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;
}

HTML5 Geolocation for IE8 and Mobile Browsers

So I have been developing a web application, and I have implemented the HTML5 geolocation feature. I have implemented this using Javascript code taken from Google's website, that interacts with the Google Map that I have on my page as well. Here is the code below:
// Note that using Google Gears requires loading the Javascript
// at http://code.google.com/apis/gears/gears_init.js
var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag = new Boolean();
function initialize() {
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Try W3C Geolocation (Preferred)
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
map.setCenter(initialLocation);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
// Try Google Gears Geolocation
} else if (google.gears) {
browserSupportFlag = true;
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
map.setCenter(initialLocation);
}, function() {
handleNoGeoLocation(browserSupportFlag);
});
// Browser doesn't support Geolocation
} else {
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
function handleNoGeolocation(errorFlag) {
if (errorFlag == true) {
alert("Geolocation service failed.");
initialLocation = newyork;
} else {
alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
initialLocation = siberia;
}
map.setCenter(initialLocation);
}
}
So what this code does is, it checks to see if the browser you are accessing the site from supports the new WC3 geolocation features, and if it does, it uses that to find latitude and longitude. If you do not support it, it tries to use Google Gears to obtain that information, and finally, if you support neither, it bails out.
I am completely fine with using this the way it is for browsers like Firefox, Safari, Opera, and Chrome, because most new versions (and even some older ones), support these features, however, IE8 (9 works), doesn't :(
IE8 doesn't even use the Google Gears failsafe, it just doesn't work at all. Is there anyway (free like the above code), to use some form of geolocation on IE8, even if it's not as accurate to obtain latitude and longitude so I can plug it into the map?
Also, I haven't testing it yet but, if you were to log onto this site using a Mobile Browser (on the major devices), would it support WC3 Geolocation/Google Gears as well, or would it bonk out like IE8 does. I know I can also use the sensor=true attribute when requesting location if coming from a mobile device if they have their GPS turned on, so that's another way to do it, but I'd rather just use geolocation if they support it.
Thanks guys.
I am waiting for the HTML5 geolocation features to mature a bit more before I begin using them in full swing. In the mean time, I've used the APIs and datasets offered by MaxMind with great success.
Edit:
I forgot to mention, this sometimes doesn't work well with mobile browsers, so you may still want to use the HTML5 geolocation feature there as I've found it to be much more quick and reliable on mobile browsers.