Skip to content Skip to sidebar Skip to footer

Adding Multiple Markers Google Maps

I am trying to add a marker all depending on what activity the user is on. For example, if the user is in the location1 activity and clicks the button to open maps, it should open

Solution 1:

ArrayList<MarkerData> markerArray = new ArrayList<MarkerData>();
for(int i = 0 ; i < markersArray.size() ; i++ ) {

createMarker(markersArray.get(i).getLatitude(), markersArray.get(i).getLongitude(), markersArray.get(i).getTitle(), markersArray.get(i).getSnippet(), markersArray.get(i).getIconResID());
}
....
protected void createMarker(double lat, double lon, String title, String snippet, int iconResID) {
return googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(lat, lon))
        .anchor(0.5f, 0.5f)
        .title(title)
        .snippet(snippet);
        .icon(BitmapDescriptorFactory.fromResource(iconResID)));
}

Solution 2:

Tying it all together:

ArrayList<LatLng> locations = new ArrayList();
locations.add(new LatLng(30.243442, -1.432320));
locations.add(new LatLng(... , ...));
.
.
.

for(LatLng location : locations){
 mMap.addMarker(new MarkerOptions()
    .position(location)
    .title(...)
 }

Solution 3:

In First Location activity:

Intent i = new Intent(FirstLocationActivity.this, MapsActivity.class);   
  String keyLatitude  = ""; //enter the value
  String keyLongitude  = ""; //enter the value
  i.putExtra("latitude", keyLatitude );
  i.putExtra("longitude", keyLongitude );
 startActivity(i);

similarly do the same for activity two location, add its corresponding latitude and longitude in extra

In your mapsActivity,

 String latitude="";
    String longitude ="";

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);

        Bundle bundle = getIntent().getExtras();

       latitude = bundle.getString("latitude");
       longitude = bundle.getString("longitude");

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        //mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // Add a marker at the Oval and move the camera
        LatLng oval = new LatLng(latitude,longitude);
        mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));

    }

Post a Comment for "Adding Multiple Markers Google Maps"