Android Find Latitude Longitude Of X point From Defined Location

后端 未结 2 486
离开以前
离开以前 2020-12-30 15:21

i m working on Android MapView and developing a map based Application. i Need to find an X distance from the Specific Co-ordinates. Di

相关标签:
2条回答
  • 2020-12-30 16:03

    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;
    }
    
    0 讨论(0)
  • 2020-12-30 16:22

    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;
    };
    
    0 讨论(0)
提交回复
热议问题