Android Question Using GPS how to get walking, driving... distance?

CesarLM

Member
Licensed User
Longtime User
Using gps get the distance walking, driving ... between two location?

Not by straight distance!

Any help would be greatly appreciated.
Thanks.
 

jsanchezc

Member
Licensed User
Longtime User
Using gps get the distance walking, driving ... between two location?

Not by straight distance!

Any help would be greatly appreciated.
Thanks.

This code is orientative, you can call Google, XML web service to get distance and duration.
You can send mode (driving, walking)...

B4X:
../..
WebURL = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=<START_LOCATION>&destinations=<END_LOCATION>&mode=driving&language=us-EN&sensor=false&units=metric"

WebURL=WebURL.Replace("<START_LOCATION>",  AnteriorLat.Trim & "," & Anteriorlong.Trim )
  WebURL=WebURL.Replace("<END_LOCATION>",  ActualLat.Trim & "," & Actuallong.Trim )
 
 
job1.Initialize( "Job_GetDistance", Me)
  Try
      job1.PostString(WebURL,"")
  Catch
    ToastMessageShow(jsc_tools.tr("No se pudo acceder obtener posición.") & job1.ErrorMessage  ,True)
  End Try
end sub

Sub JobDone (Job As HttpJob)
Dim ResultGoogle As String=""
Dim ResultGoogle2 As String=""
Dim StartData As Int =0
Dim EndData As Int=0
Dim DataLatLng As String =""
Dim SQlString As String =""
If Job.Success = True Then
        Select Job.JobName
            Case "Job_GetDistance"
              ResultGoogle = Job.GetString
              ResultGoogle=ResultGoogle.ToLowerCase
              ResultGoogle2=ResultGoogle
             
             
              StartData=ResultGoogle.IndexOf("<distance>")
              EndData=ResultGoogle.IndexOf("</distance>")
              ' Log(ResultGoogle)
            Try
              ResultGoogle=ResultGoogle.SubString2(StartData + 10, EndData -1)
              EndData=ResultGoogle.IndexOf("</value>")
              ResultGoogle=ResultGoogle.SubString2(12,EndData)
              ResultGoogle=ResultGoogle.replace("<value>","")
              ResultGoogle=ResultGoogle.replace("</value>","")
              ResultGoogle=ResultGoogle.replace(CRLF,"")
            Catch
              ResultGoogle="0"
            End Try
              Log (ResultGoogle) 'distance
   
              StartData=ResultGoogle2.IndexOf("<duration>")
              EndData=ResultGoogle2.IndexOf("</duration>"

              Log(ResultGoogle2) 'duration
            Try
              ResultGoogle2=ResultGoogle2.SubString2(StartData + 10, EndData -1)
              EndData=ResultGoogle2.IndexOf("</value>")
              ResultGoogle2=ResultGoogle2.SubString2(12,EndData)
              ResultGoogle2=ResultGoogle2.replace("<value>","")
              ResultGoogle2=ResultGoogle2.replace("</value>","")
              ResultGoogle2=ResultGoogle2.replace(CRLF,"")
            Catch
              ResultGoogle2="0"
            End Try
              Log (ResultGoogle2)
 
Upvote 0

CesarLM

Member
Licensed User
Longtime User
Hi, jsanchezc

This example is with Internet?
We can do something GPS? Not with internet.

Sorry for my basic English.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Yes, you can,
You need to either:
- memorize all points and then calculate the distance between the points and add the distances.
- memorize the previous point, calculate the distance to the current point and add it to the total.
In the GPS library you have the DistanceTo method for the Loccation object.
Distance = Location1.DistanceTo(Location2)
 
Last edited:
Upvote 0

CesarLM

Member
Licensed User
Longtime User
Hi, klaus

Distance = Location1.DistanceTo(Location2) = Return straight distance

Take a look to this image which given below.

http://i.stack.imgur.com/jiuDs.png

I want driving distance between two location (latitude - longitude).

Which is displayed as PURPLE LINE (Not RED LINE).
We can do something GPS? Not with internet.

Thanks!
 

Attachments

  • jiuDs.png
    jiuDs.png
    365.7 KB · Views: 383
Upvote 0

klaus

Expert
Licensed User
Longtime User
That's what I told you.
You need to memorize, or treat all the points between the start and end point given by the GPS when you are moving!
If you expect giving a start point and an end point and GPS returns you the distance of the blue line from your image this is IMPOSSIBLE with GPS.
Where do the points of your blue line come from ?
If you have only the start point and the end point the solution is given by jsanchezc in post #2 !
 
Upvote 0
Top