How to find my real latitude and longitude using JavaScript code? - javascript

I want to find my latitude and longitude using PHP.
I have tried multiple ways, but my solutions only show the service provider location and IP address. I want to find my real latitude and longitude.
I am using the below code but it's not working for me.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<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>

navigator.geolocation will generally use the most precise data available to the browser.
If the client has a GPS (as phones tend to) then it will probably use that.
If it has Wi-fi, then it may be able to use nearby access points to determine the location.
If it has to fallback to GeoIP lookups, then it will only be a precise as the data held on the ISP.
You are currently suffering from using a client which is using the last of the above. You can't get more precise than that without changing the client.

Related

Django web app that detects a smart-phone user's position

I want to build a web app using Django-Python that will be mainly used from smart-phones. I want the app to be able to detect the user's position and showcase it into a google maps front end. Basically, I want the app to be something like google maps GPS and then I will make some calculations with the coordinates and print out to the user some alternatives. I want the user's coordinates to be updated when he walks for example.
Do you have any suggestions about what modules, libraries or packages can I use to get this done? I found some packages like djangocms-gmaps but I am not sure if this is the right way to go.
This is not a function provided by Django. More javascript anf HTML5
Here is a example snippet that you ofcourse can use with a Django project.
<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>

How to get current location in google map for web

I am new to this API. I generated api key but I don't know how to implement google map.
I searched a lot but nothing is working for me. I need help in finding users or device current location.
I need to display google map in my website with pointing to the current location of the device using angularjs.
Can anyone please suggest me how to get current location in google map or any tutorial where can I get code.
to get the current position you need to take location from the navigator itself
with simple html5 geolocation code
<p id="demo"></p>
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;
}
after getting this location pass it on the map, your current location will be displayed

Reverse Geocoding does not return a post code

I am trying to use HTML5 geolocation functionality with some google map APIs, for a mobile web app, as follows...
I have a MYSQL table that holds address records for properties including post code, but no coordinates. I am successfully using HTML5 Geolocation to get the current coordinates using this code...
<script>
window.onload = function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var latlng = position.coords.latitude + "," + position.coords.longitude;
var lat = position.coords.latitude;
var lng = position.coords.longitude;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
...The DB table is large and I want to restrict the results of a query on post code, and so I need to do some kind of reverse geocoding to convert my current latlng into a post code to feed my query.
Using the corodinates returned in 'var latlng' testing at my home address, I have then tested this 'latlng' in the Reverse Geocoding sample in google dev examples here https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse but it only returns 'town, country' and no post code. Is there a way for me to get the post code every time, or perhaps find the nearest post code?
I then want to create my SQL e.g. "SELECT * FROM tblProperties WHERE tblProperties.postCode = 'geocodingPostalCode';"
I have tested the google dev example using other random coordinates that do return full addresses including post code but not at my home address, I need it to work all the time.
Any help much appreciated. Thank you.
The example you are using for reverse geocoding uses the second result, which may be to broad for a postcode. You won't necessarily always get a postcode, but the first result (results[0], rather than results[1]) would be more likely to have a postcode.

Check if user device's GPS is on

I am developing an app using jQuery Mobile with PHP. I am not using Phonegap or other frameworks. I need to find user's geolocation. If user device's GPS is off, then I cant get a location. now I need to find user device's GPS is on or off.
this is what i using now.
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var lat=position.coords.latitude;
var long=position.coords.longitude;
}
You can call this function on load
// Function to get location
function getLocation(){
navigator.geolocation.getCurrentPosition(function (pos) {
var lat = pos.coords.latitude;
var lng = pos.coords.longitude;
if (lat == null) {
alert("GPS not activated!");
} else {
alert("Latitude: "+ lat + " , Longitude: " + lng );
}
});
}
There is no way to check if the device has a GPS module or if it has it enabled through the browser's API. Your laptop will, for example, try to estimate the position based on the IP address with very poor accuracy.
You can, however, use a trick that will likely be good enough for many applications: instead of using the getCurrentPosition(), use the watchPosition() function with an options object { enableHighAccuracy: true } and set a threshold of accuracy that the measurement has to reach for you to accept it as most likely a result based on the GPS module.
What happens when you start to listen to the watchPosition() with enableHighAccuracy set to true is that if GPS module is available, the API will let it know that you're trying to get a measurement and after up to a few seconds the accuracy distance will go from very high (often thousands of meters - based on IP address, cell tower triangulation, etc.) to a very low (few meters - based on the GPS) and that means that the GPS kicked in. If the accuracy stays at hundreds or thousands of meters, it probably means that there is no GPS module available.
Here's the documentation for the GeolocationCoordinates object (the result within the callback passed to the watchPosition()) which comes with the accuracy field. I wrote a longer post that also contains a code snippet showing how I use the API within React.
I just solved this one. I am using:
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {maximumAge: 60000});
In the successCallback i written the codes for what it should do once I got the positions and in the error callback i wrote a simple alert message to prompt the user to turn the GPS on.
I Implemented This In Real World Project
KMaps-API GPS.js
<script>
function getLocationw() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPositionw);
} else {
x.innerHTML = "Something Is Wrong";
}
}
function showPositionw(position) {
lat = position.coords.latitude;
if(lat != null){
document.write('<center><div class="alert alert-info" role="alert"> Please Turn On Your GPS </div></center>')
}
}
getLocationw();
</script>

js geolocation but without prompting - possible?

Is it possible to get the geolocation of a user without the browser prompt?
Here's the code sample from W3
<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>
Is there any preventPrompt()-like function ?
No you cant prevent the prompt, its a security feature cause not every user wanna share its location.
From the W3C docs:
A conforming implementation of this specification must provide a
mechanism that protects the user's privacy and this mechanism should
ensure that no location information is made available through this API
without the user's express permission.
But you can try to use a service like geoip in the error callback.
No, that's not possible.
The prompt is there so that the user can choose whether you know the location or not.

Categories

Resources