GeoCoordinateWatcher location not returning speed. NaN instead - windows-phone-7

I've been experimenting with getting readings from the GPS with my wp7.
I'm using very similar code to the sample found here:
Getting GPS coordinates on Windows phone 7
I've got 2 textblocks on the page. One showing the speed from Position.Location.Speed and one showing the timestamp from Position.Location.TimeStamp. This is triggered from the watcher_PositionChanged event.
I notice the event being triggered every second or so, as the timestamp updates to reflect this. I'm also reading the GPS status as "ready" (the first couple of readings are "No Data") However, the speed value continues to display NaN.
I loaded this code onto a physical device (LG Optimus 7), and drove down the street with the phone on the dashboard to test.

OK, I realized that GeoCoordinateWatcher runs on the UI thread and it was acting a bit.. strange. Moving this off into a separate thread fixed the problem.

Try to do it with that way:
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// you cannot change the UI in this function -> you have to call the UI Thread
Deployment.Current.Dispatcher.BeginInvoke(() => ChangeUI(e));
}
void ChangeUI(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// do magic
}

Related

How to get the position of touch in a simple windows phone 8 app?

I am making an app for windows phone 8.
On one of the pages of the app, I want to get the co-ordinates of the touch.
I tried searching on net but I got results related to games.
I want to know is it possible to get the position of touch on canvas/or any other component in a simple app.
If yes, plz guide me through it.
Hook up to Tap event of the Control, and use GetPosition of event args.
<Canvas Tap="HandleTap/>
private void HandleTap(object sender, GestureEventArgs e)
{
Point point = e.GetPosition(null);
}

Windows phone calculate distance

I am working on windows phone app.
public void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs e)
{}
Does this event work on every position change when application run in background not in foreground?
I want to calculate traveled distance at every 10 seconds while app run in background.How can i do it?
You can't. Microsoft is very specific on what is allowed to run in the background.
See this article for more info.

Picking photo from Photochooser or holding hardware back button cause active timers ring

This is a very strange problem, and all the people I had asked to confirm it said that it takes place.
I have a Threading.Timer instance which fires every 15 minutes. And if I call the PhotoChooser view and then select a photo from it, when going back to the calling page my application calls that timer's callback! I tried different timers either it be Timer from Threading namespace or Dispatcher timer.
The same happens when being in my app I hold the back button of my device and then choose the app from the list.
My application is as plain as it can be - the timer with a callback and method calling PhotoChooser. Could anyone help with solution or workaround please?
Update:
My code construction is as follows:
private Timer _timer;
public void CallTimer()
{
var period = 15 * 1000 * 60;
_timer = new Timer(repeatTimer_Tick, null, 0, period);
}
private void repeatTimer_Tick(object state)
{
// Some action here
}
private void Stop()
{
if (_timer != null)
_timer.Dispose();
}
private void CallPhotoChooser()
{
// Some basic actions calling photochooser task
}
As explained in Windows Phone 7 Tombstoning the application is most likely tombstoned when the user presses and holds the back button or a launcher, like the PhotoChooserTask, is called. This happens unless the page is returned to in a matter of seconds.
You need to store the timer timeout in your application state somehow, or set the initial timeout to 15 minutes so it doesn't fire immediately. To store the application state, take a look at the article linked, it recommends doing this in the NavigatedFrom event which you can overload in the page code behind.
The time left before the timer fires is a bit more difficult. I guess to know how long it's left of a timeout you need to get the time with DateTime.UtcNow (which you can store in the application state) when creating the timer and calculate the next time it will fire upon resuming the application.
You should not be creating those long running timers :) Just handle the activation/deactivation and reset your timers, then reinstate them when photo chooser returns you back to your app.

DispatcherTimer not updating with AdControl running

