Is it possible to access GPS data (Country name, City name, Street name and house number) in real time on a smart phone with JavaScript?
3 Answers
Javascript Geolocation requires special permissions. Chrome used to allow geoloc to be more easily accessible but has since restricted most code from accessing it. Here are a few examples of things you can do:
Here is an example from Google. You need secure origin https, a map api key and likely permissions from client browser:
https://developers.google.com/maps/documentation/javascript/examples/map-geolocation#try-it-yourself
This JSFiddle uses an API and doesn't require permissions: http://jsfiddle.net/zK5FN/2/
You can also get the client's IP address through PHP and then run it through something to match the exact location:
$_SERVER['REMOTE_ADDR'];
-
Just to add, you won't necessarily be able to "match the exact location" from the IP address alone. This can depend on the DB/service you are using, as well as how your ISP assigns IP addresses.– MrWhiteCommented Mar 6, 2018 at 16:45
-
So there's no chance I'll get the address for my gps coordinator on my phone, if not https and "third person"? this is injustice, suction.. :(– Xen42Commented Mar 6, 2018 at 18:00
Yes, Javascript can be used to obtain real-time GPS location, if the browser has allowed the website to access the device location.
The following code obtains the user's location, and the function can be called several times. Each time, it should give the current (updated) location:
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
How the location is obtained and what is reported to the website depends on the device and browser.
Interesting read: HTML5 Geolocation API – how accurate is it, really? Part 1: Non-GPS enabled devices, Part 2: GPS-enabled devices
For more info, see:
-
hi, thank you for your reply. I did not read everything, but I looked at it mostly just by the coordinates. and later read it completely. I want to get the user's location (country, city, street, house number) based on gps and put it in one input. sorry for my English!– Xen42Commented Mar 14, 2018 at 6:16
-
The solution I tested here (stackoverflow.com/questions/39366758/…) is a https required. it works on a local network, but it does not work sharply. So today (2018) there is no solution that works without https?– Xen42Commented Mar 15, 2018 at 13:47
-
@Xenophobe All of these used to work with HTTP, but Mozilla and Google (very rightly) now enforce HTTPS as a requirement. It's not that they cannot work on HTTP, it's that they should never be. HTTP IS NOT SECURE. All HTTP data can be be intercepted at various points. Transmitting data as personal as your real-time location over HTTP is a BAD idea, especially since SSL certificates can be obtained for free– undoCommented Mar 16, 2018 at 15:46
If you want to access accurate geolocation data through a smart phone. You may consider HTML5 Javascript API. It can access the latitude/longitude data through various devices/browsers. To be aware, it requests the users' permission sharing the geolocation data, otherwise it won't be available. Furthermore, you can implement geocode API to convert lat/long to accurate country name, city name, and street address.
Hope it helps!