Android how to use location.distanceTo()

…衆ロ難τιáo~ 提交于 2019-12-05 05:13:18

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
Chi Minh Trinh

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

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

doc: here

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.

For Google's official documentation read here.

And for a tutorial read here.

Hope this would help!!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!