Java Android 如何使用 location.distanceTo()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28209548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:47:43  来源:igfitidea点击:

Android how to use location.distanceTo()

javaandroidstringgoogle-mapslocation

提问by Joolah

I am using a fragement, I need to get my currently Location (lat and lng) and I need to calculate my currently location with the destination location.

我正在使用片段,我需要获取我当前的位置(lat 和 lng),我需要使用目标位置计算我当前的位置。

Can you pls give me a hand. what is the best way to calculate the location:

你能帮我一把吗?计算位置的最佳方法是什么:

How can I get the distance from my currently location and the destination location using lat and lng ;

如何使用 lat 和 lng 获得我当前位置和目的地位置的距离;

    LocationManager locationManager;
    Location locatio;



locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);





String product_latitude     = "3.121191";
            String product_longtitude   = "101.673298";

            Double latitude     = Double.valueOf(product_latitude);
            Double longtitude   = Double.valueOf(product_longtitude);

            // Calculate the Distance
            String product_distance = location.distanceTo(latitude, longtitude);

回答by Jai Rajesh

Try this

尝试这个

double latitude=lat;
double longitude=lng;    
float distance=0;
Location crntLocation=new Location("crntlocation");
crntLocation.setLatitude(currentLatitude);
crntLocation.setLongitude(currentLongitude);

Location newLocation=new Location("newlocation");
newLocation.setLatitude(latitude);
newLocation.setLongitude(longitude);


//float distance = crntLocation.distanceTo(newLocation);  in meters
distance =crntLocation.distanceTo(newLocation) / 1000; // in km

回答by AniV

Use Direction API to find to the distance from your current location to the destination. It returns a JSON object as distance which gives you the distance in mile, you can parse that JSON and display that on your app screen.

使用 Direction API 来查找从您当前位置到目的地的距离。它返回一个 JSON 对象作为距离,它为您提供以英里为单位的距离,您可以解析该 JSON 并将其显示在您的应用程序屏幕上。

For Google's official documentation read here.

有关 Google 的官方文档,请阅读此处

And for a tutorial read here.

有关教程,请阅读此处

Hope this would help!!!

希望这会有所帮助!!!

回答by Chi Minh Trinh

You can use static method of Location: distanceBetweento get distance between 2 locations.

您可以使用 Location: distanceBetween 的静态方法来获取 2 个位置之间的距离。

float [] results = new float[5]; 
Location.distanceBetween(startLatitude, startLongitude, endLatitude,
endLongitude, results)

doc: here

医生:这里

回答by Mohsin Falak

"distanceTo" gives an approximate result...

“distanceTo”给出了一个近似结果......

You can use DistanceMatrix API to get a close to accurate results.

您可以使用 DistanceMatrix API 来获得接近准确的结果。

DistanceMatrix API

距离矩阵 API