Java ANdroid中的位置未更新
我想获得手机当前位置,当我安装应用程序时,比第一次获得正确的lat-long,但比每次显示第一次获得位置后获得正确的lat-long。请给我正确的代码以获得每次正确的lat-long。 _在排序中,我的位置未更新,或位置cach不清楚。 ->我的代码 公共类MyLocationListener实现LocationListener{Java ANdroid中的位置未更新,java,android,Java,Android,我想获得手机当前位置,当我安装应用程序时,比第一次获得正确的lat-long,但比每次显示第一次获得位置后获得正确的lat-long。请给我正确的代码以获得每次正确的lat-long。 _在排序中,我的位置未更新,或位置cach不清楚。 ->我的代码 公共类MyLocationListener实现LocationListener{ private LocationManager mLocationManager; private Location currentLocation; public
private LocationManager mLocationManager;
private Location currentLocation;
public MyLocationListener(Context context) {
mLocationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
currentLocation = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, this);
}
public Location getBestLocationAvailable() {
return currentLocation;
}
@Override
public void onLocationChanged(Location newBestLocation) {
mLocationManager.removeUpdates(this);
currentLocation = newBestLocation;
}
@Override
public void nProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
位置=loc.getBestLocationAvailable;
system.out.printlnlocation.getLatitude++location.getLatitude
给我一个书面的答案,为什么我会老得很长?试着把纬度和经度放在onlocation技术里面
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
String lat = loc.getLatitude();
String longt= loc.getLongitude();
Toast.makeText(getApplicationContext(),"Lat"+ lat+ "Long"+longt, Toast.LENGTH_LONG).show();
}
在menifest中添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
您的代码看起来不错,但为什么要添加此字符串
mLocationManager.removeUpdates(this);
因此,您将只收到一个位置更新,更新后您的侦听器将被禁用。请尝试删除此字符串并再次检查
另外,请注意,不同的设备需要不同的时间进行GPS冷启动。在测试应用程序之前,请检查其他应用程序(例如地图或GPS监视器)中的GPS状态。如果您的设备处于冷态,您将不会收到任何长时间10-20分钟或更长时间的更新。我想使用Gps在没有互联网的情况下获取lat long。此外,我还更新了OnLocationChange中的位置,而不需要使用OnLocationChange方法获取lat long。请告诉我您的最新情况。可能位置未更新。。您确定调用了OnCoationChanged方法吗?
mLocationManager.removeUpdates(this);