开发者

Android getting the distance using a Location object

I’d like to take a series of samples of coordinates returned by GPS and calculate the (straight line) distance between them so I can graph the distances via Excel. I see the method distanceBetween and distanceTo of the Location class, but I’m concerned these don’t return the straight line distance.

Does anyone know what distance is returned by 开发者_开发百科these methods or if there are any ways to calculate straight line distance based on latitude/longitude values returned by the Location class?


A Google search will offer solutions should you somehow desire to do this calculation yourself. For example the Haversine approach:

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;

is reported here. Note that this is straight line and does not account for irregularities in elevation, etc.


How can I measure distance and create a bounding box based on two latitude+longitude points in Java?

provides a Java implementation of the Haversine approach.


Here my code

float[] result=new float[1];
        if(cordenatalar.size()>1)
        {

            LatLong add_kor=(LatLong)cordenatalar.get(cordenatalar.size()-1);
            Location.distanceBetween(add_kor.getLat(), add_kor.getLongg(), location.getLatitude(), location.getLongitude(), result);
            kilometr+=result[0];
            //KMTextView.setText(String.valueOf(kilometr));
        }
        KMTextView.setText("sss: "+String.valueOf(cordenatalar.size()+"  res: "+kilometr+" metr"));
        cordenatalar.add(new LatLong(location.getLatitude(), location.getLongitude()));


source = starting location;

destination = current location;

/* this method will fetch you the distance between two geo points. */

source.distanceTo(destination);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