# Calculating distance from latitude and longitude

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
• Ah, then there are no questions. (plus or minus kilometer) - istem

` `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));` `