Geolocation not supported in Safari with wired connection? - javascript

I am trying out geolocation in Javascript, writing a very small web app on codepen. I mainly use Safari (10.1.2) on my macbook (10.12.6) and I couldn't get the following geolocation code working :
navigator.geolocation.getCurrentPosition(function(position) {
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
getRequest(latitude, longitude);
}, function(error) {
console.log(error.code);
});
After some time I opened up Chrome and tested the same code, it worked. I started doing some research on Safari and geolocation and have seen some people mention wired connection being the culprit.
I tried with wifi and indeed, it works. I don't know why, and couldn't find an explanation anywhere, but Safari refuses to handle geolocation when connected via Ethernet.
I did the same test on Google Maps and geolocation works everytime. So I thought maybe my code is wrong but I have the same trouble with code from W3Schools. I'm guessing Google uses another method.
How can I make it work on Safari with a wired connection ? I'm surprised to find posts from 2010 and to be facing the same problem 7 years later with all software up to date. Is there a universally compatible method I'm missing ? I'm wondering how services depending on geolocation handle this problem.
Thanks in advance for your help.

Related

Navigator.getLocation.getCurrentPosition is not working on Android mobile [duplicate]

i'm trying to get the geolocation on Android Browser but nothing happens. I'm using a Samsung Galaxy S3 but i'm not sure about the version of my browser. Android version is 4.1.2
Here is my Code:
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
this is a code i copied and pasted from this site
it gives me the "navigator.geolocation"
but when it comes to "getCurrentPosition" my code stops working. Mobile Chrome works fine but this is not. I shared my position but still nothing happens. Any help will be appriciated.
Thanks.
Thanks everyone i found the solution,
i was getting the geolocation after some javascript operations. I tried to get the geolocation before document is ready. And it worked.
I know this is a bit old but it keeps coming up on searches so I thought I would add a tip that helped me.
Because I want to get the location right as the page loads I found that I needed to introduce a very short delay after the page loads. When I had no delay, I would get no error but also I would not activate the location protocols on the phone. This half second delay solved the issue. You can play with the delay and see if it solves your issues.
setTimeout(function() {getAutoLocation(true)},500);
I get the location in my "getAutoLocation(true)" function. This setTimeout only exists to introduce the delay.
seems like PhoneGap has a problem with geolocation
I have the same issue
I'm using S3 with Android 4.1.2, phonegap geolocation feature doesn't work
In order to get the geolocation without errors, you have to make that code block work before using the values provided by the geolocation because operations are carried out asynchronously, in this question i found the solution by loading my geolocation script before other .js files. This solved my problem and another trick for this issue is, geolocation works more stable when you give "always" permission for browser to read your location. After loading for the first time, you never encounter geolocation errors.
I found that some Android phones (old and new) don't run properly the function
getCurrentPosition, maybe trying to save some battery.
I played with the function watchPosition and then the high accuracy GPS kicked in.
Read this to know how to use the parameters properly:
http://dev.w3.org/geo/api/spec-source.html#watch-position
In my case, this worked:
{
maximumAge: 0, timeout: 2000, enableHighAccuracy: true
}
Hope this helps someone.
Did you give Internet permission in manifest?
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

How to enable geolocation using Firefox/Chromium on Ubuntu 16.04

I'm building a node app that returns the user's geolocation, but I'm having a hard time testing it because I can't access geolocation on my computer. I tried using my localhost server in both Firefox and Chromium - Firefox asks for my permission but then can't retrieve it, and Chromium doesn't even get that far. When I deploy it to Heroku, I still can't access it on the computer. On my iPhone, Firefox still has no luck but Safari can do it. I don't know if my problem is in Ubuntu itself (can it block things like that?) or if both Firefox and Chromium have the same issue.
I doubt my code will be especially useful, but here it is anyway:
const locationButton = jQuery('#send-location');
locationButton.on('click', function() {
if (!navigator.geolocation) return alert('Geolocation not supported by your browser.');
navigator.geolocation.getCurrentPosition(function (position) {
socket.emit('newLocationMessage', {
latitude: position.coords.latitude,
longitude: position.coords.longitude
}, function (mapLink) {
alert(mapLink);
});
}, function () {
alert('unable to fetch location.')
});
});
The app is also up at http://rocky-brook-97128.herokuapp.com/.
Thanks for any guidance on this!
As of Chrome 50, the Geolocation API will only work on secure contexts such as HTTPS. If your site is hosted on an non-secure origin (such as HTTP) the requests to get the users location will no longer function.
Try it on https.
https://rocky-brook-97128.herokuapp.com/
(1) In a new tab, type or paste about:config in the address bar and press Enter/Return.
(2) In the search box above the list, type or paste geo and pause while the list is filtered
(3) If the geo.wifi.uri preference is bolded and "modified" or "user set", you can right-click > Reset it to the default
If Firefox sends anything to the geolocation service, the address should appear in the Browser Console
You can inspect and manage the permissions for all domains on the about:permissions page

