2

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?

  • 1
    To clarify, "GPS data" is literally just the Lon/Lat coords. To get the "Country name","City name", etc. from this data, you will need to use a third party service. – MrWhite Mar 6 '18 at 17:07
2

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. – MrWhite Mar 6 '18 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.. :( – Xenophobe Mar 6 '18 at 18:00
1

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! – Xenophobe Mar 14 '18 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? – Xenophobe Mar 15 '18 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 – rahuldottech supports Monica Mar 16 '18 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.