获取当前位置错误?
getting current location wrong?
我是 Android 开发的新手,我想从我的错误中吸取教训,但我首先需要了解我在这里做错了什么:
我正在尝试获取我当前的位置
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
LatLng coordinate = new LatLng(loc.getLatitude(), loc.getLongitude());
CameraUpdate currentLocation = CameraUpdateFactory
.newLatLngZoom(coordinate, 16);
mMap.animateCamera(currentLocation);
String Text = "My current location is: " +
"Latitude = " + loc.getLatitude() +
"Longitude = " + loc.getLongitude();
Log.d("Dana","acc="+loc.getAccuracy());
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
1);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
1);
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, locationListener);
但是在模拟器和我的真实设备上我的位置是在英格兰的某个地方,即使我的 phone 已激活位置。为什么会这样?为什么不检索我当前的真实位置?我已经记录了准确度,它只有 20...
此外,即使 activity 已关闭,有没有办法存储该位置?因为我已经看到,当我关闭 activity 并重新打开它时,它不会再次调用 onLocationChanged 函数。
你是在封闭的建筑物内还是什么的,试着带着你的设备出去看看。如您所知,GPS 在空旷的环境下效果更好。
如果你愿意,你可以使用 GoogleLocationAPI 而不是这个,我认为它比你使用的更有效。搜索 google,您会找到示例。
我是 Android 开发的新手,我想从我的错误中吸取教训,但我首先需要了解我在这里做错了什么: 我正在尝试获取我当前的位置
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
LatLng coordinate = new LatLng(loc.getLatitude(), loc.getLongitude());
CameraUpdate currentLocation = CameraUpdateFactory
.newLatLngZoom(coordinate, 16);
mMap.animateCamera(currentLocation);
String Text = "My current location is: " +
"Latitude = " + loc.getLatitude() +
"Longitude = " + loc.getLongitude();
Log.d("Dana","acc="+loc.getAccuracy());
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
1);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
1);
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, locationListener);
但是在模拟器和我的真实设备上我的位置是在英格兰的某个地方,即使我的 phone 已激活位置。为什么会这样?为什么不检索我当前的真实位置?我已经记录了准确度,它只有 20...
此外,即使 activity 已关闭,有没有办法存储该位置?因为我已经看到,当我关闭 activity 并重新打开它时,它不会再次调用 onLocationChanged 函数。
你是在封闭的建筑物内还是什么的,试着带着你的设备出去看看。如您所知,GPS 在空旷的环境下效果更好。
如果你愿意,你可以使用 GoogleLocationAPI 而不是这个,我认为它比你使用的更有效。搜索 google,您会找到示例。