Android Question Google Maps Polyline & Waypoints In The Vicinity

Discussion in 'Android Questions' started by RichardN, Feb 17, 2015.

  1. RichardN

    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.......
  2. warwound

    warwound Expert Licensed User

  3. RichardN

    RichardN Active Member Licensed User

    Thanks Warwound I have not. I'll take a look.

  4. eurojam

    eurojam Well-Known Member Licensed User

    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 ( to convert lat/lon to UTM.

    'Calculate the distance between the point and the segment.
    'original from
    Public Sub DistToSegment(myLoc As LatLng, p1 As LatLng, p2 As LatLngAs 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
    Dim u As UTM
    Dim ll As LatLon
       ll.Initialize = myLoc.Latitude
       ll.lon = myLoc.Longitude
       u = g.WGS84LatLonToUTM(ll)
       ll.Initialize = p1.Latitude
       ll.lon = p1.Longitude
       u = g.WGS84LatLonToUTM(ll)
       X1 = u.X
       Y1 = u.Y
       ll.Initialize = 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 = 


    ' 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 = 
    Else If t > 1 Then
                dx = px - X2
                dy = py - Y2
                near_x = X2
                near_y = Y2
                p_near = 
                near_x = X1 + t * dx
                near_y = Y1 + t * dy
                dx = px - near_x
                dy = py - near_y
                p_near = 
    End If

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

    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.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice