Android Find Latitude Longitude Of X point From Defined Location

后端 未结 2 485
离开以前
离开以前 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;
    }
    

提交回复
热议问题