« MonkeyBread Software … | Home | Omegabundle for Xojo … »

Find computer location

Sometimes you may work on a project for macOS where it would be useful to know the location of the computer. You can use CoreLocation classes in MBS Xojo Plugins to query the location, but maybe it is not available, so lets check status.

We can ask locationServicesAvailable to check whether the API is available and use locationServicesEnabled to check whether it is enabled. Nowadays most Macs have location enabled, but sometimes in a cooperate environment, it may be turned off. If turned off, we can stop bothering asking the user.

Sub Init() If Not MyLocationManager.locationServicesAvailable Then MsgBox "Location Services are not available." end if if not MyLocationManager.locationServicesEnabled then MsgBox "Location Services are not enabled." end if End

You may have noticed, that we subclassed CLLocationManagerMBS as MyLocationManager and now we create an object of that class. The subclass is to be able to add the events. Once we have a new object and set the desired accuracy. In general it may be easier and quicker to get an idea on where you are. Not every software needs to know exact location, except maybe navigation software.

Sub Start() LocationManager=New MyLocationManager LocationManager.desiredAccuracy=CLLocationMBS.kCLLocationAccuracyNearestTenMeters LocationManager.startUpdatingLocation End

Let's fill in the didFailWithError event. This event fires when an error happens. You may see various error codes. This may include error code 1 for denied access. But we often see no error reported, but also no location. The application needs to be code signed and allowed by user to get the location.

Sub didFailWithError(error as NSErrorMBS) var l as listbox = MainWindow.List l.AddRow "Error:" l.AddRow error.localizedDescription l.AddRow error.localizedFailureReason l.AddRow error.localizedRecoverySuggestion End

Once a location is there, we get the didUpdate event called. There we can inspect the new location and output what information is comes with. In our example project, we output the values to a listbox to display them:

Sub didUpdate(newLocation as CLLocationMBS, oldLocation as CLLocationMBS) var l as listbox = MainWindow.List l.AddRow newLocation.description l.AddRow "Latitude: "+str(newLocation.latitude) l.AddRow "Longitude: "+str(newLocation.longitude) l.AddRow "Altitude: "+str(newLocation.altitude) l.AddRow "Course: "+str(newLocation.course) l.AddRow "HorizontalAccuracy: "+str(newLocation.horizontalAccuracy) l.AddRow "VerticalAccuracy: "+str(newLocation.verticalAccuracy) l.AddRow "Speed: "+str(newLocation.speed) l.AddRow "Timestamp: "+newLocation.timestamp.LongDate+" "+newLocation.timestamp.LongTime End

You may need to build the application to make it work. Please have an Info.plist file i the project with the text message for the NSLocationUsageDescription key. This text gets shown in the dialog to the user about whether to allow the location access.

Please try and see if the current position can help.

19 09 24 - 14:13