android studio两点间距离测量代码
时间: 2023-11-21 19:56:27 浏览: 68
百度地图俩点距离计算Android代码
4星 · 用户满意度95%
以下是Android Studio中测量两点间距离的代码:
```java
// 创建LocationManager对象
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 获取最后已知的位置信息
Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// 计算两点间距离
float distance = location1.distanceTo(location2);
// 输出距离
Log.d("Distance", "Distance between two points is: " + distance);
```
需要注意的是,上述代码需要在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
```
阅读全文