How to create a button in the google maps API to add a bookmark?

Asked

Viewed 1,066 times

1

The doubt is as follows...

I searched for a few days before coming here, but I just think how to add the marker in a manual way like the google example:

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
Map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));   

Map.moveCamera(CameraUpdateFactory.newLatLng(Sydney));

but I would like to create a button in the maps API, which when clicked adds the marker in my current position, and if I change places and click again on that button adds another marker and so on.

1 answer

3

Basically, what you want then is how to get your current location and keep it always updates, since the way to add the marker you already have, right?

The recommendation is to use the FusedLocationProviderApi, of Google Play Service. Then the first step is to add it to your file build.gradle, if you do not already have in the dependencies of your project:

compile 'com.google.android.gms:play-services:8.4.0'

In his Activity, we need to start the GoogleApiClient and stay "listening" user location updates. First, implement the following:

public class MapaActivity extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

Declare the object in the properties of your Activity:

protected GoogleApiClient mGoogleApiClient;

And this method below you can start it on onCreate:

private void startTracking() {
    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting())
            mGoogleApiClient.connect();
    } else {
        Log.e(TAG, "Unable to connect to Google Play Services.");
    }
}

Here you are starting the Googleapiclient, and with success, then yes you request the updating of user coordinates. Below are the methods that need to be implemented according to the interfaces we have included in the Activity:

@Override
public void onConnected(Bundle bundle) {
    LocationRequest mLocationRequest = LocationRequest.create();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    stopLocationUpdates();
}

Here when the user’s latitude and longitude is obtained, you can save it to use when the add marker button is triggered.

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        // Localização atual obtida
    }
}

This method triggers when you finish the Activity, to end the location update process and disconnect the client.

private void stopLocationUpdates() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}

In this implementation, as its Activity is active, the location will always be requested according to the defined interval. Another option is to only request the current location when the button is triggered by calling the requestLocationUpdates and stop immediately when obtained.

Just define which of the two fits best to implement in the best way. A more complete example you can see here, of own Google.

  • I think my question was not very clear, what I doubt is how to create a button that every time is clicked add a marker in its current position

  • It was clear, yes, but it involves this whole process of searching for the location, which as you didn’t mention, I imagine is also part of your doubt. Anyway, the button itself, is nothing more than implementing the method setOnClickListener and thus execute the start process. Or through the android:onClick of your layout. What else?

  • Well, I made the button that way public void adicionarMarker(View view) {

 mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

 @Override
 public void onMyLocationChange(Location location) {
 LatLng posicao = new LatLng(location.getLatitude(), location.getLongitude());
 MarkerOptions options = new MarkerOptions();
 options.position(posicao);
 marker = mMap.addMarker(options);
 }
 });
 } however it is not terminating, create a marker without me clicking on it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.