Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Find Latitude Longitude Of X point From Defined Location

i m working on Android MapView and developing a map based Application. i Need to find an X distance from the Specific Co-ordinates. Direction is not my priority Distance is my priority let say i need to find 100 meters from a particular location any idea on how can i do that Thanks in advance for reading and answering.

like image 937
Usman Kurd Avatar asked Jan 15 '13 06:01

Usman Kurd


People also ask

How do I find the latitude and longitude of an address on Android?

The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);

How do I find my coordinates using destination?

To search for a place, enter the latitude and longitude GPS coordinates on Google Maps. You can also find the coordinates of the places that you've previously found. Besides longitude and latitude, you can use Plus Codes to share a place without an address.


2 Answers

in order to calculate to find a point on a line a given distance away from an origin, you need to have a bearing (or direction) as well as the distance. Here is a function that will take a starting location, a bearing and a distance (depth) and return a destination location (for Android): You may want to conver it from KM to Meters or whatever.

public static Location GetDestinationPoint(Location startLoc, float bearing, float depth) 
{ 
    Location newLocation = new Location("newLocation");

    double radius = 6371.0; // earth's mean radius in km 
    double lat1 = Math.toRadians(startLoc.getLatitude()); 
    double lng1 = Math.toRadians(startLoc.getLongitude()); 
    double brng = Math.toRadians(bearing); 
    double lat2 = Math.asin( Math.sin(lat1)*Math.cos(depth/radius) + Math.cos(lat1)*Math.sin(depth/radius)*Math.cos(brng) ); 
    double lng2 = lng1 + Math.atan2(Math.sin(brng)*Math.sin(depth/radius)*Math.cos(lat1), Math.cos(depth/radius)-Math.sin(lat1)*Math.sin(lat2)); 
    lng2 = (lng2+Math.PI)%(2*Math.PI) - Math.PI;  

    // normalize to -180...+180 
    if (lat2 == 0 || lng2 == 0) 
    {
        newLocation.setLatitude(0.0);
        newLocation.setLongitude(0.0);
    }
    else
    {
        newLocation.setLatitude(Math.toDegrees(lat2));
        newLocation.setLongitude(Math.toDegrees(lng2));
    }

    return newLocation;
};
like image 90
javram Avatar answered Sep 17 '22 23:09

javram


Just to make the answer from javram into meters and radians instead of degrees.

/**
 * Create a new location specified in meters and bearing from a previous location.
 * @param startLoc from where
 * @param bearing which direction, in radians from north
 * @param distance meters from startLoc
 * @return a new location
 */
public static Location createLocation(Location startLoc, double bearing, double distance) {
    Location newLocation = new Location("newLocation");

    double radius = 6371000.0; // earth's mean radius in m
    double lat1 = Math.toRadians(startLoc.getLatitude());
    double lng1 = Math.toRadians(startLoc.getLongitude());
    double lat2 = Math.asin(Math.sin(lat1) * Math.cos(distance / radius) + Math.cos(lat1) * Math.sin(distance / radius) * Math.cos(bearing));
    double lng2 = lng1 + Math.atan2(Math.sin(bearing) * Math.sin(distance / radius) * Math.cos(lat1), Math.cos(distance / radius) - Math.sin(lat1) * Math.sin(lat2));
    lng2 = (lng2 + Math.PI) % (2 * Math.PI) - Math.PI;

    // normalize to -180...+180
    if (lat2 == 0 || lng2 == 0) {
        newLocation.setLatitude(0.0);
        newLocation.setLongitude(0.0);
    } else {
        newLocation.setLatitude(Math.toDegrees(lat2));
        newLocation.setLongitude(Math.toDegrees(lng2));
    }

    return newLocation;
}
like image 30
Gussoh Avatar answered Sep 20 '22 23:09

Gussoh