Android Question Get gps coordinates from touch or click event

Mark Read

Well-Known Member
Licensed User
Longtime User
Is it possible to get the gps coordinates (Lat, Lon) by clicking or touching a map? Preferably in OSMDroid. This is available in OSMand for instance or in google maps (right click-what is here?).

I think it might also be possible if I can get the gps coordinates of a boundary box at the edges of the screen and then using a panel, calculate the pixel coordinates respectively.

Further info is detailed here: http://code.google.com/p/osmbonuspack/wiki/Tutorial_5.

Many thanks.
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Thanks Erel but I was kind of hoping that OSMDroid might have the same as I have to use offline maps.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Look at the OSMDroid TouchOverlay.
It raises events:
  • Click(GeoPoint1 As OSMDroid_GeoPoint) As Boolean
  • LongClick(GeoPoint1 As OSMDroid_GeoPoint) As Boolean
  • Rotate(DeltaAngle As Float)

Martin.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Great Martin, will look at that. I did look at overlays but obviously the wrong one. Many thanks. If I can get it working, I will post.
 
Upvote 0

TonyCav

New Member
Licensed User
Longtime User
I am trying to click a button and get the current GPS coordinates. My code returns 0's for the lat. and long. What am I doing wrong? Thanks

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim GPS1 As GPS
Dim Loc1 As Location
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Latitude As Label
Private Longatude As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("JUData")
GPS1.Initialize("GPS")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
GPS1.stop
End Sub
Sub GetCoord_Click
'Gets the Current GPS Coordinate
Loc1.Initialize
If GPS1.GPSEnabled=False Then
Latitude.Text="No Device"
Else
GPS1.Stop
GPS1.Start(0,0)
Latitude.Text=Loc1.converttominutes(Loc1.Latitude)
Longatude.Text=Loc1.ConvertToMinutes(Loc1.Longitude)
End If
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
use code tags when posting code...
B4X:
my code her
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I am trying to click a button and get the current GPS coordinates. My code returns 0's for the lat. and long. What am I doing wrong? Thanks

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim GPS1 As GPS
Dim Loc1 As Location
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Latitude As Label
Private Longatude As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("JUData")
GPS1.Initialize("GPS")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
GPS1.stop
End Sub
Sub GetCoord_Click
'Gets the Current GPS Coordinate
Loc1.Initialize
If GPS1.GPSEnabled=False Then
Latitude.Text="No Device"
Else
GPS1.Stop
GPS1.Start(0,0)
Latitude.Text=Loc1.converttominutes(Loc1.Latitude)
Longatude.Text=Loc1.ConvertToMinutes(Loc1.Longitude)
End If
End Sub

https://www.b4x.com/android/forum/threads/gps-tutorial.6592/

Look at that GPS code example.
See how the GPS LocationChanged event is raised and passed a Location object each time a new location is obtained?
You need to implement a sub that listens for that event and save the Location object passed to the sub.

Now when your button is clicked you can get the saved Location.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Two points.
Firstly the point from Martin that you have not used the GPS LocationChanged event which is triggered when a location is found..
Secondly, getting the location with GPS means using "line of sight" ie won't work indoors. Outdoors it will also take a few minutes to get a fix. Your code tries to get the location lon/lat before the GPS has time to do anything. Time to read the tutorials again.

Post again if you still have problems.

This example is about as simple as it gets:

B4X:
Version=2.02
IconFile=
FullScreen=False
IncludeTitle=True
NumberOfModules=0
Package=anywheresoftware.b4a.samples.gps
Label=GPS
VersionCode=1
VersionString=
Orientation=unspecified
CanInstallToExternalStorage=True
DoNotOverwriteManifest=False
ManifestCode=
NumberOfFiles=1
File1=1.bal
NumberOfLibraries=2
Library1=core
Library2=gps
@EndOfDesignText@
Sub Process_Globals
    Dim GPS1 As GPS
End Sub

Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        GPS1.Initialize("GPS")
    End If
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text = "Speed = " & Location1.Speed
End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
    lblSatellites.Text = "Satellites:" & CRLF
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite
        Satellite = Satellites.Get(i)
        lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
            " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _
            & " " & Satellite.Elevation
    Next
End Sub
 
Upvote 0
Top