I am developing iPad application using phonegap (cordova 1.9.0). I need to get current position of the user.
I used following code and it works fine when Location services are enabled.
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onSuccess(position) {
console.log(position.coords.latitude + "-" + position.coords.longitude);
}
function onError(error) {
alert('code: ' + error.code
'message: ' + error.message + '\n');
}
My problem is that this code not working when Location services are disabled. I need to get current location if Location service are enabled or disabled.
Anyone tell me how to get current location in this situtation?
You can't get the current location when location services are disabled. That would be a violation of a user's privacy. The point of disabling location services on a device is to prevent applications from getting the current location.
What I've done is display my own dialog with a lengthy explanation of why I need location services before calling getCurrentPosition(). I explain that the app won't work without it. I also say something like:
Your device may also ask you for permission.
This increases the chances of someone saying yes to the iPhone dialog.
Related
I am developing a streaming website. It works, however, at some time, I have installed a software called OBS which installed a virtual camera. Since then, when the web site tries to use the camera, it uses the OBS virtual camera, instead of my physical camera. But this occurs only in my PC. When the camera is to be used, this permission dialog appears:
In my laptop, this works, that is, in that same dialog, it shows a combobox allowing to choose which camera to use.
In the web site, I am starting the camera this way:
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
});
Can I do something with the getUserMedia function or this problem is only related to the PC configuration?
This has to do with Google Chrome. Assuming that OBS's deviceID stays the same, then use navigator.mediaDevices.enumerateDevices() to get a list of all available devices.
(MDN's example is quite handy):
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
});
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
});
However, if you would like to be able to switch cameras mid stream, then I would recommend using WebRTC's "official" example, however, you will need to use stream.replaceTracks to view it in real time.
It sounds to me like your web software lacks a user user interface to let your user choose a camera or virtual camera, as #divinelemom suggested. It's a good idea to do that. Example code doing that is here.
https://webrtc.github.io/samples/src/content/devices/input-output/
In the meantime, use this URL to see Chrome's interface for choosing the default camera.
chrome://settings/content/camera
It gives you a page like this. Your choice is "sticky" in the sense that it will be used by your web app after you set it.
How can i implement click event for Map Pins in Xamarin.Forms ?
I tried Something like this :
var pin = new Pin
{
Position = _testPosition,
Label = string.Format("Latitude: {0},Longitude: {1}", _curentPosition.Latitude, _curentPosition.Longitude)
} ;
pin.Clicked += (sender, args) =>
{
DisplayAlert("Tapped!", "Pin was tapped.", "OK");
};
But it doesn't worked !
Anyone have idea with this !
Thanks in Advance.
Does the code compile but doesn't fire the event or is it not seeing the clicked property at all?
Please check which version of Xamarin.Forms you are using.
The "Clicked" event was added to the xamarin.forms map control in version 1.3.0, but has only actually been working since version 1.4.2-pre2
The usage is exactly the same as you listed in your example.
Check that your packages and environment are up to date.
Some users are reporting issues with the clicked event not firing.
If this happens to you, use appropriate Xamarin support channels to report/vote on the issue to get fixed.
If you can't get it to work and can't wait for Xamarin to fix the issue, you can write custom renderers for the map control and access the native API to handle the tap event.
For more information on this, search for "Xamarin.Forms Custom Renderer"
i am trying to get user current position in mobile web application
my app work in all android telephone device except samsung galaksy s2 telephone device ..
it give errror POSITION UNAVAILABLE error
this is demo link.you can view source
this is code
navigator.geolocation.getCurrentPosition(handle_geolocation_query1, handle_errors) ;
function handle_errors(error) {
switch (error.code) {
case error.PERMISSION_DENIED: alert("user did not share geolocation data");
break;
case error.POSITION_UNAVAILABLE: alert("could not detect current position");
break;
case error.TIMEOUT: alert("retrieving position timed out");
break;
default: alert("unknown error");
break;
}
}
function handle_geolocation_query1(position) {
$('#map_canvas').gmap('addMarker', {
'id':'m_1',
'position': new google.maps.LatLng(position.coords.latitude,position.coords.longitude),
'bounds': true,
'icon': 'http://google-maps-utility-library-v3.googlecode.com/svn/trunk/geolocationmarker/images/gpsloc.png'
}).click(function () {
$('#map_canvas').gmap('openInfoWindow',{ 'content': '<font color="#2a2a2a" size="4">Location </font><br/><font color="#4a4a4a">Your current location</font>' }, this);
});
var map = $('#map_canvas').gmap('get', 'map');
map.setZoom(14);
map.setCenter(new google.maps.LatLng(41.01802007732287, 28.971880674362183));
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
}
edited:
i used phonegap to produce android apk file
when i work this apk file application it give error POSITION UNAVAILABLE error but when i call this page from web it works it does not give error ... this is web link and you can download apk from here
Your code is working fine in browser but not in mobile.
On checking your source code it seems you're specifying the sensor parameter as false.
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
You missed this while checking the documentation.
Specifying the Sensor Parameter
Use of the Google Maps API requires that you indicate whether your
application is using a sensor (such as a GPS locator) to determine the
user's location. This is especially important for mobile devices.
Applications must pass a required sensor parameter to the tag
when including the Maps API javascript code, indicating whether or not
your application is using a sensor device.
Applications that determine the user's location via a sensor must pass
sensor=true when loading the Maps API JavaScript.
Hope you understand.
On my device, I had my settings for Location mode set to "Device only".
In this case, I wasn't able to get a good connection of location and hence get Position Unavailable.
I change my Settings > Location > Mode and change it to High Accuracy.
It took me a lot of days to get this problem to work unknowingly that the problem is in the device itself. I did my best capability to fix the codes, and even traced back to the plugin's code.
Please respect my post. Some people might have this problem as well. Thank you.
I had the same error with Messenger webview, the location for Messenger was denied in phone settings so the error was resolved after allowing the location sharing for the Messenger app.
Just starting to get into HTML 5 and an testing out geo location...liking it so far. I am hitting a bit of a speed bump though...when I try to get my geo location, chrome automatically blocks the page from getting my location. This does not happen at other sites such as the site below:
http://html5demos.com/geo
The scripts I'm using:
<script type="text/javascript" JavaScript" SRC="geo.js"></script>
<script type="text/javascript" JavaScript" SRC="Utility.js"></script>
<script type="text/javascript" JavaScript" SRC="jquery.js"></script>
<script type="text/javascript" JavaScript" SRC="modernizr.js"></script>
function get_location() {
if (geo_position_js.init()) {
geo_position_js.getCurrentPosition(show_map, handle_error);
}
}
function show_map(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
alert("lat:" + latitude + " long:" + longitude);
}
function handle_error(err) {
alert(err.code);
if (err.code == 1) {
// user said no!
}
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(show_map, handle_error);
} else {
error('not supported');
}
I am testing this out from a local directory on my machine, so there isn't really a "domain" like "http://whatever.com/mytestpage.html". Is this why I am not getting prompted? If so, is it possible to force the browswer to request permission to get the user's geo location and is it possible in my scenario?
There's some sort of security restriction in place in Chrome for using geolocation from a file:/// URI, though unfortunately it doesn't seem to record any errors to indicate that. It will work from a local web server. If you have python installed try opening a command prompt in the directory where your test files are and issuing the command:
python -m SimpleHTTPServer
It should start up a web server on port 8000 (might be something else, but it'll tell you in the console what port it's listening on), then browse to http://localhost:8000/mytestpage.html
If you don't have python there are equivalent modules in Ruby, or Visual Web Developer Express comes with a built in local web server.
None of the above helped me.
After a little research I found that as of M50 (April 2016) - Chrome now requires a secure origin (such as HTTPS) for Geolocation.
Deprecated Features on Insecure Origins
The host "localhost" is special b/c its "potentially secure". You may not see errors during development if you are deploying to your development machine.
As already mentioned in the answer by robertc, Chrome blocks certain functionality, like the geo location with local files. An easier alternative to setting up an own web server would be to just start Chrome with the parameter --allow-file-access-from-files. Then you can use the geo location, provided you didn't turn it off in your settings.
The easiest way is to click on the area left to the address bar and change location settings there. It allows to set location options even for file:///
Make sure it's not blocked at your settings
http://www.howtogeek.com/howto/16404/how-to-disable-the-new-geolocation-feature-in-google-chrome/
if you're hosting behind a server, and still facing issues:
try changing localhost to 127.0.0.1 e.g. http://localhost:8080/ to http://127.0.0.1:8080/
The issue I was facing was that I was serving a site using apache tomcat within an eclipse IDE (eclipse luna).
For my sanity check I was using Remy Sharp's demo:
https://github.com/remy/html5demos/blob/eae156ca2e35efbc648c381222fac20d821df494/demos/geo.html
and was getting the error after making minor tweaks to the error function despite hosting the code on the server (was only working on firefox and failing on chrome and safari):
"User denied Geolocation"
I made the following change to get more detailed error message:
function error(msg) {
var s = document.querySelector('#status');
msg = msg.message ? msg.message : msg; //add this line
s.innerHTML = typeof msg == 'string' ? msg : "failed";
s.className = 'fail';
// console.log(arguments);
}
failing on internet explorer behind virtualbox IE10 on http://10.0.2.2:8080 :
"The current location cannot be determined"
For an easy workaround, just copy the HTML file to some cloud share, such as Dropbox, and use the shared link in your browser. Easy.
I too had this problem when i was trying out Gelocation API. I then started IIS express through visual studio and then accessed the page and It worked without any issue in all browsers.
Check Google Chrome setting and permit location access
Change your default location settings.
On your computer, open Chrome.
At the top right, click More Settings.
Under "Privacy and security," click Site settings.
Click Location.
Turn Ask before accessing on or off.
After I changed those settings, Geolocation worked for me.
today i realize that some pages can locate me (im using a laptop with no gps built-in) using google chrome with a an very impresive exactitude, i know that using my IP you can locate me, but not with such presision. Now in google maps you will see a button on top of the yellow men (street view) who locates you using google chrome.
How does this work? There is an API to use that?
It's also possible with the actual HTML 5 Geolocation functions (only if your browser supports):
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
alert("Not Supported!");
}
function success(position) {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
}
function error(msg) {
console.log(typeof msg == 'string' ? msg : "error");
}
var watchId = navigator.geolocation.watchPosition(function(position) {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
});
navigator.geolocation.clearWatch(watchId);
Greetings,
Sascha
Check out the help page
The local network information used by Google Location Services to estimate your location includes information about visible WiFi access points, including their signal strength; information about your local router; your computer's IP address. The accuracy and coverage of Google Location Services will vary by location.
Here is a link to the Firefox code that allows you to access the Google Location Service:
http://mxr.mozilla.org/mozilla1.9.1/source/dom/src/geolocation/NetworkGeolocationProvider.js
Enjoy!