I have a simple app that I am creating with a countdown timer that uses a DispatcherTimer for the time base. I have an event handler setup for On_Tick of the DispatcherTimer (set for 1 sec interval). I have three (3) pivot pages using three different instances of AdControl and all are "live" with a real ApplicationID and AdUnitID. This timer is setup on one of the pivot pages.
What I am seeing is that when I open my app and the AdControl starts, after 60 seconds, the adControl wants to refresh. My timer works fine for the first minute, then starts to lose a second every three seconds, like it is missing a tick event (coincidentally when the adcontrol "scrolls" to a new message every three seconds?). I've tried using a background worker for the dispatcherTimer but that did not seem to do anything for me. The code in the event handler is fairly short, with just a couple of "if-then" statements and a few textBlock updates.
Anyone else seen similar issues with the AdControl?
I would say the reason is that the ad control and the timer both want to do something on the UI thread. Thus when the ad control is busy the timer action is blocked during this time. To quote MSDN:
Timers are not guaranteed to execute exactly when the time interval
occurs, but they are guaranteed to not execute before the time
interval occurs. This is because DispatcherTimer operations are placed
on the Dispatcher queue like other operations. When the
DispatcherTimer operation executes is dependent on the other jobs in
the queue and their priorities.
It also explains why using a background worker does not help. As soon as you go back from another thread to the UI thread you have the same problem again. So this issue is basically by design.
Oh and it can also be the other way round. If you would be doing intensive work in the UI thread then the ad control would be blocked. As well as the rest of your UI. This is the reason why you should do as much work as possible in background threads. Maybe the ad control doesn't adhere to this advice.
So far this probably won't help you much. But perhaps it is possible to just use one AdControl and move this from Pivot to Pivot as the user pans around?
I've experienced the same problem with my own timer style app. In my case it only appears to happen when there is animation in the current advertisement.
According to the DispatcherTimer documentation, the delay is expected behaviour, so the solution it to use a different timer... eg System.Threading.Timer
...
//create the timer
var timer = new System.Threading.Timer(
new System.Threading.TimerCallback(TimerTick),
null,
//Set the due time to infinite so the timer wont start immediately
System.Threading.Timeout.Infinite,
0);
//start the timer
timer.Change(0, 1000);
//stop the timer
timer.Change(System.Threading.Timeout.Infinite, 0);
}
void TimerTick(object state)
{
//Dont forget to update the UI on the UI thread.
Dispatcher.BeginInvoke(() =>
{
MyTextBox.Text = "New Text";
});
}
Problem solved!

Is there any way to capture an image using the camera, programatically, without the user pressing a button?

I wish to take a picture on a timer, and process the image at regular intervals, but cannot find anything.
Any help would be appreciated,
Cheers.
There's no way for 3rd party apps to take a picture without the users interaction in this v1 release of the 3rd Party SDK. AR capability is on the radar for the platform team though, so watch this space.
Picture taking capability is currently provided through CameraCaptureTask.
FYI: In Windows Phone OS 7.1 (a.k.a. "Mango"), you can now programmatically capture an image from the camera using the PhotoCamera class. Fire-off a camera capture using the CaptureImage method. When the capture is available, you can access the image (and a thumbnail) from the arguments in the eventhandlers for CaptureImageAvailable and CaptureThumbnailAvailable.
This process is fully described in the following topic:
How to: Create a Base Camera Application for Windows Phone
In that sample, a button is used to trigger the call to CaptureImage, but in a real-world application a timer, like you suggested, would be much more appropriate. (we recommend using the hardware button for user-triggered photos, rather than a UI button. That is described here: How to: Access the Hardware Camera Shutter Button).
Here's the method that actually programmatically triggers the image capture, where cam is the PhotoCamera object:
private void ShutterButton_Click(object sender, RoutedEventArgs e)
{
// Capture a still image. Events are fired as the thumbnail
// and full resolution images become available.
try
{
cam.CaptureImage();
}
catch (Exception ex)
{
this.Dispatcher.BeginInvoke(delegate()
{
// Cannot capture an image until the previous capture has completed.
txtDebug.Text = ex.Message;
});
}
}
Note: PhotoCamera will throw an exception if you try to capture when another capture is in progress. You probably wouldn't have this problem with a timer-based app, but that is why the try/catch is used here. Also, BeginInvoke is used to access the UI thread and display the message in a textBlock on the corresponding page.
Hope that helps. Cheers

Resources