Get latitude and longitude of user in PHP or JavaScript for use in PHP - javascript

I have an Android app which gets the user's location (latitude and longitude). The latitude and longitude is used to query a database via a web service.
I am in the process of developing a website which will be another gateway into the backend. In order to make the website useful I need to get the latitude and longitude of the user, as the results are sorted by distance (i.e. distance from user's location to location of returned data which is a bar/restaurant).
So how do I get the latitude and longitude of the user?

It is not possible via PHP directly although if you are creating a website you can use the javascript geolocation API to query the browser about what it knows about the users current location:
<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>
I have simply copied the code example from w3 schools vertabrim.
Source:
http://www.w3schools.com/html/html5_geolocation.asp

Related

HTML/Javascript: access to geolocation from local HTML file

I'm trying to implement a web page which displays your location on a map. Given that the person accessing the web page may not have internet access, I want the web page to be a local HTML file.
These days, for security reasons, it doesn't seem to be possible to access geolocation form a local HTML file.
Any ideas as to how I can get access to geolocation from a local HTML file?
Thanks
I have tried accessing my local HTML file via a local web server but these only allow http pages whereas geolocation seems to be dependent upon an https file.
To use Geolocation API your application should run on HTTPS protocol or on the localhost web server. Otherwise, Geolocation API will not work.
Most of all you will get the coordinates of your internet provider stations :)
navigator.geolocation.getCurrentPosition(showPosition);
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude +
" Longitude: " + position.coords.longitude);
}
<button onclick="getLocation()">Get Location</button>
<script>
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);
}
</script>

Why is JavaScript geolocation only Approximate?

No matter how I use the JavaScript:
position.coords.latitude;
position.coords.longitude;
The returned coordinates are always at least a few city blocks off.
So - it will show the "start point" or "Your location" as a few streets over?
This approximate result seems new, as the code I used before produced a more accurate result.
I have tested on all browser's from a "https" web page.
<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 + '/' + position.coords.longitude;
}
</script>
Has something changed?
Why is the produced coordinates only approximate?
This is a tradeoff between speed and accuracy. Gelocation.getCurrentPosition() at MDN details the optional parameter enableHighAccuracy
JavaScript geolocation is only an approximation of the device's actual location because it relies on the device's built-in GPS, Wi-Fi, IP address lookup and/or network triangulation to determine your location. Additionally, the Geolocation API provides an estimated location, not an exact location. The accuracy of the location information can vary greatly and can be influenced by various factors, including the type of device, its settings, and the environment (buildings, trees, etc) in which it is used.
I used your function and indeed the latitude and longitude coordinates were not exact, there is a difference of a few meters (more than 30-40).

Redirection to different landing page according to city - Wordpress Site

I am new to Website development so I want to redirect people from specific cities to the specific landing page and all the rest to normal homepage in a WordPress site. I have to do this for a client. is there a sample blog or code that can help me I got the longitude and latitude via script I want to detect the location automatically and then redirect according to the city.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
document.getElementById("demo").innerHTML =
"Geolocation is not supported by this browser.";
}
function showPosition(position) {
document.getElementById("demo").innerHTML =
"Latitude: " + position.coords.latitude + "<br>" +
"Longitude: " + position.coords.longitude;
}
however, I don't know how to proceed from here. (TT)

Alternatives to getCurrentposition?

I have been working on an app that uses the getCurrentPosition(), but that doesn't work anymore in the latest version of Chrome, see:
Deprecating Powerful Features on Insecure Origins
So my code here doesn't work in latest stable version of Chrome:
var latitude = 0;
var longitude = 0;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
$("#data").html("latitude: " + latitude + "<br>longitude: " + longitude);
});
}
What are alternatives to getting the user's position with HTML Geolocation API? Any thoughts?
I'm grappling with this myself. If HTTPS is not an option for you, then maybe add a fallback for Chrome where you prompt the user to submit their address. Then have your app geocode it.

How to track location using GPS for jQuery mobile app? [duplicate]

This question already has answers here:
Geographical coordinates to street names
(4 answers)
Closed 9 years ago.
I am developing a jQuery mobile app. I need to track the user's location (city address).
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;
}
This gives only latitude and longitude. I don't need lat or long or maps. I need to get only the city address. What must I do to get this?
Try it using Reverse Geocoding, this returns JSON. Use the URL this:
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false
Get the JSON like explained here: http://api.jquery.com/jQuery.getJSON/

Categories