Android Question Question about showing google map

Alphaw

Member
Licensed User
Longtime User
Dear all,

I am going to do this thing:
1. type a venue name in a textbox
2. click a button
3. using google map distance api to show the route between mylocation and targetlocation

My question is that, is it possible to do 3? When I follow the step in google developer console, and finally get the json within two venue, i.e
https://maps.googleapis.com/maps/ap...t&key=AIzaSyDlahITQsk6VvxETeny0N95ZUeOxOCg-ds
And I know nothing about how to show the route in the map inside my app.

Besides, I try to use webview to load the uri, like
B4X:
    WebView1.LoadUrl($"https://maps.googleapis.com/maps/api/staicmap?center=${MyLatitude},${MyLongitude}&zoom=12&size=400*400&maptype=terrain&markers=size:medium%7Ccolor:blue%7C${MyLatitude},${MyLongitude}&sensor=false"$)
But the web show error 404, do this method work anymore?

I am hoping to see any response. Thank you.
 

Alphaw

Member
Licensed User
Longtime User
The result from the google direction api will be an json string. you can paste it into this tool to see the code, how to manage the json; https://www.b4x.com:51041/json/index.html
the next step will be to decode the polyline within the json (legs->steps->polyline->points). In this post there is an example how to do it: https://www.b4x.com/android/forum/threads/mapsdecodepolyline.31499/
I have try to do so, however, there are duplicate of sub! Do I do something wrong?

upload_2016-3-29_10-42-10.png
 
Upvote 0

Alphaw

Member
Licensed User
Longtime User
looks like, that you already have this sub in your project?
Is that part not a sub, but is running at jobdone? Like
B4X:
Sub Jobdone (Job As HttpJob)
    If Job.Success = True Then
        Dim rStr As String
        Select Job.JobName
            Case "Route"
                rStr = Job.GetString

                Dim parser As JSONParser
                parser.Initialize(rStr)
                Dim root As Map = parser.NextObject
                Dim routes As List = root.Get("routes")
                For Each colroutes As Map In routes
                 Dim summary As String = colroutes.Get("summary")
                 Dim copyrights As String = colroutes.Get("copyrights")
                 Dim legs As List = colroutes.Get("legs")
                 For Each collegs As Map In legs
                  Dim steps As List = collegs.Get("steps")
                  For Each colsteps As Map In steps
                   Dim polyline As Map = colsteps.Get("polyline")
                   Dim points As String = polyline.Get("points")
                  Next
                 Next
                 Dim overview_polyline As Map = colroutes.Get("overview_polyline")
                 Dim points As String = overview_polyline.Get("points")
                 Dim waypoint_order As List = colroutes.Get("waypoint_order")
                Next
                Dim geocoded_waypoints As List = root.Get("geocoded_waypoints")
                For Each colgeocoded_waypoints As Map In geocoded_waypoints
                 Dim types As List = colgeocoded_waypoints.Get("types")
                 For Each coltypes As String In types
                 Next
                 Dim geocoder_status As String = colgeocoded_waypoints.Get("geocoder_status")
                 Dim place_id As String = colgeocoded_waypoints.Get("place_id")
                Next
                Dim status As String = root.Get("status")
                ToastMessageShow(points,True)

                MapsDecodePolyline(points, steps)
  
                Dim i As Int
                Dim llPoint As LatLng
                Dim lpoints As List
                Dim mpiCurrent As MapsPointItem
                lpoints.Initialize
                For i = 0 To steps.Size - 1
                  mpiCurrent = steps.Get(i)
                  llPoint.Initialize(mpiCurrent.dLatitude, mpiCurrent.dLongitude)
                  lpoints.Add(llPoint)
                Next
'                gmap = MapFragment1.GetMap
                Dim pl As Polyline = gmap.AddPolyline
                pl.points = lpoints
        End Select
    End If
    Job.Release
End Sub
But actually, I don't know how to use MapsDecodePolyline as which item from json I need to used? Or I should create new item
 
Upvote 0

Alphaw

Member
Licensed User
Longtime User
i dont believe that.
Due to the error you have this sub twice in your project
In original post, https://www.b4x.com/android/forum/threads/mapsdecodepolyline.31499/
There should be as
B4X:
MapsDecodePolyline(sPolyline, lMapsPoints)
  
Dim i As Int
Dim llPoint As LatLng
Dim points As List
Dim mpiCurrent As MapsPointItem

bMapsRoutingInProgress = True
bMapsManeuverPassed = False
Main.bDoNotDisturb = True
points.Initialize

For i = 0 To lMapsPoints.Size - 1
  mpiCurrent = lMapsPoints.Get(i)
  llPoint.Initialize(mpiCurrent.dLatitude, mpiCurrent.dLongitude)
  points.Add(llPoint)
Next

Dim pl As Polyline = gmap.AddPolyline
pl.points = points
pl.Color = utilities.v4_Colors("holo_blue_light")
But I cannot use this one. I try to Fill in Sub/End Sub and get the result as my photo above.
So, will it be used in Jobdone as I get json and do json praser. How about what the item I need to use in the json code as I don't know which item I should use.
Thank you.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
MapsDecodePolyline(sPolyline, lMapsPoints)

Dim i As Int
Dim llPoint As LatLng
Dim points As List
Dim mpiCurrent As MapsPointItem

bMapsRoutingInProgress =
True
bMapsManeuverPassed = False
Main.bDoNotDisturb = True
points.Initialize

