我尝试制作适用于Google地图的应用。在我的地图中我有当前标记和标记选项,当我移动当前标记(蓝点)进行移动但标记选项(红色标记,带有CurrenrtLocation标记)没有使用当前标记移动( Blue Point).how我可以使用googlemap.Thanks中的当前标记移动标记选项
This picture shows when i move current marker move but markeroption didnt move
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapbutton);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location)
{
if (location != null) {
// ---Get current location latitude, longitude---
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Marker currentLocationMarker = mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
// Move the camera instantly to hamburg with a zoom of 15.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15));
// Zoom in, animating the camera.
if (!zoomed) {
mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
zoomed = true;
}
if (!firstPass){
currentLocationMarker.remove();
}
firstPass = false;
Toast.makeText(this,"Latitude = "+
location.getLatitude() + "" +"Longitude = "+ location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission(){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Asking user if explanation is needed
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission was granted.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
}
} else {
// Permission denied, Disable the functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other permissions this app might request.
//You can add here other case statements according to your requirement.
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}}
答案 0 :(得分:0)
使currentLocationMarker成为一个类属性,实际上你只是在添加它之后将其删除。
class YourActivity extends Activity implements onMapReady {
private Marker currentLocationMarker;
@Override
public void onLocationChanged(Location location){
if ( location != null ){
if ( currentLocationMarker == null ){
currentLocationMarker = mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
}else{
currentLocationMarker.setPosition(new LatLng(location.getLatitude(), location.getLongitude()));
}
}
}
}
<强>说明强>
在您的代码中,您在第一次更改位置时添加标记,然后每次更改位置时添加新标记并立即将其删除:
Marker currentLocationMarker = mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
//...
if (!firstPass){
currentLocationMarker.remove();
}
最好的办法是保持对你创建的标记的引用,而不是删除它创建一个新标记,只需更新它的位置,即我在代码中执行的操作,如果标记不存在(currentLocationMarker == null)然后创建它并在类中保持引用。如果存在标记(else),则只需更新其位置。