有几天我尝试修改程序,这样每次按下按钮addMarkerButton
时,将Marker放在我的新位置。
当我立即打开应用程序以显示我的位置时,没有任何按钮始终导航我当前的位置,当您按下addMarkerButton
程序时,在我的新位置放置标记,当我去其他地方时,我按addMarkerButton
想要第二个位置的第二个标记,但那个没有消失。
可能吗?我已经尝试了很多组合,在互联网上寻求帮助,什么都没有。
我的按钮看起来像这样(目前只有2个标记):
addMarkerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
if (tab[0] == null) {
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am");
tab[0] = mMap.addMarker(options);
}
else if (tab [0] != null) {
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am");
tab[1] = mMap.addMarker(options);
在他的申请中我使用:
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
所以我有:
@Override
public void onConnected(Bundle bundle) {
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) {
return;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);
}
else {
handleNewLocation(location) ;
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
方法onMapReady()
我设置了mGoogleApiClient.connect()
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mGoogleApiClient.connect();
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) {
return;
}
}
下载位置的方法:
public void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}
以及方法onCreate()
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000)
.setFastestInterval(1 * 1000);