PositionChanged & StatusChanged event getting fired twice in WP7 - windows-phone-7

I am trying to getting coordinates with the help of GPS and when i am putting a dry run or debug the event makes PositionChange & StatusChange to call TWICE. Here is my code please help me.
private void button1_Click(object sender, RoutedEventArgs e)
{
if (NetworkInterface.GetIsNetworkAvailable())
{
flag = true;
if (watcher == null)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 20;
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.Start();
}
}
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
MessageBox.Show("Location Service is not enabled on the device");
break;
case GeoPositionStatus.NoData:
MessageBox.Show(" The Location Service is working, but it cannot get location data.");
break;
}
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (e.Position.Location.IsUnknown)
{
MessageBox.Show("Please wait while your prosition is determined....");
return;
}
List<string> locationData = new List<string>();
locationData.Add(e.Position.Location.Latitude.ToString("Latitude:" + "0.000"));
locationData.Add(e.Position.Location.Longitude.ToString("Longitude:" + "0.000"));
locationData.Add(e.Position.Location.Altitude.ToString());
locationData.Add(e.Position.Location.Speed.ToString());
locationData.Add(e.Position.Location.Course.ToString());
}

It's called twice because the status changes from
Initializing -> Ready
For Initializing it fires once and For ready the second time :)

Related

Adding Xamarin GestureRecognizers on SKCanvasView

I have added Xamarin GestureRecognizers on a SKCanvasView and overridden OnTouch method.
I have some implementations on SKTouchAction.Moved. In order to trigger it after SKTouchAction.Pressed on Android I set e.Handled = true;
Doing so, none of the Xamarin GestureRecognizers seems to be working.
Is there any way to make both of these events work together or any alternative way to achieve this requirement?
Here is my code sample.
public abstract class GestureContainer : SKCanvasView, IViewportable
{
public GestureContainer()
{
var doubleTapGesture = new TapGestureRecognizer();
doubleTapGesture.NumberOfTapsRequired = 2;
doubleTapGesture.Tapped += TapGesture_DoubleTapped;
GestureRecognizers.Add(doubleTapGesture);
var pinchGesture = new PinchGestureRecognizer();
pinchGesture.PinchUpdated += OnPinchUpdated;
GestureRecognizers.Add(pinchGesture);
}
private void TapGesture_DoubleTapped(object sender, EventArgs e)
{
//Double tap action here
}
public void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
//Pinch action here
}
protected override void OnTouch(SKTouchEventArgs e)
{
switch (e.ActionType)
{
case SKTouchAction.Moved:
{
//Move action here
}
break;
case SKTouchAction.Pressed:
{
//Move action here
}
break;
}
e.Handled = false; // Xamarin gesture works when I set this to false
InvalidateSurface();
}
}

WebClient.DownloadStringAsync webexception wp7

I'm trying to remove a node from an xml file, everything works, but sometimes it happens that the xml web page does not load. this is my code:
private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
if (e.Error == null)
{
XDocument doc = XDocument.Parse(e.Result, LoadOptions.None);
var lyric = doc.Descendants(XName.Get("Lyric", "http://api.chartlyrics.com/")).FirstOrDefault();
TextBlock1.Text = lyric.Value;
}
}
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += HttpsCompleted;
wc.DownloadStringAsync(new Uri("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad));
}
I' ve read to use the WebException to handle this "mistake" but I am not able to use it. can someone give me some help?
Have you tried this way?
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
try
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += HttpsCompleted;
wc.DownloadStringAsync(new Uri("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad"));
}
catch (WebException ex)
{
// Check the exception here
}
}
And check for error in the handler too:
private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
if (e.Error == null)
{
XDocument doc = XDocument.Parse(e.Result, LoadOptions.None);
var lyric = doc.Descendants(XName.Get("Lyric", "http://api.chartlyrics.com/")).FirstOrDefault();
TextBlock1.Text = lyric.Value;
}
}
else
{
// Check for error here
}
}
I realized it sometimes return Error, and I think the problem is in the server, because if I access it from the web browser, I sometimes get the result, but many times I get an error.

