Sub Process_Globals
Private FusedLocationProvider1 As FusedLocationProvider
Private LastLocation As Location
Dim geoaddress As Address
End Sub
Sub Globals
Dim Latitude, Longitude As Double
Dim lc As Geocoder
Dim ResultsList As ListView
End Sub
Sub Activity_Create(FirstTime As Boolean)
' ** IMPORTANT see manifest for required entries **
Activity.LoadLayout("Main")
ResultsList.Initialize("ResultsList")
If FirstTime Then
FusedLocationProvider1.Initialize("FusedLocationProvider1")
End If
End Sub
Sub Activity_Resume
' attempt to connect to the location services
' after calling Connect we are waiting for either ConnectionFailed or ConnectionSuccess events
FusedLocationProvider1.Connect
End Sub
Sub Activity_Pause (UserClosed As Boolean)
FusedLocationProvider1.Disconnect
End Sub
Sub FusedLocationProvider1_ConnectionFailed(ConnectionResult1 As Int)
Log("FusedLocationProvider1_ConnectionFailed")
' the FusedLocationProvider ConnectionResult object contains the various CoonectionResult constants
Select ConnectionResult1
Case FusedLocationProvider1.ConnectionResult.NETWORK_ERROR
' a network error has occurred, this is likely to be a recoverable error
' so try to connect again
FusedLocationProvider1.Connect
Case Else
' TODO handle other errors
End Select
End Sub
Sub FusedLocationProvider1_ConnectionSuccess
Log("FusedLocationProvider1_ConnectionSuccess")
Dim LastKnownLocation As Location
LastKnownLocation = FusedLocationProvider1.GetLastKnownLocation
If LastKnownLocation.IsInitialized Then
GeocodeLocation(LastKnownLocation)
Else
Dim LocationRequest1 As LocationRequest
LocationRequest1.Initialize
LocationRequest1.SetInterval(1000) ' 1000 milliseconds
LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_NO_POWER)
LocationRequest1.SetSmallestDisplacement(1) ' 1 meter
FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
End If
End Sub
Sub FusedLocationProvider1_ConnectionSuspended(SuspendedCause1 As Int)
Log("FusedLocationProvider1_ConnectionSuspended")
' the FusedLocationProvider SuspendedCause object contains the various SuspendedCause constants
Select SuspendedCause1
Case FusedLocationProvider1.SuspendedCause.CAUSE_NETWORK_LOST
' TODO take action
Case FusedLocationProvider1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
' TODO take action
End Select
End Sub
Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
Log("FusedLocationProvider1_LocationChanged")
FusedLocationProvider1.RemoveLocationUpdates
GeocodeLocation(Location1)
End Sub
Sub GeocodeLocation(Location1 As Location)
Log("GeocodeLocation: "&Location1.Latitude&", "&Location1.Longitude)
' here you can get the Location Latitude and Longitude properties and use the Geocoder library to perform a reverse geocode
' if the reverse geocode is successful you will have the country that the device is located in
lc.GetFromLocation(Location1.Latitude, Location1.Longitude,1,Null)
Longitude = Location1.Longitude
Latitude = Location1.Latitude
lc.Initialize("lc")
Log(lc)
End Sub
Sub Button1_Click
ResultsList.Clear
' MaxResults parameter hardcoded to 5 for the demo
Dim MaxResults As Int
MaxResults=5
lc.GetFromLocation(Latitude, Longitude, MaxResults, Null)
If lc.IsInitialized = False Then
Msgbox("Location not found, please check that your Location Services and Data connection are working","")
End If
End Sub
Sub lc_GeocodeDone(Results() As Address, Tag As Object)
Try
' If Results.Length>0 Then
Dim Address1 As Address
Dim i As Int
Address1=Results(i)
If Address1.AddressLines.Size>0 Then
Log("Country: "&Address1.CountryName)
End If
' Else
' Msgbox("GetFromLocation", "No Address matched the Latitude and Longitude")
' End If
Catch
End Try
End Sub