Android Question Polylines between 2 markers

qey

Member
Hi. I have a problem on displaying polylines between 2 markers. First marker for user current location and the second one for the destination location. I already try follow this thread https://www.b4x.com/android/forum/threads/polyline-in-google-map-api-v2.35041/post-318879 . But its not working for me. For now the two marker already displayed on the map. Any idea on how to make the polylines appear?

B4X:
Sub MapFragment1_Ready
    Sleep(500)
    Log("MapFragment1_Ready")
    Log(MapFragment1.GetOpenSourceLicenseInfo)
    
    gmap = MapFragment1.GetMap
    gmap.MapType = gmap.MAP_TYPE_NORMAL
    
    If  gmap.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
        cpLat = 3.3205
        cpLong = 101.5759
    
        Dim m1 As Marker = gmap.AddMarker(cpLat, cpLong, JobMain.JobAddress) 'destination location
        m1.Snippet = Main.jobs.record.TransID
        
    '    Dim line As Polyline
    '    Dim points As List
    '    Dim point As LatLng
    '    line=gmap.AddPolyline
    '    points.Initialize
        
    '    line.color=Colors.Black
    '    line.Geodesic=True
    '    line.Visible =True
    '    line.Width=2

        Dim m2 As Marker = gmap.AddMarker(GPSModule.latitude,GPSModule.longitude, "You Are Here") 'current location
        m2.Snippet = "Current Location"

        Dim cp As CameraPosition
        cp.Initialize(GPSModule.latitude,GPSModule.longitude, gmap.CameraPosition.Zoom)
        gmap.AnimateCamera(cp)
    End If
End Sub
 

DonManfred

Expert
Licensed User
Upvote 0

qey

Member
Sorry for that. This is polyline code that i try using the thread. When i used point.initialize for the current location, the marker not appear on the map. Thats why i used Dim m2 As Marker for the current location

B4X:
Sub MapFragment1_Ready
    Sleep(500)
    Log("MapFragment1_Ready")
    Log(MapFragment1.GetOpenSourceLicenseInfo)
    
    gmap = MapFragment1.GetMap
    gmap.MapType = gmap.MAP_TYPE_NORMAL

    If  gmap.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
        Dim m1 As Marker = gmap.AddMarker(cpLat, cpLong, JobMain.JobAddress) 'destination location
        m1.Snippet = Main.jobs.record.TransID
        
        Dim line As Polyline
        Dim points As List
        Dim point As LatLng
        line=gmap.AddPolyline
        points.Initialize
        
        line.color=Colors.Black
        line.Geodesic=True
        line.Visible =True
        line.Width=2

        'Dim m2 As Marker = gmap.AddMarker(GPSModule.latitude,GPSModule.longitude, "You Are Here") 'current location
        'm2.Snippet = "Current Location"
        
        Dim point As LatLng
        point.Initialize(GPSModule.latitude, GPSModule.longitude)'current location
        points.Add(point)
        
        line.Points=points
        
        Dim cp As CameraPosition
        cp.Initialize(GPSModule.latitude,GPSModule.longitude, gmap.CameraPosition.Zoom)
        gmap.AnimateCamera(cp)
End Sub
 
Upvote 0

RichardN

Active Member
Licensed User
Looks like you are only adding one point to your list of points, you will need at least two to see a line.

You should also check that the GPS is returning valid information before using the data elsewhere
 
Upvote 0

qey

Member
Looks like you are only adding one point to your list of points, you will need at least two to see a line.

You should also check that the GPS is returning valid information before using the data elsewhere

I only have 2 points which is the current location and the destination location. For destination location i call location from geocoder function. How i want to initialize the point for the destination location ? Also i encountered an error at Sub gg_GeocodeDone(Results() As Location, Tag As Object).
Attached with the updated code and error

