Android Question Distance between two points

Beja

Expert
Licensed User
Longtime User
Hi,
Long time ago, Erel and Klaus presented an answer to the above question, but I didn't like to resurrect that post, so I will just quote Erel's comment
Location.DistanceTo returns the distance in meters between two Location objects. The current one and the one passed as a parameter.

Question:
Is "The current one" the same is the one obtained from LocationChanged? Can I consider it a necessary redundancy?
Do I have to put the absolute values of location1 (lat lon)?
Thanks
 

eurojam

Well-Known Member
Licensed User
Longtime User
Beja,
here is an example of one of my apps which has set of Point objects (points of a self guided tour). when the location change occurs, I use the Location.Distanceto in a loop to get the current distance to each point of interest:
B4X:
Sub OnMyLocationChangeListener1_MyLocationChange(location1 As Location)

    For j = 0 To POImap.Size-1
      Dim dLabel As Label
      Dim p As POI
      Dim loc As Location
      Dim d As Double
     
      p = POImap.GetValueAt(j)
      dLabel = LabelMap1.Get(p.id)
      loc.Initialize2(p.lat,p.lon)
      d = loc.DistanceTo(location1)
      p.dist = d
      POImap.Put(p.id,p)
      If d < 1000 Then
         dLabel.Text = "Entfernung: " & NumberFormat(d,0,0) & " m"
      Else
         dLabel.Text = "Entfernung: " & NumberFormat((d/1000),0,2) & " km"
      End If
      dLabel.Invalidate
    Next

End Sub

hope this helps to answer your question, maybe I've missunderstood it...;-)
stefan
 
Upvote 0

Rene Barrera

Member
Licensed User
Longtime User
Hi Stefan, is it posible for you to post the complete example due that i didn't understand for instance what is "POImap.Size".
I want to make an app that each 15 minutes gets the location, and know the distance between to points.

Thanks in advance
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
Hi Rene,
the example is part of a large self-guided tour project (over 5000 lines of code) which I developed for a customer, so I can not post the whole project.
But I can give you some more explanation to the example:

B4X:
Sub Process_Globals
....
Type POI (lat As Double, lon As Double, tour As Int, visited As Int) 'this is the Type which holds the point information (POI=Point of interest)
Dim POImap As Map 'this is a Map/Dictionary which holds the various points of my tour
....
end sub

Sub OnMyLocationChangeListener1_MyLocationChange(location1 As Location)

    'when the location changed we loop trough the map and calculationg the distance of each Point of Interest. The labels are also in a map with the same key as the POI
    For j = 0 To POImap.Size-1
      Dim dLabel As Label
      Dim p As POI 'type declared in process_globals
      Dim loc As Location
      Dim d As Double
  
      p = POImap.GetValueAt(j)
     'Each POI as a corresponding label in a scrollview showing it's distance to my position....
      dLabel = LabelMap1.Get(p.id)
      loc.Initialize2(p.lat,p.lon)
      d = loc.DistanceTo(location1)
      p.dist = d
      POImap.Put(p.id,p)
     'now we are changing the distance information for each poi (label in a scrollview)
      If d < 1000 Then
         dLabel.Text = "Entfernung: " & NumberFormat(d,0,0) & " m"
      Else
         dLabel.Text = "Entfernung: " & NumberFormat((d/1000),0,2) & " km"
      End If
      dLabel.Invalidate
    Next

End Sub


hope this helps

stefan
 
Upvote 0

Rene Barrera

Member
Licensed User
Longtime User
Stefan, it is very kind of you, this is very helpful. just one more question:
- What is inside location1 in the line "d = loc.DistanceTo(location1)".
(I tried to put (Lat, Lon) but i got an error) This is realy what i am looking for

I appreciate your support
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
OnMyLocationChangeListener1 is a listener declared in the MapFragment1_Ready Sub (it is part of the google maps API)
B4X:
Sub MapFragment1_Ready
....
    Dim OnMyLocationChangeListener1 As OnMyLocationChangeListener
     OnMyLocationChangeListener1.Initialize("OnMyLocationChangeListener1")
     GoogleMapsExtras1.SetOnMyLocationChangeListener(GoogleMap1, OnMyLocationChangeListener1)
...
every time when the location of the device changed, the Sub OnMyLocationChangeListener1_MyLocationChange(location1 As Location) starts to work where location1 is the location of the device. But you can also use the GPS location listener, it is nearly the same:
B4X:
Sub GPS_LocationChanged (Location1 As Location)
'where location 1 ist the location of the GPS
end sub

hope this is not confusing...
 
Upvote 0

Rene Barrera

Member
Licensed User
Longtime User
Hi Stefan, no, this is not confusing, now it´s clear for me
I reiterate that I appreciate your support.

Best Regards
Armando Barrera
 
Upvote 0
Top