iOS Tutorial Location & GPS

Status
Not open for further replies.
The iLocation library allows you to get the current known location and to monitor location updates. You can also monitor the device heading direction.

As the location is considered a private information, you can only get the location if the user has allowed you to get the location. This type of authorization is different than Android permissions system. At runtime the user can decide whether to allow your app do use this service or not.

SS-2014-10-27_15.33.40.png


The AuthorizationStatusChanged event is raised after you initialize LocationManager. It will also be raised after the user has changed the authorization status from the device settings (Under Privacy - Location Services).

The authorization status can be in one of the following states:
- Not determined - This will be the status when the user first runs your app. You should call LocationManager.Start. The user will be asked to approve it.
- Authorized - The user has already allowed your app to use this service.
- Not authorized - You need to ask the user to go to the settings page and enabled this service.

B4X:
Private Sub LocManager_AuthorizationStatusChanged (Status As Int)
   lblEnable.Visible = (LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_DENIED _
     OR LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_RESTRICTED)
   StartLocationUpdates
End Sub

Private Sub StartLocationUpdates
   'if the user allowed us to use the location service or if we never asked the user before then we call LocationManager.Start.
   If LocManager.IsAuthorized OR LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_NOT_DETERMINED Then
     LocManager.Start(0)
   End If
   LocManager.StartHeading
End Sub

Note that you can safely call Start multiple times.

Once we are authorized to use the location service we can start to work with the LocationChanged event.

It will fire once with the last known location and then whenever there is a new known location.

SS-2014-10-27_15.53.21.png


Notes
-
Monitoring the device heading doesn't require any special permission.
- If the heading accuracy is important then you need to handle the AllowCalibration event and return true:
B4X:
Sub LocManager_AllowCalibration As Boolean
   Return True
End Sub
The device built-in calibration screen may show if calibration is required.
- You need to add the following two lines to your app:
B4X:
  #PlistExtra:<key>NSLocationWhenInUseUsageDescription</key><string>Used to display the current navigation data.</string>
   #PlistExtra:<key>NSLocationUsageDescription</key><string>Used to display the current navigation data.</string>
This message will be displayed to the user, explaining why the location services are required. The first line is for iOS 8+ and the second is for iOS 7.
 

Attachments

  • LocationExample.zip
    3.5 KB · Views: 2,430
Last edited:

Baris Karadeniz

Active Member
Licensed User
So I Understand that, altough GPS is NOT turn on, app will show the location by using wifi. And if Gps and wifi are not tun on, still location will be seen by geolocation. Am I right?
 

Baris Karadeniz

Active Member
Licensed User
The locations service in iPhone works in one of the three ways, from cell triangulation, wifi hot spots or the GPS.

I mean cell triangulation. In other words, if user give permission and when the app is turned on, location service will run in background and foreground. There is only one way to stop location servise, that is "disable the app". Is it correct?
 

JanPRO

Well-Known Member
Licensed User
Longtime User
B4X:
    DateTime.DateFormat = "yyyy-MM-dd"
    DateTime.TimeFormat = "HH:mm:ss"
    Dim Date As String = DateTime.Date(Location1.Time) & " " & DateTime.Time(Location1.Time)
    Log("Date: " & Date)
;)

Jan
 

melvin2345

Member
Licensed User
Longtime User
Does this work universally through the various version of iOS? I'm compiling on a local Mac, and it works fine on most devices, but on some devices, LocManager_LocationChanged never gets invoked. For example, on the 6s it does not work. It asks to allow use of location, I say allow, but nothing happens beyond that.
 

Jean Weets

Member
Licensed User
Longtime User
Does this example still work with the current library version 1.20?
The Heading works but the location does not seem to work anymore :|
 

Jean Weets

Member
Licensed User
Longtime User
Yes, and I think I know what the problem is. Only Wi-Fi devices previously worked with localization, but after the IOS update to 9.2 it is no longer working.
 

Jean Weets

Member
Licensed User
Longtime User
I've tested it on an ipad air 32gb 4g with ios 10.3.3 and it works.
On the ipad mini wifi IOS 9.3. it only works the first time i start the app.
 
Status
Not open for further replies.
Top