error:
(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
Sleep not resumed (context is paused): b4a.example3.customlistview$ResumableSub_PanelClickHandler
** Activity (jobtaskdetails) Pause, UserClosed = false **
** Activity (maps) Create, isFirst = true **
** Activity (maps) Resume **
MapFragment1_Ready
MapFragment1_Ready
com.xfs.xfsPnD.maps:_gg_geocodedone, [[Luk.co.martinpearman.b4a.location.AddressWrapper;@61f9100, null]
Error occurred on line: 151 (Maps)
java.lang.IllegalArgumentException: method com.xfs.xfsPnD.maps._gg_geocodedone argument 1 has type anywheresoftware.b4a.gps.LocationWrapper[], got uk.co.martinpearman.b4a.location.AddressWrapper[]
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:200)
    at android.app.ActivityThread.main(ActivityThread.java:6971)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
Attached with the updated code.

B4X:
Sub MapFragment1_Ready
    Sleep(500)
    Log("MapFragment1_Ready")
    Log(MapFragment1.GetOpenSourceLicenseInfo)
    
    gmap = MapFragment1.GetMap
    gmap.MapType = gmap.MAP_TYPE_NORMAL

'    If GPSModule.Address.EqualsIgnoreCase("") Then
'        GetMapByAddress(JobMain.JobAddress)
'    Else
'        GetDirections(GPSModule.Address,JobMain.JobAddress)
'    End If

    If  gmap.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    End If
    Try
        If GPSModule.Location.EqualsIgnoreCase("") Then
            GetMapByAddress(JobMain.JobAddress)
            
            Dim m2 As Marker = gmap.AddMarker(GPSModule.latitude,GPSModule.longitude, "You Are Here")
            m2.Snippet = "Current Location"
        Else
            Log("get map failed")
        End If
        
    '    Dim line As Polyline
    '    Dim points As List
    '    Dim point As LatLng
    '    line=gmap.AddPolyline
    '    points.Initialize
        
    '    line.color=Colors.Black
    '    line.Geodesic=True
    '    line.Visible =True
    '    line.Width=2

        'line.Points=points
    Catch
        Log("ERROR" & LastException.Message)
    End Try
End Sub

Sub GetMapByAddress (aDD As String)
    Dim gg As Geocoder   
    gg.Initialize("gg")   
    gg.GetFromLocationName(aDD,5,Null)     
End Sub

Sub gg_GeocodeDone(Results() As Location, Tag As Object)   
    xfs.WriteTextWriterDim("gg_GeocodeDone")
    If Results.Length > 0 Then
        Dim Location1 As Location
        Dim i As Int
    
        For i=0 To Results.Length-1
            Location1=Results(i)
            cpLat = Location1.Latitude
            cpLong = Location1.Longitude
'            'gmap.AddMarker(cpLat, cpLong, Main.documentID & CRLF & Main.dlvName)       
        Next
        Log("Try")
        Dim m1 As Marker = gmap.AddMarker(cpLat, cpLong, JobMain.JobAddress)
        m1.Snippet = Main.jobs.record.TransID
    Else
        ToastMessageShow("Location not found!", False)
    End If
    cp.Initialize(cpLat, cpLong, 15)
    gmap.AnimateCamera(cp)
End Sub
 
Upvote 0

qey

Member
Knowing which line of code generates the error might get you more help!

the error is on Line 51. I think there is a null value been passed in the program.

B4X:
Sub MapFragment1_Ready
    Sleep(500)
    Log("MapFragment1_Ready")
    Log(MapFragment1.GetOpenSourceLicenseInfo)
    
    gmap = MapFragment1.GetMap
    gmap.MapType = gmap.MAP_TYPE_NORMAL

'    If GPSModule.Address.EqualsIgnoreCase("") Then
'        GetMapByAddress(JobMain.JobAddress)
'    Else
'        GetDirections(GPSModule.Address,JobMain.JobAddress)
'    End If

    If  gmap.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    End If
    Try
        If GPSModule.Location.EqualsIgnoreCase("") Then
            GetMapByAddress(JobMain.JobAddress)
            
            Dim m2 As Marker = gmap.AddMarker(GPSModule.latitude,GPSModule.longitude, "You Are Here")
            m2.Snippet = "Current Location"
        Else
            Log("get map failed")
        End If
        
    '    Dim line As Polyline
    '    Dim points As List
    '    Dim point As LatLng
    '    line=gmap.AddPolyline
    '    points.Initialize
        
    '    line.color=Colors.Black
    '    line.Geodesic=True
    '    line.Visible =True
    '    line.Width=2

        'line.Points=points
    Catch
        Log("ERROR" & LastException.Message)
    End Try
End Sub

Sub GetMapByAddress (aDD As String)
    Dim gg As Geocoder   
    gg.Initialize("gg")   
    gg.GetFromLocationName(aDD,5,Null)     
End Sub

Sub gg_GeocodeDone(Results() As Location, Tag As Object)   
    xfs.WriteTextWriterDim("gg_GeocodeDone")
    If Results.Length > 0 Then
        Dim Location1 As Location
        Dim i As Int
    
        For i=0 To Results.Length-1
            Location1=Results(i)
            cpLat = Location1.Latitude
            cpLong = Location1.Longitude
'            'gmap.AddMarker(cpLat, cpLong, Main.documentID & CRLF & Main.dlvName)       
        Next
        Log("Try")
        Dim m1 As Marker = gmap.AddMarker(cpLat, cpLong, JobMain.JobAddress)
        m1.Snippet = Main.jobs.record.TransID
    Else
        ToastMessageShow("Location not found!", False)
    End If
    cp.Initialize(cpLat, cpLong, 15)
    gmap.AnimateCamera(cp)
End Sub
 
Upvote 0

oparra

Well-Known Member
Licensed User
See, it can guide you to trace routes between points.
Other:
 
Last edited:
Upvote 0

oparra

Well-Known Member
Licensed User
if you are using this library keep in mind:
Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do not use this API for any safety-critical or regulatory compliance purpose.
https://developer.android.com/reference/android/location/Geocoder
 
Upvote 0

Apip Bayok

Member
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

I use this code to draw a polyline and #AdditionalJar: mapsutil on main activity
 
Upvote 0

qey

Member
if you are using this library keep in mind:

https://developer.android.com/reference/android/location/Geocoder

Alright. Thank you so much oparra. i will look at it.
 
Upvote 0

oparra

Well-Known Member
Licensed User
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

I use this code to draw a polyline and #AdditionalJar: mapsutil on main activity
Only Android or B4A
 
Upvote 0
Top