android ACTIVITY中写一个方法返回经度值
时间: 2023-06-14 21:08:37 浏览: 49
你可以在你的 Android Activity 中添加以下方法来获取经度值:
```java
public double getLongitude() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
try {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (location != null) {
return location.getLongitude();
}
} catch (SecurityException e) {
e.printStackTrace();
}
}
return 0.0;
}
```
请注意,此方法需要权限访问用户的位置信息。你需要在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
```