convert meters to latitude longitude from any point

前端 未结 2 1092
小鲜肉
小鲜肉 2020-12-09 20:18

In my project I have to find [latitude, longitude] coordinate(s) from one point in distance of 500 meters (this could be any random coordinate or an array of coordinates aro

相关标签:
2条回答
  • 2020-12-09 20:50

    If I understand your question right you have a known point in Lat/Long and you need calculate the Lat/Long of another point or points 500m away from your starting point.

    If this is what you are doing, you have several options most of which involve specialist GIS APIs. However, I'm guesing you're a programmer/mathematician rather than a Geographer so, you may prefer to opt for using the Haversine formula. You can find a discussion on this topic here plus the formula.

    One caveat is that the distamce you are working with (500m is quite small) and the Earth is far from being a perfect sphere or even a slightly flattened spheroid. It is locally "lumpy" and that can put your calculation out. If you need more accuracy you will have to account for these imperfections by using an appropriate local Datum (model of the Earth - there are many e.g. see EPSG list) and to do that you will probably need to start using the GIS libraries as the maths gets very detailed otherwise.

    0 讨论(0)
  • 2020-12-09 20:50

    This is the code used by google map (SphericalUtil.java)

      // from  SphericalUtil.java
      // compile 'com.google.maps.android:android-maps-utils:0.4.4'
      public static LatLng computeOffset(LatLng from, double distance, double heading) {
        distance /= 6371009.0D;  //earth_radius = 6371009 # in meters
        heading = Math.toRadians(heading);
        double fromLat = Math.toRadians(from.latitude);
        double fromLng = Math.toRadians(from.longitude);
        double cosDistance = Math.cos(distance);
        double sinDistance = Math.sin(distance);
        double sinFromLat = Math.sin(fromLat);
        double cosFromLat = Math.cos(fromLat);
        double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * Math.cos(heading);
        double dLng = Math.atan2(sinDistance * cosFromLat * Math.sin(heading), cosDistance - sinFromLat * sinLat);
        return new LatLng(Math.toDegrees(Math.asin(sinLat)), Math.toDegrees(fromLng + dLng));
    }
    

    to use it, you just have to enter the centerLatLng, the distance in meters, and the heading in degrees from centerLatLng.

    you can change the formula to the language of your preference.

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