refactor LocationService

This commit is contained in:
2011-04-04 18:22:03 +03:00
parent 2de2c2485e
commit f79ee0adc2
16 changed files with 236 additions and 78 deletions
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Device.Location;
using MyFriendsAround.WP7.Model;
namespace MyFriendsAround.WP7.Service
{
public interface ILocationService
{
event EventHandler<LocationChangedEventArgs> LocationChanged;
event EventHandler<LocationStatusEventArgs> StatusChanged;
Location CurrentLocation { get; }
Location GetCurrentLocation();
void Start();
void Stop();
}
}
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyFriendsAround.WP7.Model;
namespace MyFriendsAround.WP7.Service
{
public class LocationChangedEventArgs: EventArgs
{
public Location Location { get; private set; }
public LocationChangedEventArgs(Location location)
{
Location = location;
}
}
}
@@ -0,0 +1,70 @@
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyFriendsAround.WP7.Model;
using System.Device.Location;
namespace MyFriendsAround.WP7.Service
{
public class LocationService : ILocationService
{
private IGeoPositionWatcher<GeoCoordinate> _gpsWatcher;
public event EventHandler<LocationChangedEventArgs> LocationChanged;
public event EventHandler<LocationStatusEventArgs> StatusChanged;
public Location CurrentLocation { get; private set; }
public LocationService()
{
#if GPS_EMULATOR
_gpsWatcher = new GpsEmulatorClient.GeoCoordinateWatcher();
#else
_gpsWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold = 10
};
#endif
_gpsWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
_gpsWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
}
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs args)
{
//
if (StatusChanged != null)
StatusChanged(sender, new LocationStatusEventArgs(args.Status));
}
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> args)
{
if (LocationChanged != null)
LocationChanged(sender, new LocationChangedEventArgs(
new Location(args.Position.Location.Latitude, args.Position.Location.Longitude, args.Position.Timestamp)
));
}
public Location GetCurrentLocation()
{
return CurrentLocation;
}
public void Start()
{
_gpsWatcher.Start();
}
public void Stop()
{
_gpsWatcher.Stop();
}
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Device.Location;
namespace MyFriendsAround.WP7.Service
{
public class LocationStatusEventArgs : EventArgs
{
public GeoPositionStatus Status { get; private set; }
public LocationStatusEventArgs(GeoPositionStatus status)
{
Status = status;
}
}
}
@@ -16,8 +16,8 @@ namespace MyFriendsAround.WP7.Service
static ServiceAgent()
{
baseUrl = "http://myfriendsaround.cloudapp.net/myfriends";//live azure
//baseUrl = "http://127.0.0.1:8086/myfriends";//running in local azure emulator
//baseUrl = "http://myfriendsaround.cloudapp.net/myfriends";//live azure
baseUrl = "http://127.0.0.1:82/myfriends";//running in local azure emulator
//baseUrl = "http://localhost.:55672/myfriends";//for local asp.net mvc use
}