Event for Geolocation Changed in Windows Phone 8 - c#

Is there any event for the geolocation changed in Windows Phone 8 (C#)?
I want to trigger some event when the geolocation is changed like city from reversegeocoding changed.
If not, then is it possible to call some event manually whenever the Phone's location like City is Changed.
(suppose I have fetched the city using reverse geocoding). (for Windows Phone 8)

I can't understand your question properly, But this answer may solve your problem.
There is a event called "PositionChanged" in geolocator. This event gets triggered when geolocator position is changed.
geolocator = new Geolocator();
geolocator.PositionChanged -= geolocator_PositionChanged;
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString("0.00");
LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0.00");
});
}
for more info :::http://msdn.microsoft.com/en-us/library/windows/apps/jj247548(v=vs.105).aspx

Related

Detect changes in Wifi

I need to detect when wifi is turned on/off. For that purpose I'm using Connectivity by James Montemagno, but the problem is that I don't get an ConnectivityChanged event if the Phone have access to mobile network and I turn on/off the wifi.
Here is the mapping of the event:
CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
{
WiFiConnected = CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi);
};
So can I detect Connectivity Changed on Wifi? I would like to do it in Xamarin Forms code so I won't have to implement a solution for each platform.
Here What Your Looking For
CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
{
if (args.IsConnected.ToString().Equals("False"))
{
if (CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi))
{
// WE LOST AN CONNECTION BUT WIFI IS STILL ON
}
}
else
{
if (CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi))
{
// WIFI WAS TURN ON AND WE HAVE A CONNECTION
}
else
{
// WE HAVE A CONNECTION BUT NOT WIFI
}
}
};
I dont know if there is a xamarin forms solution but you can do it plateform specific. In android with BroadcastReceiver.. for other plateforms i have no idea..

Accelerometer Shaken event in windows universal app doesn't work

I try to detect a shake on my phone with the Shaken event from Accelerometer object. The accelerometer object is not null but when I shake the phone, it never go in the _accelerometer_Shaken event.
public int shakeCount = 0;
Accelerometer _accelerometer = Accelerometer.GetDefault();
public MainPage()
{
this.InitializeComponent();
if (_accelerometer != null)
{
_accelerometer.Shaken += _accelerometer_Shaken;
}
}
async private void _accelerometer_Shaken(Accelerometer sender, AccelerometerShakenEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
shakeCount++;
tbInfo.Text = shakeCount.ToString();
});
}
I don't understand why
This feature is not supported yet.
This is an extract from the official sample code
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Accelerometer
Accelerometer Shake Events
When you choose the Enable button for the Shake Events option, the app
displays the cumulative number of shake events each time an event
occurs. (The app first increments the event count and then renders the
most recent value.)
Note that shake events are not supported in Windows 10 build 10240, so
the Shaken event will never be raised, but the sample demonstrates how
to handle the event when support for shake is added.
I made a test under Windows 10 10586 and it still does not work.

how to get the current gps coordinates in xamarin

I would like to get the user's current latitude and longitude for ios in xamarin.
I've looked at other posts and as i understand it.
it's like:
CLLocationManager lm = new CLLocationManager();
... (Acurray)
... (Other Specs)
lm.StartUpdatingLocation();
lm.Location.Coordinate.Latitude.ToString();
lm.Location.Coordinate.Longitude.ToString();
However, when i run it in the simulator it doesn't do anything. It doesn't ask me to turn on my location services or update my Label with the latitude and longitude.
Also, how do i make it check it location services are turned on and if they're not. How to make the user do it. (like the pop up on some apps asking you to turn on your gps)
CLLocationManger is async - it will fire events when the location is updated.
CLLocationManager lm = new CLLocationManager(); //changed the class name
... (Acurray)
... (Other Specs)
lm.LocationsUpdated += delegate(object sender, CLLocationsUpdatedEventArgs e) {
foreach(CLLocation l in e.Locations) {
Console.WriteLine(l.Coordinate.Latitude.ToString() + ", " +l.Coordinate.Longitude.ToString());
}
};
lm.StartUpdatingLocation();

call method when phone is shaken [duplicate]

This question already has answers here:
Is there an easy way to detect shake motions on Windows Phone 8?
(3 answers)
Closed 8 years ago.
I have created a windows phone app that locates a users location when a button is pressed but I want to do away with the button and make this function occur when the phone is shaken! Below is the code that I have created so far, when the application loads it will call a function called Locate_Me which initializes the Accelerometer.
private async void Locate_Me()
{
if (accelerometer == null)
{
// Instantiate the Accelerometer.
accelerometer = new Accelerometer();
accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
accelerometer.CurrentValueChanged +=
new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);
}
try
{
statusTextBlock.Text = "starting accelerometer.";
accelerometer.Start();
}
catch (InvalidOperationException ex)
{
statusTextBlock.Text = "unable to start accelerometer.";
}
}
So how would I go about making the onShaken function?
First step: Download ShakeGestures library from microsoft site here. Add ShakeGetures.dll to your project.
Now it's a piece of cake for you to detect shake gestures. Below is the code you can use:
//constructor of page register event handler for shake
public Page1()
{
InitializeComponent();
// register shake event
ShakeGesturesHelper.Instance.ShakeGesture +=new
EventHandler<ShakeGestureEventArgs>(Instance_ShakeGesture);
// optional, set parameters
ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 2;
// start shake detection
ShakeGesturesHelper.Instance.Active = true;
}
private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
//call your method
}
This is the minimal code you would require. Worked for me.

Windows Phone 8 Geolocator can't set desiredAccuracy = High AND tie into PositionChanged event

background: I'm well versed in WPF/XAML, but new to Windows Phone 8.
Hopefully there is just something stupid that I'm missing...
I want DesiredAccuracy to be high, but I also want to hook into the PositionChanged event.
When the below code reaches _GeoLocator.DesiredAccuracy = PositionAccuracy.High; it throws an abort. If it off, everything works but I really want high accuracy.
It seems the two are mutually exclusive of one another.
Error message is: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT)). I have Location capabilities enabled.
Example of offending code:
public MainPage()
{
InitializeComponent();
_GeoLocator.MovementThreshold = 1;
_GeoLocator.PositionChanged += (Geolocator sender, PositionChangedEventArgs args) =>
{
//UpdateLocation(args);
Console.WriteLine("Position Changed");
};
//THIS WILL THROW...WHY?? IF I COMMENT OFF POSITIONCHANGED ABOVE, IT WORKS FINE.
_GeoLocator.DesiredAccuracy = PositionAccuracy.High;
}
You have to set "DesiredAccuracy" before "PositionChanged" event handler (Similar question).
_GeoLocator.MovementThreshold = 1;
_GeoLocator.DesiredAccuracy = PositionAccuracy.High;
_GeoLocator.PositionChanged += (Geolocator sender, PositionChangedEventArgs args) =>
{
//UpdateLocation(args);
Console.WriteLine("Position Changed");
};

Categories

Resources