Android Question Google Maps Polyline & Waypoints In The Vicinity

RichardN

Active Member
Licensed User
I have a Google Maps application that depicts a polyline route between two geographic points. I have a lat/long Sqlite database of points of interest that I wish to display where they are in the vicinity of that route.

Executing an SQL 'BETWEEN' query that returns all lat/long points in the quadrangle desribed by the start/finish points is a first attempt but returns too many waypoints that are of no interest. If I could extract only the waypoints within X radius of start/finish locations plus all waypoints X distance either side of the route it would be the perfect solution.

For the resident experts please.......
 

eurojam

Well-Known Member
Licensed User
Richard,
as Martin said, using spatialite is the best choice to make spatial queries. alternativly you can code your own spatial routines. I have used this routine (converted from good old VB6) to compute the distance to a segment. You have to use it together with the geodesic Lib (http://www.b4x.com/android/forum/threads/class-geodesic-convert-lat-lon-utm-coordinates.30702/) to convert lat/lon to UTM.

B4X:
'Calculate the distance between the point and the segment.
'original from http://vb-helper.com/howto_distance_point_to_line.html
Public Sub DistToSegment(myLoc As LatLng, p1 As LatLng, p2 As LatLng) As Double
Dim dx As Double
Dim dy As Double
Dim t As Double
'Dim dm As doManeuver
Dim dist As Double
Dim px , py , X1 , Y1 , X2 , Y2 , near_x , near_y As Double
Dim p_near As String
Dim g As Geodesic
   'dm.Initialize
   g.Initialize
   Dim u As UTM
   Dim ll As LatLon
   ll.Initialize
   ll.lat = myLoc.Latitude
   ll.lon = myLoc.Longitude
   u = g.WGS84LatLonToUTM(ll)
   px=u.X
   py=u.Y
   ll.Initialize
   ll.lat = p1.Latitude
   ll.lon = p1.Longitude
   u = g.WGS84LatLonToUTM(ll)
   X1 = u.X
   Y1 = u.Y
   ll.Initialize
   ll.lat = p2.Latitude
   ll.lon = p2.Longitude  
   u = g.WGS84LatLonToUTM(ll)
   X2 = u.X
   Y2 = u.Y  
    dx = X2 - X1
    dy = Y2 - Y1

'   Log("dx: " & dx)
'   Log("dy: " & dy)
   
    If dx = 0 AND dy = 0 Then
        ' It's a point not a line segment.
        dx = px - X1
        dy = py - Y1
        near_x = X1
        near_y = Y1
        dist = Sqrt(dx * dx + dy * dy)
        p_near = "P1"

    Else
   

        ' Calculate the t that minimizes the distance.
        t = ((px - X1) * dx + (py - Y1) * dy) / (dx * dx + dy * dy)

        ' See if this represents one of the segment's
        ' end points or a point in the middle.
        If t < 0 Then
            dx = px - X1
            dy = py - Y1
            near_x = X1
            near_y = Y1
            p_near = "P1"
                   
        Else If t > 1 Then
            dx = px - X2
            dy = py - Y2
            near_x = X2
            near_y = Y2
            p_near = "P2"
        Else
            near_x = X1 + t * dx
            near_y = Y1 + t * dy
            dx = px - near_x
            dy = py - near_y
            p_near = "0"
        End If

        dist = Sqrt(dx * dx + dy * dy)
    End If
    'Log("Dist " & dist & " Near " & P_near)
    Return dist
End Sub
 

RichardN

Active Member
Licensed User
Thanks Guys,

The Spatialite library is no doubt the business for mapping and tiling but I am a mere mortal and it looks like someone's PhD project :) In any case I need to encrypt and there seem to be complications using SQLcipher.

I have arrived at a quick & dirty way of defining departure point/route/arrival proximity waypoints. I was interested in waypoints within ~100nm of track so....

- Determine the overall track & distance
- Define a List as long as the number of database waypoints (about 500 in my case) and populate with a big number
- Step down the track each 100nm making a new geopoint each time
- Compare the off-track distance already stored in the the list and decrement as required.
- Write the list to an SQL field and query by OffsetRange < 100nm

As long as you manipulate the data within a List rather than directly to the database it is quite fast.
 
Top