0

For several days I try to modify the program so that each time you press the button addMarkerButton program put the Marker in my new location. When I turn on the application at once to display me my location without any buttons at all times navigate where currently I am, and when you press the addMarkerButton program put a marker in my new location, when I go to another place, and I press addMarkerButton wants a second marker at a second location but that one did not disappear. Is it possible? I've tried a lot of combinations, looking for help on the internet and nothing.

My button looks like this (currently only for 2 markers):

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);

In his application I use:

public class MapsActivity extends FragmentActivity implements
        OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

so I have:

@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());
    }
}

The method onMapReady() I set 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;
    }    
}

method of downloading location:

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));
}

and the addition in the method 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);
reflective_mind
  • 1,475
  • 3
  • 15
  • 28
  • Have a look at [this previous discussion](http://stackoverflow.com/questions/13692398/remove-a-marker-from-a-googlemap) about removing markers. Looks like you are storing the markers in the `tab` array so you could use that to call the `remove()` method for the old marker when adding a new one. – Markus Kauppinen Oct 03 '16 at 07:48
  • but i dont want to remove my last marker, i want a lot of marker on my map, and i want make 2 button, hide all and unhide all marker – monik19511 Oct 03 '16 at 10:11
  • Then you could use the marker's [setVisible()](https://developers.google.com/android/reference/com/google/android/gms/maps/model/Marker#setVisible%28boolean%29) method, which was also mentioned in that discussion. – Markus Kauppinen Oct 03 '16 at 11:07
  • Yes, i know about this method, but it's not my problem. Problem is, my button work only for first location/marker :( when i click in new position, second marker does not appear :( – monik19511 Oct 03 '16 at 11:20
  • Can you also share your code wherein you used `setVisible()` method to customize your markers? What error/s have you encountered when using it? – Teyam Oct 06 '16 at 15:17
  • i dont have code when i use setVisible() – monik19511 Oct 06 '16 at 16:40
  • Based from [documentation](https://developers.google.com/maps/documentation/android-api/marker#customize_a_marker), set `visible` to false to make the marker invisible. Defaults to true. – Teyam Oct 06 '16 at 17:00

0 Answers0