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

1

The question is as follows ...

I checked one for a few days before coming here, but I just think how to manually add the bookmark to 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 bookmark to my current position, and if I change places and click that button again, add another bookmark and so on.

    
asked by anonymous 02.02.2016 / 22:55

1 answer

3

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

We recommend using FusedLocationProviderApi , strong> Google Play Service . So the first step is to add it to your build.gradle file, if you do not already have it in your project dependencies:

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

In your Activity , we need to start GoogleApiClient and get "listening" for 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 in 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 you request to update the user coordinates. Here are the methods that need to be implemented according to the interfaces that we have included in the Activity signature:

@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 user latitude and longitude is obtained, you can save it to use when the Add Bookmark button is triggered.

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

This method triggers when you end Activity , to terminate 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, while your 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 pressed, calling requestLocationUpdates and stopping shortly when it is obtained.

Just set which of the two option fits best to implement in the best way. A more complete example you can see Google here in>.

    
03.02.2016 / 12:32