我的手机上运行了以下测试代码。它在地图上显示了我的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);
}
}
}
}
答案 0 :(得分:0)
您可能需要在调用StartMonitoring而不是之后尝试设置事件处理程序。