Geolocation API doesn't work on mobile

I'm writing my web application on React/Redux. And I need to get user location with a help of Geolocation API. On desktop browsers everything works fine, but on mobile phones (checked out on Xiaomi Redmi Note 3 and iPhone 5s) it throws error code 1 - permission denied. And it doesn't requests any permissions to get the location.
Here's a test sample which I ran on my site:
componentDidMount() {
if (window.navigator.geolocation) {
window.navigator.geolocation.getCurrentPosition(position => {
alert(position.coords.latitude + ' ' + position.coords.longitude);
}, err => {
alert('ERROR: ' + err.code);
});
} else {
alert('Geolocation API is not supported!');
}
}
What's the solution of this problem?
Got the same Problem... Solved:
Check your phone permissions for sharing your location.
On iPhone:
Settings -> Location Services -> [your Browser]
https://support.apple.com/en-ca/HT203033
Added:
Chrome requires https for geolocation usage:
https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only
I've got the solution. I'm using the Web Application Manifest and it needed to set the permission to use Geolocation API.
We just need to set an "required_features" option at manifest.webapp file:
{
"required_features": ["geolocation"]
}
Hope it will be useful for somebody ;)
As of the Year 2021, this still does not work.
This is the link in that error message.In case you're wondering, it talks about "prefer secure origins for powerful new features" and location is consider one of those powerful features.
To generate the above, update the error section as follows:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
// other
},
err => {
// include the "code" part
alert(`ERROR(${err.code}): ${err.message}`)
});
};
On desktop during development...It works because if you read from the above link you will note that localhost is considered a secure origin.
In fact, even the chrome link shared in #chrisheyn's answer above, there is a section "Does this affect local development?" and explains why this should work on locahost.
So how about Mobile during development?Notice that react serves the app over your network e.g. http://192.168.0.134:3000 and that is definitely not considered a "secure origin" at all.
This question "Can I detect at runtime if the geolocation was blocked because of not being on a secure context
" mentions that... Errors due to this secure-context issue will return a code of 1 which is a "Permission Denied Error".
What's the solution?
Until the react team updates how your mobile picks the app during development, there is absolutely nothing you can to solve this issue.
To use the HTML5 Geolocation API, you will need to run the app over HTTPS. This means push your app to the cloud/host (in order to test this feature) or if you can some manage to get this network url http://192.168.0.134:3000 to do https The latter option, i believe, is much harder but I'd be interested to know if someone pulls it off.

Geolocation fail in case of inactive mobile browser

I am using following code:
var fail = function(error) {
alert("Unable to get location");
};
function getGeo() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
success,
fail,
{maximumAge: 50000, timeout: 30000, enableHighAccuracy: true});
}
};
Mostly It works but often it gives geolocation error and unable to get coordinates.
How can I make possible to get coordinates no matter browser window is inactive or browser is in background or mobile itself in sleep mode?
Without specifics of "geolocation error" my answer can no be complete but: -
At present you cannot get coordinates if browser window is inactive or browser is in background or mobile itself in sleep mode, unless you are developing a hybrid App with something like phonegap/cordova. (Firefox has the exception of continuing to service watchPosition() if the App is foregrounded but the phone is asleep.)
I have made several suggestions to W3C, IETF, Chrome Dev, Mozilla Dev, and Edge Dev regarding a workable solution using the Service Worker Extensibility functionality for a Javascript-only solution.
Briefly, the UA tracks Geolocation changes and if within range of a dev supplied filter, a ServiceWorker will be instantiated which may foreground the App or merely notify the App Server.
Please pursue this issue with the relevant bodies as the idea is beginning to gain traction.
HTH

Identifying Geolocation of user behind proxy

I am writing an web application that requires the current geolocation of user. For identifying geolocation I have used the following code that uses HTML5 geolocation api. I have taken this code from W3schools.
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<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>
</body>
</html>
I have run this code with Tomcat, but it is not working
neither in Firefox nor in Chrome. Then I have tried this code with "Try It Yourself" in W3Schools and it is not working even there also neither in Firefox nor in Chrome. I am behind the proxy.
I have introduced an alert() statement at the start of body of the function showPosition() and it did not get called. So it seems this function is not getting called at all. I have no clue how to fix that. I desperately need to fix that. I have previously posted that problem but got no response probably because of not framing the problem clearly. So I am again posting the problem.
Is there any other way to get the geolocation of the users who are accessing this application throuogh computers. So please help. Thank you.
EDIT: I have hosted this file on Tomcat. Each time I am executing, it is giving error code 3 i.e. timeout. But I have executed the same Javascript two months back. Then it executed perfectly. Is it a problem with current version of my browser?
there is nothing for the proxy to do with the html5 geolocation api, it's detecting location based on your gps device if you have one on your machine, second it goes to use the wifi device, finally it uses the internet ip to detect your location, and if you connecting using proxy it'll show the location of your proxy.
your issue may be because you didn't give the permission to the localhost on your browser, check it again and retry your code.

Categories

Resources