Pass Coordinates from Activity to MapsFragment

0

I need to pass the latitude and longitude that I get in HomeActivity to MapsFragment , for the map to open soon in the user's position.

I've tried via Intent and it does not 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

    
asked by anonymous 21.09.2016 / 01:18

1 answer

0

Why not get the location in MapsFragment ?

Pass this code

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

into the onMapReady() method:

@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 will need to construct the object mGoogleApiClient
  • You will have to deal with run-time permissions if targetApi is 23 or higher.

However the easiest way to get the location of the device marked on the map is to use the setMyLocationEnabled() method.

@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 targetApi is 23 or higher.

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

21.09.2016 / 21:46