Google 地图 API 显示位置,但不居中
Google Maps API shows location, but doesn't center
我在 Android Studio 2.2.3 中使用 Google 地图 API,由于某种原因,位置按钮可以使用,但它不居中。此外,我得到了一条消息,没有找到位置。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getLocationPermission();
}
private void getDeviceLocation() {
Log.d(TAG, "getDeviceLocation: getting the devices current location");
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try {
if (mLocationPermissionsGranted) {
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful() && task.getResult() != null) {
Log.d(TAG, "onComplete: found location");
Location currentLocation = (Location) task.getResult();
moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),
DEFAULT_ZOOM);
} else {
Log.d(TAG, "onComplete: current location is null");
Toast.makeText(MapActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
Location currentLocation = (Location) task.getResult();
}
}
});
}
} catch (SecurityException e) {
}
}
以上代码是我认为用于查找位置然后将相机定位到给定位置的代码。我没有收到任何错误,当我将 LocationButton 设置为 true,然后单击它时,它确实以我的位置为中心。
将该代码更改为
private void getDeviceLocation(){
try{
if(mLocationPermissionGranted){
Task locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful() && task.getResult() != null){
Log.d(TAG, "onComplete: location found!");
Location currentLocation = (Location) task.getResult();
float currentZoom = mMap.getCameraPosition().zoom;//edit (added line)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(),
currentLocation.getLongitude()), DEFAULT_ZOOM));//edit (changed line)
}else{
Log.d(TAG, "onComplete: current location is null");
Toast.makeText(MapActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
}
}
});
}
}catch (SecurityException e){
}
}
我设法得到了一个 return 和一个位置,在我手动设置设备的位置之后,因为出于某种原因无法立即确定位置。
现在问题已经解决了!
试过你的代码。
它对我不起作用,但我解决了问题。
这是正确的代码。
private void getDeviceLocation(){
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try{
if(mLocationPermissionGranted){
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful() && task.getResult() != null){
Toast.makeText(DriverMapActivity.this, "Task is successful", Toast.LENGTH_SHORT).show();
Location currentLocation = (Location) task.getResult();
mLatLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
moveCamera(mLatLng, DEFAULT_ZOOM);
}else{
Toast.makeText(DriverMapActivity.this, "Task is unsuccessful", Toast.LENGTH_SHORT).show();
}
}
});
}
}catch (SecurityException e){
Toast.makeText(this, "SecurityException Found", Toast.LENGTH_SHORT).show();
}
}
因此,如果您的应用程序崩溃,您可能想试试这个。
我在 Android Studio 2.2.3 中使用 Google 地图 API,由于某种原因,位置按钮可以使用,但它不居中。此外,我得到了一条消息,没有找到位置。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getLocationPermission();
}
private void getDeviceLocation() {
Log.d(TAG, "getDeviceLocation: getting the devices current location");
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try {
if (mLocationPermissionsGranted) {
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful() && task.getResult() != null) {
Log.d(TAG, "onComplete: found location");
Location currentLocation = (Location) task.getResult();
moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),
DEFAULT_ZOOM);
} else {
Log.d(TAG, "onComplete: current location is null");
Toast.makeText(MapActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
Location currentLocation = (Location) task.getResult();
}
}
});
}
} catch (SecurityException e) {
}
}
以上代码是我认为用于查找位置然后将相机定位到给定位置的代码。我没有收到任何错误,当我将 LocationButton 设置为 true,然后单击它时,它确实以我的位置为中心。
将该代码更改为
private void getDeviceLocation(){
try{
if(mLocationPermissionGranted){
Task locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful() && task.getResult() != null){
Log.d(TAG, "onComplete: location found!");
Location currentLocation = (Location) task.getResult();
float currentZoom = mMap.getCameraPosition().zoom;//edit (added line)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(),
currentLocation.getLongitude()), DEFAULT_ZOOM));//edit (changed line)
}else{
Log.d(TAG, "onComplete: current location is null");
Toast.makeText(MapActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
}
}
});
}
}catch (SecurityException e){
}
}
我设法得到了一个 return 和一个位置,在我手动设置设备的位置之后,因为出于某种原因无法立即确定位置。 现在问题已经解决了!
试过你的代码。 它对我不起作用,但我解决了问题。 这是正确的代码。
private void getDeviceLocation(){
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try{
if(mLocationPermissionGranted){
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful() && task.getResult() != null){
Toast.makeText(DriverMapActivity.this, "Task is successful", Toast.LENGTH_SHORT).show();
Location currentLocation = (Location) task.getResult();
mLatLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
moveCamera(mLatLng, DEFAULT_ZOOM);
}else{
Toast.makeText(DriverMapActivity.this, "Task is unsuccessful", Toast.LENGTH_SHORT).show();
}
}
});
}
}catch (SecurityException e){
Toast.makeText(this, "SecurityException Found", Toast.LENGTH_SHORT).show();
}
}
因此,如果您的应用程序崩溃,您可能想试试这个。