Find center geopoint between start geo point and end geo point on android

后端 未结 3 1196
梦如初夏
梦如初夏 2020-12-11 19:05

I have start latitude, longitude and end latitude,longitude.

I find Geo Point using latitude and longitude.

After that i draw line between the two Geo points

相关标签:
3条回答
  • 2020-12-11 19:25

    You can use Haversine formula snippet and checked with this page

    public static void midPoint(double lat1,double lon1,double lat2,double lon2){
    
    double dLon = Math.toRadians(lon2 - lon1);
    
    //convert to radians
    lat1 = Math.toRadians(lat1);
    lat2 = Math.toRadians(lat2);
    lon1 = Math.toRadians(lon1);
    
    double Bx = Math.cos(lat2) * Math.cos(dLon);
    double By = Math.cos(lat2) * Math.sin(dLon);
    double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
    double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
    
    //print out in degrees
    System.out.println(Math.toDegrees(lat3) + " " + Math.toDegrees(lon3));
    }
    
    0 讨论(0)
  • 2020-12-11 19:29

    The checked solution does not work for me.

    Anyway, i have found an easiest solution :

       private GeoPoint midPoint(double lat1,double lon1,double lat2,double lon2){
            //create origin geopoint from parameters
            GeoPoint origin = new GeoPoint(lat1,lon1);
            //create destination geopoints from parameters
            GeoPoint destination = new GeoPoint(lat2,lon2);
            //calculate and return center
            return GeoPoint.fromCenterBetween(origin, destination);
        }
    

    If you have origin and destination as GeoPoint, you can use directly "GeoPoint.fromCenterBetween" method.

    0 讨论(0)
  • 2020-12-11 19:41

    You can approximate the midpoint by taking the average of the latitudes of the two points and the average of the longitudes.

    If you need it a bit more precisely, you can use the Haversine formula and some algebra to derive the midpoint.

    0 讨论(0)
提交回复
热议问题