在Xamarin IOS上显示Map时,永远不会调用DidUpdateUserLocation事件

时间:2017-11-09 19:11:21

标签: ios xamarin mkmapview

我目前正试图在Xamarin IOS上使用MKMapView显示我的位置而没有太大的成功。永远不会调用DidUpdateUserLocation事件,因此不提供位置坐标。我在Info.plist文件中使用以下权限:

NSLocationWhenInUseUsageDescription 位置

[Register("MapsViewController")]
   public class MapsViewController : UIViewController {
        private MKMapView mapView;
        private CLLocationManager locationManager = newCLLocationManager();
        private CLLocationCoordinate2D coords;

        public override void ViewDidLoad() {
            base.ViewDidLoad();
            mapView = new MKMapView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height));
            mapView.ShowsUserLocation = true;
            this.View.AddSubview(mapView);

            locationManager.RequestWhenInUseAuthorization();
            mapView.ShowsUserLocation = true;

            mapView.DidUpdateUserLocation += (sender, e) => {

                if (mapView.UserLocation != null) {
                    coords = mapView.UserLocation.Coordinate;
                    MKCoordinateSpan span = new MKCoordinateSpan(MilesToLatitudeDegrees(2),
                        MilesToLongitudeDegrees(2, coords.Latitude));
                    mapView.Region = new MKCoordinateRegion(coords, span);
                }
            };
        }


        public double MilesToLatitudeDegrees(double miles) {
            double earthRadius = 3960.0;
            double radiansToDegrees = 180.0 / Math.PI;
            return (miles / earthRadius) * radiansToDegrees;
        }

        /// <summary>
        /// Converts miles to longitudinal degrees at a specified latitude
        /// </summary>
        public double MilesToLongitudeDegrees(double miles, double atLatitude) {
            double earthRadius = 3960.0;
            double degreesToRadians = Math.PI / 180.0;
            double radiansToDegrees = 180.0 / Math.PI;

            // derive the earth's radius at that point in latitude
            double radiusAtLatitude = earthRadius * Math.Cos(atLatitude * degreesToRadians);
            return (miles / radiusAtLatitude) * radiansToDegrees;
        }

0 个答案:

没有答案