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
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?
– Elvis Roberto
Instead of
this
usegetActivity()
– ramaral