Pass Activity coordinates to Mapsfragment

Asked

Viewed 110 times

0

I need to pass the latitude and longitude that I pick up at the HomeActivity to the MapsFragment, for the map to open soon at user’s position.

I tried to see Intent and it doesn’t work.

My code:

Homeactivity

public class HomeActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
    private Button botaoPostar;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        botaoPostar = (Button) findViewById(R.id.botaoPostarId);

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.container, new MapsFragment(), "MapsFragment");
        transaction.commitAllowingStateLoss();

        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location != null) {
            lat = location.getLatitude();
            longi = location.getLongitude();
        } 
    }
}

Mapsfragment

public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback {

    private GoogleMap mMap;
    double lat;
    double longi;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        //Pegando a latitude e longitude da HomeActivity
        LatLng minhaLocalizacao = new LatLng(lat, longi);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(minhaLocalizacao, 3));
    }
}

Would it be possible?

Thank you

1 answer

0

Why not get the location on Mapsfragment?

Pass that code

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location != null) {
    lat = location.getLatitude();
    longi = location.getLongitude();
} 

into the method onMapReady():

@Override
public void onMapReady(GoogleMap map) {

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location != null) {
        lat = location.getLatitude();
        longi = location.getLongitude();
    } 

    LatLng minhaLocalizacao = new LatLng(lat, longi);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(minhaLocalizacao, 3));
}

Notes:

  • You must build the object mGoogleApiClient
  • You will have to deal with runtime permissions if the targetApi is 23 or more.

However the simplest way to have the device location marked on the map is to use the method setMyLocationEnabled().

@Override
public void onMapReady(GoogleMap map) {

    map.setMyLocationEnabled(true);

    //Apenas para fazer zoom - ver nota
    Location location = map.getMyLocation();

    if (location != null) {
        lat = location.getLatitude();
        longi = location.getLongitude();
    } 
    LatLng minhaLocalizacao = new LatLng(lat, longi);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(minhaLocalizacao, 3));
}

Note:

  • You will have to deal with runtime permissions if the targetApi is 23 or more.

  • The method getMyLocation() is considered obsolete, it is used here because it is intended that the camera zoom at those coordinates. A documentation suggests that the zoom must be done by the user using the button at the top corner of the map.

  • when implementing mGoogleApiClient of the error in "this" """mGoogleApiClient = new Googleapiclient.Builder(this)""" of the error in this this, I believe I don’t want to use this in a Fragment, what I do?

  • Instead of this use getActivity()

Browser other questions tagged

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