Android Question GPS - From Location A to Location B - How To ?

semar

Active Member
Licensed User
Longtime User
Dear All,
I would like to use the smartphone to go from a location A to a location B.

The location A ist the one who I am at the moment. It can vary as I move. The location B is fixed and has been retrieved from a file.

I would like to show on the smartphone an arrow indicating the direction to take in order to reach the location B depending on my current location. This arrow should point with a good approximation toward the location B, and if I rotate the device it should also update the pointed direction accordingly. A nice to have would be also to show the current distance to the location B.

So far I can get my current position using the GPS, as in the related GPS code example:
B4X:
lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)

I guess I need to get the bearing, which is, from what I understand, the degrees related to North I shoud turn to, in order to point toward the direction of location B.

Could someone gently collaborate and point me to the right direction ? Is there any ready library I could use to reach the goal I have in mind ?

Thanks in advance to all of you who are willing to help !

Regards,
Sergio
 
Last edited:

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
As a matter fact I do exactly the same thing. My app is a previous location finder (like a car in a parking lot finder).

The first task is getting the compass heading of the phone. I found it to be tricky to get a reliable, smoothed compass heading for the phone.

Search for "Canalrun Compass". I have several posts probably from several years ago addressing the subject – there is probably a library with Java source with a simple example app included.

The second task is to find the distance and bearing between your two given points A and B. There are several libraries on the forum from others that do this. Search for GPS related stuff.

Once you have these two pieces of information, combine them to tell the user which way to walk.

Barry.

Added later:
This is a link to my compass library:
https://www.b4x.com/android/forum/threads/phone-as-compass.83890/#post-532028
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Canalrun,

Can you post some sample code doing that and links to the needed libraries. I need to do the same thing as semar.

Thanks so much.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Hi Canalrun,
Can you post some sample code doing that and links to the needed libraries. I need to do the same thing as semar.
Thanks so much.

Here are some code snippets that I extracted from an app I have. I don't have anything that gives an isolated, complete example.

I use Google Services. Of course, your path to the gms file will be different. From my Projects Attributes region:
B4X:
#AdditionalRes: C:\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

These are the libraries I have selected for one of my projects. They are not all specifically required to find the direction and distance between two points.

upload_2018-5-12_17-31-37.png


These are my global Google Maps variables, plus I use the excellent MapsExtras found elsewhere on the forum:
B4X:
Dim mfMap As MapFragment
Dim mpMap As GoogleMap
Dim gmExtra As GoogleMapsExtras

This code maintains a global variable, PhoneBearing, set during a compass change event from my compass library (see above)
B4X:
Sub GeoRot_Bearing(bng As Double)
  PhoneBearing = bng
 
'  Log("PhoneBearing: " & NumberFormat(PhoneBearing, 3, 0))
End Sub

This is the code using Google Maps (and maybe MapsExtras) that computes the distance. You have to use your imagination a little about where the specific variables come from:
B4X:
   ' Compute distance
    Dim loc1, loc2 As Location
 
    loc1.Initialize2(CurrLoc.loc.Latitude, CurrLoc.loc.Longitude)
    loc2.Initialize2(MarkerLoc.Lat, MarkerLoc.Lon)
 
    Dim dist As Float = loc1.DistanceTo(loc2)

I hope this helps. These code snippets can be used together to implement the distance and bearing between two points solution.

Barry.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Here are some code snippets that I extracted from an app I have. I don't have anything that gives an isolated, complete example.

I use Google Services. Of course, your path to the gms file will be different. From my Projects Attributes region:
B4X:
#AdditionalRes: C:\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

These are the libraries I have selected for one of my projects. They are not all specifically required to find the direction and distance between two points.

View attachment 67817

These are my global Google Maps variables, plus I use the excellent MapsExtras found elsewhere on the forum:
B4X:
Dim mfMap As MapFragment
Dim mpMap As GoogleMap
Dim gmExtra As GoogleMapsExtras

This code maintains a global variable, PhoneBearing, set during a compass change event from my compass library (see above)
B4X:
Sub GeoRot_Bearing(bng As Double)
  PhoneBearing = bng
 
'  Log("PhoneBearing: " & NumberFormat(PhoneBearing, 3, 0))
End Sub

This is the code using Google Maps (and maybe MapsExtras) that computes the distance. You have to use your imagination a little about where the specific variables come from:
B4X:
   ' Compute distance
    Dim loc1, loc2 As Location
 
    loc1.Initialize2(CurrLoc.loc.Latitude, CurrLoc.loc.Longitude)
    loc2.Initialize2(MarkerLoc.Lat, MarkerLoc.Lon)
 
    Dim dist As Float = loc1.DistanceTo(loc2)

I hope this helps. These code snippets can be used together to implement the distance and bearing between two points solution.

Barry.

Hi,

Thanks for the reply. I will experiment with it.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
use Google Services. Of course, your path to the gms file will be different. From my Projects Attributes region:
B4X:
#AdditionalRes: C:\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
These are the libraries I have selected for one of my projects. They are not all specifically required to find the direction and distance between two points.
1. The #AdditionalRes line is not required anymore. See Google Maps tutorial.

2. The only library required to calculate the distance and bearing between two points is the GPS library with its Location type.
 
Upvote 0
Top