I have an android application in which I have 3 buttons and by pressing them I want to call three different Google maps markers, by using the same google maps activity.
CLASS WITH 3 DIFFERENT BUTTONS
package com.postgre.my_tourist_guide; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.google.android.gms.maps.GoogleMap; public class Sightseeings extends AppCompatActivity { TextView textView7; Button button7; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sightseeings); textView7 = findViewById(R.id.textView7); Intent intent = getIntent(); String name = intent.getStringExtra("name"); textView7.setText(String.valueOf(name)); } public void openMapTrain(View view){ Intent intent = new Intent(this,MapsActivityTrain.class); intent.putExtra("map1","map1"); startActivity(intent); } public void openMap2(View view){ Intent intent = new Intent(this,MapsActivityTrain.class); intent.putExtra("map2","map2"); startActivity(intent); } public void openMap3(View view){ Intent intent = new Intent(this,MapsActivityTrain.class); intent.putExtra("map3","map3"); startActivity(intent); } }
and then my GOOGLE MAPS ACTIVITY CODE
package com.postgre.my_tourist_guide; import androidx.fragment.app.FragmentActivity; import android.content.Intent; import android.os.Bundle; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import com.postgre.my_tourist_guide.databinding.ActivityMapsTrainBinding; public class MapsActivityTrain extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private ActivityMapsTrainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMapsTrainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // 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); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; Intent intent = getIntent(); String name = intent.getStringExtra("map1"); intent.getStringExtra("map2"); intent.getStringExtra("map3"); if (name == "map1"){ } LatLng lexainia = new LatLng(39.3254942, 23.053191); mMap.addMarker(new MarkerOptions().position(lexainia).title("Marker in Lexainia Train Station")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(lexainia, 18f)); LatLng map2 = new LatLng(37.9577198, 23.6526767); mMap.addMarker(new MarkerOptions().position(map2).title("Marker in map2")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(map2, 18f)); LatLng map3 = new LatLng(37.9577198, 23.6526767); mMap.addMarker(new MarkerOptions().position(map3).title("Marker in map3")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(map3, 18f)); } }
This is what I’ve done so far, but I can’t find a way to use the same onMapReady
method, to show the three different markers, when I am pressing the three buttons. It’s obvious that I have to pass something using the intent class, but I don’t know what.
Any help is appreciated!!! Thank you.
Advertisement
Answer
I think this can help you
@Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; Intent intent = getIntent(); String nameofmap1 = intent.getStringExtra("map1"); String nameofmap2 = intent.getStringExtra("map2"); String nameofmap3 = intent.getStringExtra("map3"); if (nameofmap1 != null){ LatLng lexainia = new LatLng(39.3254942, 23.053191); mMap.addMarker(new MarkerOptions().position(lexainia).title("Marker in Lexainia Train Station")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(lexainia, 18f)); } else if (nameofmap2 != null) { LatLng map2 = new LatLng(37.9577198, 23.6526767); mMap.addMarker(new MarkerOptions().position(map2).title("Marker in map2")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(map2, 18f)); } else if (nameofmap3 != null) { LatLng map3 = new LatLng(37.9577198, 23.6526767); mMap.addMarker(new MarkerOptions().position(map3).title("Marker in map3")); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(map3, 18f)); } }