public override void ViewDidLoad() { base.ViewDidLoad(); // you can set the update threshold and accuracy if you want: //locationManager.DistanceFilter = 10d; // move ten meters before updating //locationManager.HeadingFilter = 3d; // move 3 degrees before updating // you can also set the desired accuracy: locationManager.DesiredAccuracy = 1000; // 1000 meters/1 kilometer // you can also use presets, which simply evalute to a double value: //locationManager.DesiredAccuracy = CLLocation.AccuracyNearestTenMeters; locationManager.Delegate = this; locationManager.RequestWhenInUseAuthorization(); if (CLLocationManager.LocationServicesEnabled) { locationManager.StartUpdatingLocation(); } if (CLLocationManager.HeadingAvailable) { locationManager.StartUpdatingHeading(); } }
void requestLocationAsync(bool askPermissionIfNecessary) { // is not authorized? if (CoreLocation.CLLocationManager.Status == CLAuthorizationStatus.Restricted || CoreLocation.CLLocationManager.Status == CLAuthorizationStatus.Denied) { Task.Run(() => { Location = null; // location is not known LocationServicesStatus = LocationServicesStatusEnum.NotAuthorized; FailureInfo = "Not authorized to use location services"; fireEvents(); }); return; } // initialize initLocationManager(); // request permission if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0) == true && CoreLocation.CLLocationManager.Status == CLAuthorizationStatus.NotDetermined) { if (askPermissionIfNecessary == false) { return; // don't do anything if the caller doesn't want to ask for permission } locationManager.AuthorizationChanged += (s, e) => { if (e.Status == CLAuthorizationStatus.NotDetermined) { return; // in progress } if (e.Status == CLAuthorizationStatus.Authorized || e.Status == CLAuthorizationStatus.AuthorizedAlways || e.Status == CLAuthorizationStatus.AuthorizedWhenInUse) { LocationServicesStatus = LocationServicesStatusEnum.Ok; this.tryCheckingNow(); } else { Location = null; // location is not known LocationServicesStatus = LocationServicesStatusEnum.NotAuthorized; FailureInfo = "Permission to use use location services not given"; fireEvents(); } }; locationManager.RequestWhenInUseAuthorization(); return; } // check location this.tryCheckingNow(); }
public EnableLocation() { ViewController vc = new ViewController("RestaurantMap"); Maps map = vc.getMap(); CoreLocation.CLLocationManager locationManager = new CoreLocation.CLLocationManager(); locationManager.RequestWhenInUseAuthorization(); map.IsShowingUser = true; // add an annotation /*map.AddAnnotations (new MKPointAnnotation (){ * Title="MyAnnotation", * Coordinate = new CoreLocation.CLLocationCoordinate2D(42.364260, -71.120824) * });*/ }
// // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { // Member variable in the class // Save a reference to the CLLocationManager so that it stays in scope // Some method in the class // Request geoloation authorization locationManager = new CoreLocation.CLLocationManager(); if (locationManager.RespondsToSelector (new ObjCRuntime.Selector("requestWhenInUseAuthorization"))) { locationManager.RequestWhenInUseAuthorization (); } global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); return base.FinishedLaunching(app, options); }