For i = 0 To lMapsPoints.Size - 1
mpiCurrent = lMapsPoints.Get(i)
llPoint.Initialize(mpiCurrent.dLatitude, mpiCurrent.dLongitude)
points.Add(llPoint)

Next

Dim pl As Polyline = gmap.AddPolyline
pl.points = points
pl.Color = utilities.v4_Colors(
"holo_blue_light")
this is the code AFTER the sub...

The sub itself is this

B4X:
Sub MapsDecodePolyline(encoded As String, poly As List)
    Log("MapsDecodePolyline")
  
    Dim index As Int
    Dim lat As Int
    Dim lng As Int
    Dim fLat As Float
    Dim fLng As Float
    Dim b As Int
    Dim shift As Int
    Dim result As Int
    Dim dlat As Int
    Dim dlng As Int
    Dim p As LatLng
    Dim i As Int
    Dim l As LatLng

    index = 0 : lat = 0 : lng = 0
    Do While index < encoded.Length
        shift = 0 : result = 0
        Do While True
            b = Asc(encoded.SubString2(index, index + 1)) - 63 : index = index + 1
            result = Bit.OR(result, Bit.ShiftLeft(Bit.AND(b, 0x1f), shift))
            shift = shift + 5
            If b < 0x20 Then Exit
        Loop
      
        If Bit.AND(result, 1) = 1 Then
            dlat = Bit.Not(Bit.ShiftRight(result, 1))
        Else
            dlat = Bit.ShiftRight(result, 1)
        End If
        lat = lat + dlat
        shift = 0 : result = 0
        Do While True
            b = Asc(encoded.SubString2(index, index + 1)) - 63 : index = index + 1
            result = Bit.OR(result, Bit.ShiftLeft(Bit.AND(b, 0x1f), shift))
            shift = shift + 5
            If b < 0x20 Then Exit
        Loop
      
        If Bit.AND(result, 1) = 1 Then
            dlng = Bit.Not(Bit.ShiftRight(result, 1))
        Else
            dlng = Bit.ShiftRight(result, 1)
        End If
        lng = lng + dlng
        fLat = lat
        fLng = lng
      
        Dim mpiCurrent As MapsPointItem
        mpiCurrent.Initialize
        mpiCurrent.iPassed = 0
        mpiCurrent.dLatitude = fLat / 100000
        mpiCurrent.dLongitude = fLng / 100000
        poly.add(mpiCurrent)
    Loop
End Sub
 
Upvote 0

Alphaw

Member
Licensed User
Longtime User
this is the code AFTER the sub...

The sub itself is this

B4X:
Sub MapsDecodePolyline(encoded As String, poly As List)
    Log("MapsDecodePolyline")
 
    Dim index As Int
    Dim lat As Int
    Dim lng As Int
    Dim fLat As Float
    Dim fLng As Float
    Dim b As Int
    Dim shift As Int
    Dim result As Int
    Dim dlat As Int
    Dim dlng As Int
    Dim p As LatLng
    Dim i As Int
    Dim l As LatLng

    index = 0 : lat = 0 : lng = 0
    Do While index < encoded.Length
        shift = 0 : result = 0
        Do While True
            b = Asc(encoded.SubString2(index, index + 1)) - 63 : index = index + 1
            result = Bit.OR(result, Bit.ShiftLeft(Bit.AND(b, 0x1f), shift))
            shift = shift + 5
            If b < 0x20 Then Exit
        Loop
     
        If Bit.AND(result, 1) = 1 Then
            dlat = Bit.Not(Bit.ShiftRight(result, 1))
        Else
            dlat = Bit.ShiftRight(result, 1)
        End If
        lat = lat + dlat
        shift = 0 : result = 0
        Do While True
            b = Asc(encoded.SubString2(index, index + 1)) - 63 : index = index + 1
            result = Bit.OR(result, Bit.ShiftLeft(Bit.AND(b, 0x1f), shift))
            shift = shift + 5
            If b < 0x20 Then Exit
        Loop
     
        If Bit.AND(result, 1) = 1 Then
            dlng = Bit.Not(Bit.ShiftRight(result, 1))
        Else
            dlng = Bit.ShiftRight(result, 1)
        End If
        lng = lng + dlng
        fLat = lat
        fLng = lng
     
        Dim mpiCurrent As MapsPointItem
        mpiCurrent.Initialize
        mpiCurrent.iPassed = 0
        mpiCurrent.dLatitude = fLat / 100000
        mpiCurrent.dLongitude = fLng / 100000
        poly.add(mpiCurrent)
    Loop
End Sub
Oh, I see, I try it first. Thank you.
 
Upvote 0

awakenblueheart

Member
Licensed User
Longtime User
What is "utilities" here inside the line?
pl.Color = utilities.v4_Colors("holo_blue_light")
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
But the web show error 404, do this method work anymore?
I guess the problem is a bad link
probaly wrong:
B4X:
https://maps.googleapis.com/maps/api/staicmap?center=${MyLatitude},${MyLongitude}&zoom=12&size=400*400&maptype=terrain&markers=size:medium|color:blue|${MyLatitude},${MyLongitude}&sensor=false

probaly right: (note that url... staicmap should be staticmap i guess)
B4X:
https://maps.googleapis.com/maps/api/staticmap?center=${MyLatitude},${MyLongitude}&zoom=12&size=400*400&maptype=terrain&markers=size:medium|color:blue|${MyLatitude},${MyLongitude}&sensor=false
 
Upvote 0
Top