Android Question Distance between two map points and time needed to travel

Inman

Well-Known Member
Licensed User
Longtime User
I need to find the distance between two geo coordinates on Google Maps. I see that the GPS library has a DistanceTo option. My question is, does the function use a driving route (via road) based on the city's Google Map or is it calculating the distance between the coordinates as a straight line, using some mathematical formula?

If possible, I would also like to get an estimated driving time between the two geo coordinates.
 

derez

Expert
Licensed User
Longtime User
is it calculating the distance between the coordinates as a straight line, using some mathematical formula?
Yes (and no, it is actually the distance on a Great Circle of Earth)
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
That is sad. Looks like I will have to use the web API now.

Btw is it possible to plot on Google Maps, a driving road route between two points?
 
Upvote 0

derez

Expert
Licensed User
Longtime User
But it plots the route by itself in Google Maps! (from your present position to the destination point).
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Yes that is what I need. I want to mark two points on Google Maps and it should display the driving route connecting them.

Can I do that?
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Like I wrote above, Google maps calculates and shows the route from your position to a marked point:
Turn Google maps on and let the GPS work. You see your position on the map.
Touch a point on the map and you'll have it marked (a baloon icon).
On the bottom left there is an icon of car - press it - you get driving instruction and the route.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
inman, did you solve this problem ?, I would like to know that too

Here is how I did:
  1. Using Google Directions API, I got the polylines point string
  2. Using MapsDecodePolyline function given here, I converted point string into a list of points
  3. I drew the line connecting the points using GoogleMaps.AddPolyLine, using the list of points from step 2
 
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
Here is how I did:
  1. Using Google Directions API, I got the polylines point string
  2. Using MapsDecodePolyline function given here, I converted point string into a list of points
  3. I drew the line connecting the points using GoogleMaps.AddPolyLine, using the list of points from step 2
Looks complicated, Do you mind to share the code of your project ?, I mean, only the code related to this topic.
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
I have that code which maybe can help you with small adaptations...

B4X:
**************************************
FUNCTION calcwebdist (cAddress1, cAddress2,cNom,cJour)
LOCAL cUrl, oHTTP, oXMLDOM, oOrigine
LOCAL oDestination,oState,ocDuree,ovDuree ,ocDistance, ovDistance

cUrl="http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" +cAddress1+ "&destinations=" +cAddress2 + "&mode=driving&language=fr-FR&sensor=false"


oHTTP = CreateObject("MSXML2.XMLHTTP")
oHTTP.Open("GET", cURL, .f.)
oHTTP.Send
oXMLDOM = oHTTP.responseXML


oOrigine = oXMLDOM.selectNodes("//origin_address")
IF oOrigine.length != 1 then
     RETURN ("")
ELSE

    ciao = oOrigine.item(0).text
ENDIF
IF NOT USED("distance")
    CREATE CURSOR distance (nom c(50),  jour date, depart c(70), arrivee c(70), duree c(15), durees N(10), distance c(15), distancem n(10),  etat L)
endif
oDestination = oXMLDOM.selectNodes("//destination_address")
oState = oXMLDOM.selectNodes("//status")
ocDuree = oXMLDOM.selectNodes("//duration / text")
ovDuree = oXMLDOM.selectNodes("//duration / value")
ocDistance = oXMLDOM.selectNodes("//distance / text")
ovDistance = oXMLDOM.selectNodes("//distance / value")


FOR i= 0 TO odestination.length -1

    IF ostate.item(i).text = 'OK' THEN

        INSERT INTO distance(nom, jour,depart, arrivee, duree, durees, distance, distancem, etat) VALUES ;
        (cNom, CTOD(cJour),ciao,oDestination.item(i).text ,ocDuree.item(i).text, VAL(ovDuree.item(i).text), ocDistance.item(i).text, VAL(ovDistance.item(i).text),.T.)
    ELSE
        **INSERT INTO distance(depart, arrivee, duree, distance,etat) VALUES ;
        (ciao,'NON GEOCODEE' ,',',.F.)
    ENDIF
NEXT
 
Upvote 0
Top