I had this problem, when calculating the distance between two points on the map, the data update is not every 1 kilometer there at least, but every 50 - 90 km I count like this:

public void getDistanceMeters() { Location locationA = new Location("point A"); locationA.setLatitude(lat); locationA.setLongitude(lng); Location locationB = new Location("point B"); locationB.setLatitude(lat2); locationB.setLongitude(lng2); int currentDistance = (int) locationA.distanceTo(locationB) / 1000; totalDistance += Math.abs(distance - currentDistance); distance = currentDistance; } 

Maybe you need to change something that would be updated more often?

  • I am sorry, and why type (int)? Does he not discard the fractional part? - istem
  • I don’t need the fractional part - dajver
  • one
    Ah, then there are no questions. (plus or minus kilometer) - istem

1 answer 1

In this method, you will be able to speed up something, try to declare several providers for location determination (both Internet and GPS, you can also passive)

Another tip: the distanceTo () method gives the wrong distance (I recently ran into the same task, the data from the iPhone did not agree with mine. I looked at Google and Yandex maps for interest, it turned out that there is a difference of 10 km)

Use this formula:

 double lon, lat, lon2, lat2; double result = 111.2 * Math.sqrt( (lon - lon2)*(lon - lon2) + (lat - lat2)*Math.cos(Math.PI*lon/180)*(lat - lat2)*Math.cos(Math.PI*lon/180)); 
  • I will try thanks - dajver