I'm doing a test exercise, where when I press a button of the first activity I send it to MapsActivity and in that Activity (MapsActivity) I recognize the ID of the button pressed and it shows me specific coordinates, and if I go back and press the second button, I showed other coordinates in the same MapsActivity, I was trying some options and I could not solve it. This is the code that I have been driving.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private Button button2;
private final int PETICION_ACTIVITY_SEGUNDA = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
Intent intent= new Intent (MainActivity.this, MapsActivity.class);
startActivity(intent);
break;
case R.id.button2:
Intent intent1= new Intent (MainActivity.this, MapsActivity.class);
startActivity(intent1);
break;
}
}
}
MapsActivity:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener {
private GoogleMap mMap;
public Button button1;
public Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
button1=(Button)findViewById(R.id.button);
button2=(Button)findViewById(R.id.button2);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED) {
return;
}
// Add a marker in Sydney and move the camera
mMap.setMyLocationEnabled(true);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
LatLng sydney = new LatLng(4.657923, -74.059470);
LatLng sydney1 = new LatLng(4.657998, -74.059429);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 18));
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)).position(sydney));
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)).position(sydney1));
break;
case R.id.button2:
LatLng sydney2 = new LatLng(4.658035, -74.059365);
LatLng sydney3 = new LatLng(4.658078, -74.059301);
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)).position(sydney2));
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)).position(sydney3));
break;
}
}
}