GeoFence and #WinRT API

综合技术 2016-06-02

For some reason I find myself doing some Windows Phone 8.1 development in Silverlight.. I know its a dead end and all the rest but I am working on a small app and Silverlight API was more mature at least for Windows Phone 8.1. One of the things I was adding was GeoFencing. GeoFencing was introduced to Windows Phone 8.1 WinRT API and some of it was exposed to Silverlight API (whoever made the decision on what APIs were available in WinRT and what in Silverlight needs to be shot.. at least made to feel real pain.

The way you set it up is you create GeoFence
instances and add them.. System managed associated with your app. The app can access it from foreground or background.. nice right ? The constructor for GeoFence states that it requires IGeoshape along with an identifier at the very least.. IGeoshape is an enum which has possible values of Point, Circle, BoundingBox and Path. The constructor for GeoFence explicitly states now and again that the IGeoshape has to be a valid GeoCircle.. I know documentation can be wrong.. I mean why would you go through the hassle of create an Interface and passing it again if you enforce explicit type ?

I was wrong.. I tried it both the Silverlight and through WinRT API.. nope documentation is correct.. the API though designed well has shite implementation:neutral_face: So what next ?

Well DIY. Here is my implementation.. Create a Rect instance with NorthWest X1, Y1 and SouthEast and X2 Y2 and monitor the change in geo position. Check if the new point is within Rect or not.. easy peasy.. well it works too.. Here is my implementation.

public class GeoMonitoringService
{
    Geolocator _locator = null;
    Dictionary _locationRectangles = null;

    DataService _dataService = null;

    public event RoutedPropertyChangedEventHandler GeomonitorStateChanged;
    public bool lastState = false;

    public void Initialise(DataService dataService)
    {
        _locator = new Geolocator();
        _locator.DesiredAccuracy = PositionAccuracy.High;
        _locator.MovementThreshold = 1;
        _locator.ReportInterval = Convert.ToUInt32(TimeSpan.FromSeconds(5).TotalMilliseconds);

        this._locationRectangles = new Dictionary();

        this._dataService = dataService;

        if (this._dataService?.SiteDictionary?.Count > 0)
        {
            foreach (var site in this._dataService.SiteDictionary.Values)
            {
                Rect r = new Rect(new Point(site.Latitude1, site.Longitude1), new Point(site.Latitude2, site.Longitude2));
                this._locationRectangles.Add(site, r);
            }
        }

        _locator.PositionChanged += GPSPositionChanged;
    }

    private void GPSPositionChanged(Geolocator sender, PositionChangedEventArgs args)
    {
        var geoPoint = args.Position.Coordinate.Point;
        Point p = new Point(geoPoint.Position.Latitude, geoPoint.Position.Longitude);

        KeyValuePair? matchKVP = null;
        foreach (var entry in this._locationRectangles)
        {
            if (entry.Value.Contains(p))
            {
                matchKVP = entry;
                break;
            }
        }

        if (matchKVP.HasValue)
        {
            if (!lastState)
            {
                lastState = true;
                this.GeomonitorStateChanged?.Invoke(this, new RoutedPropertyChangedEventArgs(null, new GeomonitorStateChangedEventArgs(true, matchKVP.Value.Key)));
            }
        }
        else
        {
            if (lastState)
            {
                lastState = false;
                this.GeomonitorStateChanged?.Invoke(this, new RoutedPropertyChangedEventArgs(null, new GeomonitorStateChangedEventArgs(false, null)));
            }
        }
    }
}

责编内容by:Invoke IT Limited (源链)。感谢您的支持!

您可能感兴趣的

The Patent Application Information Retrieval Bulk ... I stumbled across the Patent Application Information Retrieval Bulk Data API from the US Patent Office the other day. It provides a much more us...
亚马逊联盟API上手指南 – Amazon.cn Product Advertising API q... 该指南旨在帮助使用亚马逊联盟Product Advertising API的开发人员快速上手,开发出一个可以运行的样例程序。该指南的目标读者是对WebService、RESTful Service有一定了解的Java开发人员。 主要步骤: 注册亚马逊联盟账号; 注册联盟开发...
八步部署NGINX Plus API网关 欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文来自云+社区翻译社,作者ArrayZoneYour Nginx往往是构建微服务中必不可缺的一部分,从本文中你可以习得如何使用Nginx作为API网关。 HTTP API是现代应用架构的核心。HTTP协议使开发者...
API Gateway as a Node and Moving Beyond Backend an... The more I study where the API management, gateway, and proxy layer is headed, the less I’m seeing a front-end or a backend, and I’m seeing just a nod...
How Lucas Systems Integrated Swagger OpenAPI Defin... Now available on docslikecode.com, this explanation of how to integrate Swagger with Swashbuckle for a REST API written with C#. A Pirate’s Life...