I am using the GeoCoordinateWatcher class to get the latitude and longitude of the windows phone 7, but when I debug this application on my windows phone,
I am getting GeoPositionStatus.NoData in my StatusChanged event. Please tell me what is wrong with the following code.
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
watcher.MovementThreshold = 10.0f;
// wire up event handlers
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_statusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
void watcher_statusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
if (watcher.Permission == GeoPositionPermission.Denied)
{
// the user has disabled LocServ on their device.
statusTextBlock.Text = "You have disabled Location Service.";
}
else
{
statusTextBlock.Text = "Location Service is not functioning on this device.";
}
break;
case GeoPositionStatus.Initializing:
// The location service is initializing.
statusTextBlock.Text = "Location Service is retrieving data...";
break;
case GeoPositionStatus.NoData:
// The Location Service is working, but it cannot get location data
// due to poor signal fidelity (most likely)
statusTextBlock.Text = "Location data is not available.";
break;
case GeoPositionStatus.Ready:
// The location service is working and is receiving location data.
statusTextBlock.Text = "Location data is available.";
break;
}
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs <GeoCoordinate> e)
{
// update the textblock readouts.
latitudeTextblock.Text = e.Position.Location.Latitude.ToString("0.0000000000");
longitudeTextblock.Text = e.Position.Location.Longitude.ToString("0.0000000000");
speedreadout.Text = e.Position.Location.Speed.ToString("0.0") + " meters per second";
coursereadout.Text = e.Position.Location.Course.ToString("0.0") + " degrees";
altitudereadout.Text = e.Position.Location.Altitude.ToString("0.0") + " meters above sea level";
}
You should start the watcher.
watcher.Start(...);
I see that you've discovered that location services was turned off. But something to keep in mind is in certification they will check to see if your program can handle that scenario. When you start Location services you can then see if it is disabled.
watcher.Start();
bool IsLocationServicesTurnedOff = (watcher.Permission == GeoPositionPermission.Denied);
Also remember that in Mango your program can be suspended, location services disabled, and your program reactivated. In other words location services can perceivable be disabled at any time during the life of your program.
Related
I have a listener that captures the location every 10 seconds or 100 meters or so. I am using
xam.plugin.geolocator
to implement the listener. My problem is the location listener is not working(meaning the changes in location were not capturing or saved in the location cache) when my application is minimized or the application is opened but the phone is locked.
Here is my code:
async Task StartListening()
{
if (!CrossGeolocator.Current.IsListening)
{
var defaultgpsaccuracy = Convert.ToDouble(Preferences.Get("gpsaccuracy", String.Empty, "private_prefs"));
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(10), defaultgpsaccuracy, false, new Plugin.Geolocator.Abstractions.ListenerSettings
{
ActivityType = Plugin.Geolocator.Abstractions.ActivityType.Other,
AllowBackgroundUpdates = true,
DeferLocationUpdates = true,
DeferralDistanceMeters = 1,
DeferralTime = TimeSpan.FromSeconds(1),
ListenForSignificantChanges = true,
PauseLocationUpdatesAutomatically = false
});
}
}
I place this code in the first view/page of my application in my login.xaml.cs
Here are my questions:
How can I implement the listener properly so that when the application minimized or the phone/device is locked it still captures the changes of location?
What is the best GPS settings I need to capture the changes in location faster and accurately? Right now, my current settings are capturing the location every 10 seconds or 100 meters.
First you need to init StartListening then create event handlers for position changes and error handling
public Position CurrentPosition { get; set; }
public event EventHandler PositionChanged;
Don't forget to init it in your constructor :
CurrentPosition = new Position();
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(20), 10, true);
CrossGeolocator.Current.PositionChanged += PositionChanging;
CrossGeolocator.Current.PositionError += PositionError;
Functions :
`private void PositionChanging(object sender, PositionEventArgs e)
{
CurrentPosition = e.Position;
if (PositionChanged != null)
{
PositionChanged(this, null);
}
}
private void PositionError(object sender, PositionErrorEventArgs e)
{
Debug.WriteLine(e.Error);
}`
You can now call PositionChanged when ever you want the latest position
Don't forget to stop listening :
`public async Task StopListeningAsync()
{
if (!CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StopListeningAsync();
CrossGeolocator.Current.PositionChanged -= PositionChanging;
CrossGeolocator.Current.PositionError -= PositionError;
}`
I am working on Xamarin form app with andorid, UWP and Windows 8 project. I am using Geolocation plugin created by Jamesmontemagno to get the current device location. It is working fine in windows 8 and UWP but whenever I am trying to run it against the android device I keep getting task cancelled exception. I have checked all the permissions that are required as per suggestion but still no luck. My code to access location is below
protected override void OnAppearing()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
var position = locator.GetPositionAsync(timeoutMilliseconds: 60000).Result;
//var pp = helper.Setting.Location;
var Latitude = position.Latitude;
var Longitude = position.Longitude;
}
catch(Exception ex)
{
var exc = ex;
}
}
}
Below is an image for my settings for android manifest
For anyone else who gets a timeout even with the await, only on Android, and even though the device's Google Maps app works fine, you are probably running into this bug which only happens on certain Android devices, but quite a few of them at that.
The issue is an old one that Google has never fixed. The solution, and one possible reason the Google Maps app works fine, is to use Google Play Services' fused location provider.
Currently the Geolocator Plugin just uses the regular Android Location Provider, but James has mentioned that he would like to use the Fused provider at some point. I have yet to try the fused provider myself though.
Try using the await keyword like it is used in the original code:
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);
Console.WriteLine ("Position Status: {0}", position.Timestamp);
Console.WriteLine ("Position Latitude: {0}", position.Latitude);
Console.WriteLine ("Position Longitude: {0}", position.Longitude);
}
catch(Exception ex)
{
Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
}
This should take care that there are no race condition and therefore TaskCancellationException.
Thanks to #Radinator below is the working solution.
protected async override void OnAppearing()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
await SetLocation();
}
catch (Exception ex)
{
var exc = ex;
}
}
}
private async Task SetLocation()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
var position = await locator.GetPositionAsync(timeoutMilliseconds: 60000);
var Latitude = position.Latitude;
var Longitude = position.Longitude;
}
catch (Exception ex)
{
//log ex;
throw ex;
}
}
}
Faced 'Task killed' issue with v3.0.4. The following worked for me:
Uninstall the app
Update Geolocator to prerelease 4.0
Is there a way to detect if the phone, at the call try moment, can only make emergency calls (using Xamarin)?
This is something platform specific. Unfortunaelty I don't know any plugin for this, so you have to use the native API in Xamarin.
On Android it is the TelephonyManager as shown here: https://stackoverflow.com/a/14355706/1489968 It's Java, but can be easily translated to C#:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var telMng = (TelephonyManager) GetSystemService(TelephonyService);
var myPhoneStateListener = new MyPhoneStateListener();
myPhoneStateListener.ServiceStateChanged += (s, e) => Console.WriteLine("State: {0}", e);
telMng.Listen(myPhoneStateListener, PhoneStateListenerFlags.ServiceState);
}
}
public class MyPhoneStateListener : PhoneStateListener
{
public event EventHandler<ServiceState> ServiceStateChanged;
public override void OnServiceStateChanged(ServiceState serviceState)
{
base.OnServiceStateChanged(serviceState);
ServiceStateChanged?.Invoke(this, serviceState);
}
}
On iOS: Not sure if that information is available, never have seen it exposed via the SDK... (maybe add iOS tag to this question, or ask an iOS only question, answer might be in ObjC/Swift but you can translate it)
On Android: The info you are looking for is contained within the ServiceState of the phone:
var callState = new ServiceState ();
switch (callState.State) {
case PhoneState.InService:
{
var uri = Android.Net.Uri.Parse ("tel:555-2368"); // Jim Rockford's number ;-)
var intent = new Intent (Intent.ActionDial, uri);
StartActivity (intent);
break;
}
case PhoneState.EmergencyOnly:
{
Toast.MakeText (this, "Emergency Calls Only", ToastLength.Long).Show();
break;
}
case PhoneState.OutOfService:
{
Toast.MakeText (this, "Out of Service", ToastLength.Long).Show();
break;
}
case PhoneState.PowerOff:
{
Toast.MakeText (this, "Cell/Modem Power Off", ToastLength.Long).Show();
break;
}
default:
{
Toast.MakeText (this, "Should never be shown on a real device", ToastLength.Long).Show();
break;
}
}
Ref: http://developer.android.com/reference/android/telephony/ServiceState.html
For testing on the emulator, you can set the state to denied via the adb shell:
voice denied
data denied
Ref: https://developer.android.com/tools/devices/emulator.html
Issue
I'm trying to trigger a BackgroundTask when a Geofence Event (Enter / Exit) occurs in WP8.1 (WinRT). I've written a sample application to try to get it working, but can't seem to be able to do so.
So far, these are the steps I've taken to try to get Geofences working in the background:
Check for Location Capabilities
Create + Register a Geofence
Create + Register a BackgroundTask that listens for LocationTrigger(LocationTriggerType.Geofence);
In my background task, trigger a simple popup notification
Things I have done to Troubleshoot
I have enabled in my app.manifest:
Toast Capable => Yes
Capabilities: Location, Internet(Client &
Server)
Declarations: BackgroundTasks (Location). EntryPoint = BackgroundTask.GeofenceBackgroundTask
My background task is located in a separate project, titled BackgroundTask. It is a WindowsRT Component and contains one class GeofenceBackgroundTask.
Sample Project
The code for the project can be found at this [link](https://github.com/kiangtengl/GeofenceSample):
How To Test
Run the code in the emulator
Set Location to to: Latitude = 01.3369, Longitude = 103.7364
Click the Register Geofence + BackgroundTasks button
Exit the app (press the home button)
Change the current location to anywhere 100m away from the location you set previously. A notification should pop out.
Project Code:
Check for Location Capabilities
public static async Task GetLocationCapabilities()
{
try
{
var geolocator = new Geolocator();
await geolocator.GetGeopositionAsync();
var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
Debug.WriteLine("background access status" + backgroundAccessStatus);
}
catch (UnauthorizedAccessException e)
{
Debug.WriteLine(e);
}
catch (TaskCanceledException e)
{
Debug.WriteLine(e);
}
}
Create Geofence
public static void CreateGeofence(BasicGeoposition position, double radius, string id = "default")
{
// The Geofence is a circular area centered at (latitude, longitude) point, with the
// radius in meter.
var geocircle = new Geocircle(position, radius);
// Sets the events that we want to handle: in this case, the entrace and the exit
// from an area of intereset.
var mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited;
// Specifies for how much time the user must have entered/exited the area before
// receiving the notification.
var dwellTime = TimeSpan.FromSeconds(1);
// Creates the Geofence and adds it to the GeofenceMonitor.
var geofence = new Geofence(id, geocircle, mask, false, dwellTime);
try
{
GeofenceMonitor.Current.Geofences.Add(geofence);
}
catch (Exception e)
{
Debug.WriteLine(e);
// geofence already added to system
}
}
Register Background Task
public static async Task RegisterBackgroundTask()
{
try
{
// Create a new background task builder
var geofenceTaskBuilder = new BackgroundTaskBuilder()
{
Name = GeofenceBackgroundTaskName,
TaskEntryPoint = "BackgroundTask.GeofenceBackgroundTask"
};
// Create a new location trigger
var trigger = new LocationTrigger(LocationTriggerType.Geofence);
// Associate the location trigger with the background task builder
geofenceTaskBuilder.SetTrigger(trigger);
var geofenceTask = geofenceTaskBuilder.Register();
// Associate an event handler with the new background task
geofenceTask.Completed += (sender, e) =>
{
try
{
e.CheckResult();
}
catch(Exception error)
{
Debug.WriteLine(error);
}
};
}
catch(Exception e)
{
// Background task probably exists
Debug.WriteLine(e);
}
}
BackgroundTask Code to Trigger Toast
namespace BackgroundTask
{
public sealed class GeofenceBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var toastTemplate = ToastTemplateType.ToastText02;
var toastXML = ToastNotificationManager.GetTemplateContent(toastTemplate);
var textElements = toastXML.GetElementsByTagName("text");
textElements[0].AppendChild(toastXML.CreateTextNode("You have left!"));
var toast = new ToastNotification(toastXML);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
}
}
I've discovered that the above code sample, as well as the above code works. The problem that I was facing was that Windows Phone 8.1 does not automatically trigger a Geofence event. You have to wait a certain amount of time <5 mins before the BackgroundTask is triggered.
This applies to Geofencing in the foreground as well.
I'm busy with the same stuff, and I also noticed this behaviour, but for me its 2 mins.
Unfortunately it always triggers after 2 min, even, if there was no change in location and still inside the fence..
Declare a GeoCoordinateWatcher tracker. This tracker will start on PageOnload event as below.
I dont know how to create a condition such that it will show this :case GeoPositionStatus.NoData, So I need some advise. My objective is it will re-start until it gets the current Latitude and longitude.
Q1) for case GeoPositionStatus.NoData, what will be the best practice to re-start the GeoCoordinateWatcher?
Do this in case GeoPositionStatus.NoData
tracker.Stop();
tracker.Start();
void tracker_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
if (tracker.Permission == GeoPositionPermission.Denied)
{
MessageBox.Show("The location Service on this device is off.Please read privacy.");
}
else
{
MessageBox.Show("Location service is working but it can not get location data.");
}
break;
case GeoPositionStatus.Initializing:
// Code which needs to be initialized goes here
break;
case GeoPositionStatus.NoData:
//MessageBox.Show("Location data is not available.");
break;
case GeoPositionStatus.Ready:
// Code which needs to be executed when location data is available goes here
break;
}
}