GeoCoordinateWatcher isn't working after migration to WP8

I developed a navigation application for windows phone 7.1. It was running fine there. After updating to 8.0 my GeoCoordinateWatcher isnt working anymore. I know that I could use Geolocator instead, but I refuse to do so, because of lack of time.
For my app I read the current position of my watcher to store it for an object instance with location information. When I save my object instance the longitude and latitude are 0.0. Even when I change the position in my emulator still 0.0. The same issue occurs on my other pages, which use GeoCoordinateWatcher. It doesnt work. As I already said, on WP 7.1 - 7.8 it works very well.
public Map()
{
InitializeComponent();
watcher = new GeoCoordinateWatcher();
watcher.MovementThreshold = 20;
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (e.Position.Location.IsUnknown)
{
MessageBox.Show("Please wait while your prosition is determined.");
return;
}
geo.Latitude = e.Position.Location.Latitude;
geo.Longitude = e.Position.Location.Longitude;
}
try this code:
GeoCoordinateWatcher watch;
public GeoCoordinate loc = null;
public MainPage()
{
InitializeComponent();
if (watch == null)
{
watch = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold=10
};
watch.Start();
watch.PositionChanged += watch_PositionChanged;
}
}
void watch_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//throw new NotImplementedException();
Dispatcher.BeginInvoke(()=>LocUpdate(e));
}
void LocUpdate(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
try
{
location = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
}
catch
{
MessageBox.Show("Error");
}
}

PositionChanged event gives the old location details when application is restarted

I have the below code which works fine except that it gives the last location details it tracked when application is restarted.
And it gives the correct location details after I click on btnGetLocationDetails button 2 or 3 times.
Any idea how I can fix this issue so that every time app is launched user doesn't need to click on this button 2 or 3 times to get the correct current location?
Code:
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher = null;
Location location=null;
private void btnGetLocationDetails_Click_1(object sender, RoutedEventArgs e)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.Permission == GeoPositionPermission.Granted)
{
watcher.MovementThreshold = Convert.ToDouble("100");
}
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>(watcher_PositionChanged);
// PositionChanged events occur whenever your position changes
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged);
watcher.Start();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//get the coordinates
location = new Location();
location.Latitude = e.Position.Location.Latitude;
location.Longitude = e.Position.Location.Longitude;
location.Altitude = e.Position.Location.Altitude;
}
void watcher_OnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Disabled)
MessageBox.Show("The location service is turned off.");
else if (e.Status == GeoPositionStatus.NoData)
MessageBox.Show("No location data is available. ");
}
}

How can I use Bing Maps to get my current location?

My code is not working, it's show the whole world map. I just want to get current location and place a pushpin at it. I have been through Google and none of the examples works.
This is my code.
GeoCoordinateWatcher watcher;
// Constructor
public MainPage()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
if (watcher == null)
{
Console.WriteLine(watcher);
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 20;
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
}
watcher.Start();
}
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Pushpin pin = new Pushpin();
pin.Template = this.Resources["currentPushPin"] as ControlTemplate;
pin.Location = watcher.Position.Location;
mapControl.Items.Add(pin);
myMap.SetView(watcher.Position.Location, 15.0);
watcher.Stop();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (e.Position.Location.IsUnknown)
{
MessageBox.Show("Please wait while your prosition is determined....");
return;
}
Pushpin pin = new Pushpin();
myMap.Center = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
pin.Template = this.Resources["currentPushPin"] as ControlTemplate;
pin.Location = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
myMap.SetView(watcher.Position.Location, 18.0);
}
You start the GeoCordinateWatcher in PhoneApplicationPage_Loaded but this method never gets called. Add
Loaded+=PhoneApplicationPage_Loaded
at the end of the constructor.

Resources