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