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.
Related
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>
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.
I am attempting to use Google Sign-In for Websites (https://developers.google.com/identity/sign-in/web/) and noticed that my solution is not working in Internet Explorer 11. To try to eliminate as many factors as possible, I created a simple test case based on the sample code provided by Google.
I've tested it in Chrome on my Windows 7 PC, Chrome on my Mac, Safari on my Mac, Firefox on my Mac and Safari on my iPhone. It works on all of these (e.g., when I click the sign in button and select/enter my Google account, it returns to the page and the button says, "Signed in").
It does not, however, work on Internet Explorer 11 on PC or, strangely enough, Chrome for iOS. When the button is clicked, a window opens to allow me to select my Google account, but after making a selection, the window closes and returns to the page with a button that still says, "Sign In."
Here is the sample code:
<html>
<head>
<meta name="google-signin-client_id" content="61023618497-vqfbod57f26ncjl9d6firk3t09ve4tt3.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div class="g-signin2"></div>
</body>
</html>
Any ideas as to what might be going on? I've searched around and have not found any solutions.
One idea was to add "accounts.google.com" to IE's Trusted Sites. This didn't work. I also tried accessing the page via https instead of http. That didn't make a difference either. Anything else I should try?
I ran into the same problem now, a few months later.
If you look at:
https://developers.google.com/identity/sign-in/web/build-button
and try a signin with their demo button, it does not work with IE11 (but it works with other browsers I am using at different OS). I could not find any solution.
I am leaving the comment for a future reader, who searches for the same problem in the future. If the demo button at Google does not work, she can at least rest assured that the problem is probably not in her code. :)
Be sure that you don't have IE 11 configured to block all third party cookies.
Third party cookies are required, and the user experience that occurs when third party cookies are blocked--as you've discovered--leaves much to be desired. There is no warning or error message presented to the user.
You could try to catch the error before it happens. It is possible to detect whether or not third party cookies are blocked by trying to set a cookie on a second domain (that you control) and then making a second request to ensure the cookie is set. You'll need a script or something on your server that can set and check for the cookie (it can't be done using only JavaScript because of the browser security model).
I've had success doing the following:
Loading the script to initiate the Google button rendering etc. from $document.ready. (i.e.Whatever you have in the apis.google.com/js/client:platform.js?onload= x )
e.g.
<script src="https://apis.google.com/js/client:platform.js?onload=startApp" async defer></script>
Move startApp() to here:
$(document).ready(function () {
startApp();
Where startApp() looks something like this:
function startApp() {
gapi.load('auth2', function () {
gapi.client.load('plus', 'v1').then(function () {
gapi.signin2.render('signin-button', {
scope: 'https://www.googleapis.com/auth/plus.login',
fetch_basic_profile: false
});
gapi.auth2.init({
fetch_basic_profile: false,
scope: 'https://www.googleapis.com/auth/plus.login'
}).then(
function () {
console.log('init');
auth2 = gapi.auth2.getAuthInstance();
auth2.isSignedIn.listen(updateSignIn);
auth2.then(updateSignIn());
});
});
});
}
I struggled to get the example to work on localhost, but as soon as I deployed it to the real URL, it worked.
I have the following html that tries to set one key in local storage.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script>
document.addEventListener('DOMContentLoaded', loaded, false);
function loaded(){
try {
window.localStorage.setItem("Test", "SetItemValue");
document.getElementById("test").innerHTML = "Test OK";
} catch (err) {
document.getElementById("test").innerHTML = "Test FAIL<br>" + err.message;
}
}
</script>
</head>
<body>
<div id="test">Testing...</div>
</body>
</html>
On one single iPhone5 this causes the following exception.
Test FAIL SecurityError: DOM Exception 18
Other iPhones tested (three others) with the same iOs-version (7.0.2) works.
I have tested the above page from both a https://x.y.domain.tld and a http://x.domain.tld with the same exception.
Other questions concerning "DOM Exception 18" seem to be about security settings when eg. testing on localhost but linking in remote content over https. But this is a simple html page that simply tries to access local storage.
I read somewhere that if cookies are blocked, the DOM Exception 18 error shows up when setting localStorage. I was able to reproduce the error (not sure if I reproduced the issue, per se) on a simulator iPhone 5 (w/ iOS7) by going to the Settings, then for Safari, "Block Cookies" always. Don't know if that's how your iPhone 5 is configured though...
Problem was solved. It was revealed that the client (the errant phone was a client phone) uses a company-wide security platform installed on their iPhones. That platform has a separate web browser that must be used to enable every Javascript feature. The end user with the phone didn't know this so he used Safari that somehow is crippled when this platform is active. So the solution was to use the right application for browsing.
The platform was http://www.mobileiron.com/ and the secure browser is called Web#Work
I've seen that you can't change data in local storage within the first couple of seconds. Set a timer for 5000 milliseconds, and then run the setItem-function to see what happens.
I'm trying to use Google's GeoCoding API within Dynamics CRM 2011 and am having problems. I've written the following JavaScript function:
function geocodeXML()
{
var mDataPath = "http://maps.googleapis.com/maps/api/geocode/xml?address=WV1+1UL&sensor=false";
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
alert(1);
xmlDoc.load(mDataPath);
alert(2);
var theLat = xmlDoc.getElementsByTagName("lat")[0].childNodes[0].nodeValue;
var theLng = xmlDoc.getElementsByTagName("lng")[0].childNodes[0].nodeValue;
alert(theLat + ", " + theLng);
}
I've run this locally on my computer and it works fine - it runs through the code and gives me an alert box with the latitude and longitude. However, as soon as I include it in CRM, it refuses to run. I've added alert boxes as above and it's the line inbetween the two alerts (xmlDoc.load(mDataPath);) that's causing the problem. I get the alert box with "1" in it and then nothing else. I've tried including the code in a CRM function (which gives me an "Access is denied" error for some reason) and I've tried wrapping it in HTML and including it as an iframe.
I have seen that this API returns a tag called "Status" which should give responses like "OK", "ZERO_RESULTS", "OVER_QUERY_LIMIT", "INVALID_REQUEST", etc so I know it's not an issue with quotas or the syntax of my request - it's as if the API can't be reached from the server at that address. As I've said, running the code locally work fine and I get a response from the API (with a status of "OK") but as soon as it's in CRM, it returns nothing.
I really am at a loss as to what could be causing this and I'd appreciate any help that can be offered.
Thanks.
I believe that you need to change browser settings to make this work as you are attempting to make a cross-site call from script.
Go to Security Settings in IE. From Miscellaneous select "Access data sources across domains" and enable it. From Scripting go to "Enable XSS filter" and disable it. Not really optimal but I think it might work for you - and at least confirm for you that it is a cross-site scripting issue.
If you put this in an IFRAME - worth at least testing - make sure you enable cross site scripting in the IFRAME setup.