Java 在android中获取地图上的当前位置
我正在使用此帖子:获取设备的当前位置。我的代码是:Java 在android中获取地图上的当前位置,java,android,google-maps,location,Java,Android,Google Maps,Location,我正在使用此帖子:获取设备的当前位置。我的代码是: locationResult = new LocationResult() { @Override public void gotLocation(Location location) { if (location != null) { latitude = location.getLatitude(); longitude
locationResult = new LocationResult() {
@Override
public void gotLocation(Location location) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
};
myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
mapController = mapView.getController();
point = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
mapController.animateTo(point);
Toast.makeText(getApplicationContext(), ("Latitude = "+latitude+" Longitude = "+longitude), Toast.LENGTH_SHORT).show();
mapController.setZoom(17);
mapView.invalidate();
纬度和经度声明为全局双类型变量。但是,在运行应用程序后,我得到了蓝屏。我想,问题是我的代码不能得到纬度和经度的值。谁能帮帮我吗?谢谢 您的问题并没有告诉我您的代码到底出了什么问题,但如果您真正需要做的是获取设备的当前位置,我可以给您一个提示 提示:尝试使用位置侦听器。如果设备位置发生更改,它将为您提供lat和long值 代码: 你的活动
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
你的听众课
@Override
public void onLocationChanged(Location loc){
//you are getting values
loc.getLatitude();
loc.getLongitude();
}
明示许可
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
我想你忘了这个权限
谢谢。我要试试。:-)