繁体   English   中英

Xamarin IOS无法使GeoFencing正常工作

[英]Xamarin IOS Can't get GeoFencing to work

我的手机上正在运行以下测试代码。 它在我的GeoFence所在的地图上显示了一个圆圈。 如果我走出栅栏,那么返回然后退出事件永远不会触发。 我不知道我缺少什么:

using MonoTouch;
using CoreLocation;
using MapKit;
using System.Drawing;
using CoreGraphics;

namespace GeoFencingTest
{
    public class GeoFencingController : UIViewController
{
    UIWindow Parent;
    CustomMapView mapView;
    CLLocationManager locMgr = new CLLocationManager ();
    UITextView textview;
    UIButton button;
    CLCircularRegion region;
    CLLocationCoordinate2D FenceCenter;


    public GeoFencingController (UIWindow parent)
    {
        var radius = 25;
        FenceCenter = new CLLocationCoordinate2D (33.8399522334765, -84.3729872355209);

        Parent = parent;
        View = new UIView (new RectangleF ((float)Parent.Bounds.Left, (float)Parent.Bounds.Top, (float)Parent.Bounds.Width, (float)Parent.Bounds.Height));
        parent.AddSubview (View);

        locMgr.RequestWhenInUseAuthorization ();
        locMgr.DesiredAccuracy = 5;
        locMgr.StartUpdatingLocation ();

        region = new CLCircularRegion (FenceCenter, radius, "TPM");
        locMgr.StartMonitoring (region);

        mapView = new CustomMapView ();
        mapView.Frame = new RectangleF ((float)Parent.Bounds.Left + 20, (float)55, (float)Parent.Bounds.Width - 40, (float)Parent.Bounds.Height - 80);
        mapView.ShowsUserLocation = true;
        mapView.UserInteractionEnabled = true;
        mapView.MapType = MKMapType.Hybrid;
        mapView.ZoomEnabled = true;
        mapView.ScrollEnabled = true;
        mapView.UserTrackingMode = MKUserTrackingMode.Follow;
        mapView.Delegate = new MapDelegate ();
        mapView.CenterCoordinate = FenceCenter;

        View.AddSubview (mapView);

        var circleOverlay = MKCircle.Circle (new CLLocationCoordinate2D (FenceCenter.Latitude, FenceCenter.Longitude), radius);
        mapView.AddOverlay (circleOverlay);

        textview = new UITextView (new RectangleF (0, 20, 190, 50));
        View.AddSubview (textview);


        if (CLLocationManager.IsMonitoringAvailable (typeof(CLCircularRegion))) {

            locMgr.DidStartMonitoringForRegion += (o, e) => {
                textview.Text = "Now monitoring region " + e.Region.ToString ();
            };

            locMgr.RegionEntered += (o, e) => {
                textview.Text = "Just entered " + e.Region.ToString ();
            };

            locMgr.RegionLeft += (o, e) => {
                textview.Text = "Just left " + e.Region.ToString ();
            };


        } else {
            textview.Text = "This app requires region monitoring, which is unavailable on this device";
        }
    }

    public class MapDelegate : MKMapViewDelegate
    {


        public override MKOverlayView GetViewForOverlay (MKMapView mapView, NSObject overlay)
        {
            // return a view for the polygon
            MKCircle circle = overlay as MKCircle;
            MKCircleView circleView = new MKCircleView (circle);
            circleView.FillColor = UIColor.Yellow;
            circleView.Alpha = 0.5f;
            circleView.LineWidth = 10;
            circleView.StrokeColor = UIColor.Red;
            return circleView;
        }
    }

    public class CustomMapView : MKMapView
    {
        public override void TouchesBegan (NSSet touches, UIEvent evt)
        {
            base.TouchesBegan (touches, evt);

            UITouch touch = touches.AnyObject as UITouch;
            CGPoint pointInView = touch.LocationInView (this);
            CLLocationCoordinate2D touchCoordinates = base.ConvertPoint (pointInView, this);
            MKMapPoint mapPoint = MKMapPoint.FromCoordinate (touchCoordinates);
            Console.WriteLine ("LAT: " + touchCoordinates.Latitude);
            Console.WriteLine ("LON: " + touchCoordinates.Longitude);

        }
    }

}

}

您可能想尝试在调用StartMonitoring之前而不是之后设置事件处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM