Distance for GPS Application [duplicate]

最后都变了- 提交于 2020-01-13 15:53:36

问题


Possible Duplicate:
How to track distance via GPS on Android?

I have designed a GPS application and its telling my location well. But Now I want to include more feature. How I will make a radius there? to have a surrounding area of 5 or 6 km! How I can mention the distance between a place on that area and my place?


回答1:


I feel like this question is starting to turn into a lot of questions. I decided to tackle this answer by directing it towards your question title "Distance for GPS Application".

In my application, instead of using Google's API's I request the users distance from a list of GPS coordinates by doing the following:

In my JJMath Class:

Getting distance (Haversine Formula, in miles):

/**
 * @param lat1
 * Latitude which was given by the device's internal GPS or Network location provider of the users location
 * @param lng1
 * Longitude which was given by the device's internal GPS or Network location provider of the users location 
 * @param lat2
 * Latitude of the object in which the user wants to know the distance they are from
 * @param lng2
 * Longitude of the object in which the user wants to know the distance they are from
 * @return
 * Distance from which the user is located from the specified target
*/
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);
    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    return dist;
}

Then I round that number by:

/** This gives me numeric value to the tenth (i.e. 6.1) */
public static double round(double unrounded) {
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(1, BigDecimal.ROUND_CEILING);
    return rounded.doubleValue();
}

I don't work with Map overlays, but I am sure there are great tutorials or answers to come.




回答2:


If you simply have different coordinates and want to do calculations with them, just check out the Android functions already available for it: http://developer.android.com/reference/android/location/Location.html

You can create Location objects, put lat/long coordinates with the set-functions and then just use

float distanceInMeters=location1.distanceTo(location2);

to get results.



来源:https://stackoverflow.com/questions/13181010/distance-for-gps-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!