Android Question Google Map Routes

Status
Not open for further replies.

prajinpraveen

Active Member
Licensed User
Longtime User
Hello All, i have a route drawn on google maps. is there a way to find out if the vehicle has deviated from the route. i did some research and it seems like there is an api that i could use PolyUtil.isLocationOnPath()
any suggestions?
 

MAGAREY

Member
Licensed User
Longtime User
Hello All, i have a route drawn on google maps. is there a way to find out if the vehicle has deviated from the route. i did some research and it seems like there is an api that i could use PolyUtil.isLocationOnPath()
any suggestions?

were you able to find anything about that?
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
were you able to find anything about that?
Recently l have been thinking about this as well. I have found this project https://github.com/googlemaps/android-maps-utils and created a jar from part of this project. I also added a function to find coordinates on the polyline where a line drawn from current location, perpendicular to the polyline, intersects the polyline - this makes updated polyline look better if current location is a couple of meters of the path (polyline) which depends on the GPS accuracy etc.
What I do is:
Add mapsutil.jar to the project:
B4X:
    #AdditionalJar: mapsutil

Sub Globals
    Dim pline As Polyline
    Dim gmap As GoogleMap  
    Dim LastLoc, DestLoc As LatLng
End Sub
Get directions from Google:
B4X:
Sub GetDirections(source As String, destination As String)
    Dim j As HttpJob
    Dim API_KEY As String = "YOUR API KEY"        'directions api key
'    Get directions (Location source -> Location destination)
    j.Initialize("Directions", Me)
    j.Download2("https://maps.googleapis.com/maps/api/directions/json", Array As String("origin", source,"destination", destination,"key",API_KEY))

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As Map = parser.NextObject
        If root.Get("status") = "OK" Then
            Dim routes As List = root.Get("routes")
            For Each colroutes As Map In routes
                Dim legs As List = colroutes.Get("legs")
                For Each collegs As Map In legs
                    start_address  = collegs.Get("start_address")
                    end_address = collegs.Get("end_address")
                    Dim steps As List = collegs.Get("steps")
                    For Each colsteps As Map In steps
                        Dim duration As Map = colsteps.Get("duration")
                        travelduration = travelduration + duration.Get("value")
                        Dim distance As Map = colsteps.Get("distance")
                        traveldistance = traveldistance + distance.Get("value")
                    Next
                    Dim overview_polyline As Map = colroutes.Get("overview_polyline")
                    polyPoints = overview_polyline.Get("points")
                Next
            Next
            j.Release
            drawDirections                            'draw polyline
        Else
            ToastMessageShow("Error: " & root.Get("status"), True)
        End If
    Else
        Log("Error!")
    End If
    j.Release
End Sub
Decode polyline and draw polyline path on the map
B4X:
Sub drawDirections
    Dim jo As JavaObject
    Dim Points As List

    Points.Initialize
    jo.InitializeStatic("com.poly.PolyUtil")
    Points = jo.RunMethod("decode", Array(polyPoints))
    If Points.Size > 0 Then
        pline = gmap.AddPolyline
        pline.Points = Points
        pline.Color = Colors.Magenta
    End If
End Sub

Call sub updateDirections every couple of seconds (or whatever you decide) to find if you are on path.
If yes - then update polyline removing points past the current direction path, if not - get new directions from Google and draw polyline.
B4X:
Sub updateDirections  
    Dim source, destination As String
  
    Dim jo As JavaObject
    Dim Points As List
    Dim res As Int
    Dim curloc As LatLng
    curloc.Initialize(LastLoc.Latitude, LastLoc.Longitude)
    Points.Initialize
    jo.InitializeStatic("com.poly.PolyUtil")
    Points = pline.Points
    Dim tolerance As Double
    tolerance = 10        '10 m
    res = jo.RunMethod("locationIndexOnPath", Array(curloc, Points, False, tolerance))     'checks whether the given point lies on or near a polyline, within a specified tolerance in meters
'returns -1 if point does not lie on or near the polyline.
' 0 if point is between poly[0] and poly[1] (inclusive),
' 1 if between poly[1] and poly[2],
' 2 if between poly[2] and poly[3],
' ...etc

    Dim res1 As Double
    Dim p1, p2 As LatLng
    Dim pathdata As String
    p1.Initialize(LastLoc.Latitude, LastLoc.Longitude)
    pathdata = pathdata & "last loc " & LastLoc.Latitude & ", " & LastLoc.Longitude & CRLF
    If res = -1 Then                        'not on path
        source = LastLoc.Latitude & "," & LastLoc.Longitude                'current location from GPS_LocationChanged sub
        destination = DestLoc.Latitude & "," & DestLoc.Longitude                'destination location coordinates
        p2 = Points.Get(0)
        GetDirections(source, destination)                                                                        'get new directions
    Else                                                                                                         'on path, res = index in Points list (polyline)
        p2 = Points.Get(res)
        If res = 0 Then                                                                                '0 so it is the first item in the Points list (polyline)
            Dim resll As LatLng
            jo.InitializeStatic("com.poly.PolyUtil")
            resll = jo.RunMethod("intersectOnLine", Array(curloc, Points.Get(0), Points.Get(1)))        'find coordinates on the polyline where current location intersects with polyline
            If resll.Latitude <> 0 And resll.Longitude <> 0 Then            'resl1 equals to coordinates on the polyline, if current loc. intesects with polyline path
                                                                                                                                'res11 = 0,0 - current location doesn't intersect with the polyine,
                Points.Set(0, resll)
                pline.Points = Points
                pline.Color = Colors.Red
            End If
        Else                                                                                                    '>0 so, remove polyline points from the Points list and update polyline
            If Points.Size > 2 Then
                For i = 0 To res - 1
                    Points.RemoveAt(0)
                Next
                pline.Points = Points                                                            'set polyline
                pline.Color = Colors.Green
            End If
        End If
    End If
End Sub
I am still testing it that's why I use change polyline colour so I know which part is executed. It works pretty well - it still needs some work.
I hope that this code works. I had to remove parts that are relevant to my application only.

Any suggestions how to make it better as far as updating polyliine path are welcome.
 

Attachments

  • mapsutil.jar
    8.4 KB · Views: 565
Last edited:
Upvote 0

Carlos marin

Active Member
Licensed User
Longtime User
I find the updateDirections great, my question is there is a way to move the bookmark while it is updated. Do not clean the map and add the marker. instead move it as in the UBER application
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
I find the updateDirections great, my question is there is a way to move the bookmark while it is updated. Do not clean the map and add the marker. instead move it as in the UBER application
You should be more specific what you want? Not everybody uses UBER application to understand what you mean?
 
Upvote 0

yfleury

Active Member
Licensed User
Longtime User
I have to implement a html_directions to sub updateDirections.
Can you point me for my search?
 
Upvote 0
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…