簡體   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