简体   繁体   English

如何在Android中使用经度和纬度查找两点之间的距离

[英]how to find distance between two points using latitudes and longitude in android

I have used two buttons.one two get the current location. 我使用了两个按钮,其中两个获得当前位置。 Next button is a start button which when I click should give the distance between current location and location after 5 metres 下一个按钮是一个开始按钮,当我单击该按钮时,应该给出当前位置和5米后位置之间的距离

If you know lat long for both point then you can calculate distance between those two points in following way 如果您知道两个点的纬度都长,则可以按照以下方式计算这两个点之间的距离

double distance;
Location oldlocation = new Location("");
oldlocation.setLatitude(main_Latitude);
oldlocation.setLongitude(main_Longitude);
Location newlocation = new Location("");
newlocation.setLatitude(sub_Latitude);
newlocation.setLongitude(sub_Longitude);
distance = oldlocation.distanceTo(newlocation);

Use following api 使用以下API

GET http://maps.googleapis.com/maps/api/directions/json?origin="sourceLat,sourceLang"&destination="destLat,destLan"g&sensor=false

You'll end up with some JSON. 您最终会得到一些JSON。 In routes[].legs[].distance, you'll get an object like this: 在routes []。legs []。distance中,您将获得如下对象:

"legs" : [
    {
       "distance" : {
          "text" : "542 km",
          "value" : 542389
       },

Use This method: 使用此方法:

public double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    if (unit == 'K') {
        dist = dist * 1.609344;
    } else if (unit == 'N') {
        dist = dist * 0.8684;
    }
    return (dist);
}
 String currentlat="";// put the lat
String currentlong="";// put the long
        String str_lat=""; //put the destination lat or last point to get the distance
        String str_lng="";//put the destination longtitude 




            int Radius = 6371;// radius of earth in Km
            double lat1 = Double.valueOf(Str_userlat);
            double lat2 = Double.valueOf(str_lat);
            double lon1 = Double.valueOf(Str_userlng);
            double lon2 = Double.valueOf(str_lng);

            double dLat = Math.toRadians(lat2 - lat1);
            double dLon = Math.toRadians(lon2 - lon1);
            double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                    + Math.cos(Math.toRadians(lat1))
                    * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
                    * Math.sin(dLon / 2);
            double c = 2 * Math.asin(Math.sqrt(a));
            double valueResult = Radius * c;
            double km = valueResult / 1;
            DecimalFormat newFormat = new DecimalFormat("####");
            int kmInDec = Integer.valueOf(newFormat.format(km));
            double meter = valueResult % 1000;
            int meterInDec = Integer.valueOf(newFormat.format(meter));
            Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
                    + " Meter   " + meterInDec);

            String str_location="0";
            if(kmInDec==0)
            {
                tvLocation.setText("<"+String.valueOf(meterInDec)+" m");
                str_location=String.valueOf(meterInDec)+" m";

            }
            else if(kmInDec==0&&meterInDec==0)
            {
                tvLocation.setText("<"+String.valueOf(meterInDec)+" m");
                str_location=String.valueOf(meterInDec)+" m";
            }
            else
            {
                tvLocation.setText("<"+String.valueOf(kmInDec)+" kM");
                str_location=String.valueOf(kmInDec)+" km";

            }

Look at bellow Url google provides utility library for most of the Maps use case. 看看下面的网址,谷歌为大多数Maps用例提供了实用程序库。

https://developers.google.com/maps/documentation/android-api/utility/#introduction https://developers.google.com/maps/documentation/android-api/utility/#introduction

For calculating distance between two lat longs: 计算两个纬度之间的距离:

SphericalUtil.computeDistanceBetween(LatLng from, LatLng to);

Do This Way 这样做

double distance;
        Location locationA = new Location("Kaaba");
        locationA.setLatitude(21.45);
        locationA.setLongitude(-39.75);
        Location locationB = new Location("My Location");
        locationB.setLatitude("17.20");
        locationB.setLongitude("73.12");

        distance = locationA.distanceTo(locationB) / 1000; //in Km

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM