更新位置时位置标记消失 Android Studio
Marker for location disappears when updating the location Android Studio
在我的应用程序中,我有一个以编程方式获取位置的按钮。一切顺利,直到我意识到一旦我添加了位置标记,每次我请求位置时都会添加一个新标记。因此,我添加了一个 if 语句,在我看来它是合乎逻辑的,但并不像我希望的那样工作。当我第一次到达位置时,一切都很好,并且放置了一个标记。当我第二次按下位置按钮时,标记被删除并且没有添加新标记。该应用程序在我的 phone 上编译没有问题。我究竟做错了什么?
public void showCurrentLocation(MenuItem item) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location currentLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (currentLocation == null){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_gps_view, null);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else {
LatLng latLng = new LatLng(
currentLocation.getLatitude(),
currentLocation.getLongitude()
);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 16);
mMap.animateCamera(update);
if (myPosition != null){
myPosition.remove();
} else {
myPosition = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_icon))
.position(latLng)
.title("I'm here!"));
}
}
}
当 Marker
已经存在但您没有再次添加时,您正在从地图中删除 Marker
。
请注意,当您从地图上删除标记时,它不是 null
,它的状态是未定义的。
更改您的 if
以在 Marker
存在时更新您的 Marker
的位置:
if (myPosition != null){
myPosition.setPosition(latLng);
} else {
myPosition = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_icon))
.position(latLng)
.title("I'm here!"));
}
在我的应用程序中,我有一个以编程方式获取位置的按钮。一切顺利,直到我意识到一旦我添加了位置标记,每次我请求位置时都会添加一个新标记。因此,我添加了一个 if 语句,在我看来它是合乎逻辑的,但并不像我希望的那样工作。当我第一次到达位置时,一切都很好,并且放置了一个标记。当我第二次按下位置按钮时,标记被删除并且没有添加新标记。该应用程序在我的 phone 上编译没有问题。我究竟做错了什么?
public void showCurrentLocation(MenuItem item) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location currentLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (currentLocation == null){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_gps_view, null);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else {
LatLng latLng = new LatLng(
currentLocation.getLatitude(),
currentLocation.getLongitude()
);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 16);
mMap.animateCamera(update);
if (myPosition != null){
myPosition.remove();
} else {
myPosition = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_icon))
.position(latLng)
.title("I'm here!"));
}
}
}
当 Marker
已经存在但您没有再次添加时,您正在从地图中删除 Marker
。
请注意,当您从地图上删除标记时,它不是 null
,它的状态是未定义的。
更改您的 if
以在 Marker
存在时更新您的 Marker
的位置:
if (myPosition != null){
myPosition.setPosition(latLng);
} else {
myPosition = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_icon))
.position(latLng)
.title("I'm here!"));
}