Ryan Scollard
Ryan Scollard

Reputation: 45

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 Google Maps with a marker at where location1 is. Alternatively, if the user is in the location2 activity and clicks the button to open maps, it should open Google Maps with a marker at where location 2 is.

I have it working when they click on one activity, it brings them to Google Maps and has a marker at where that location is. I have simply tried to copy the code and paste it below with the edited names in it, but if I click a different activity to go to maps, it brings me to the same marker as previously.

My Code for GoogleMapsActivity is below:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@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);
}

@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(53.3484013, -6.2605243);
    mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));

    /*
    // Add a marker at Diceys and move the camera
    LatLng diceys = new LatLng(53.3358088,-6.2636688);
    mMap.addMarker(new MarkerOptions().position(diceys).title("Diceys Nightclub"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(diceys));
    */
}

public void changeType(View view)
{
    if(mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL)
    {
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }
    else
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}}

As you can see, I have commented out the code where I have tried adding a marker for a different location, but it seems to bring me to the same location as above.

Not sure if this is something simple or not as I am new to Google Maps in Android.

Any help would be greatly appreciated.

Thank you.

Upvotes: 0

Views: 8072

Answers (3)

D Agrawal
D Agrawal

Reputation: 471

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));

    }

Upvotes: 0

Ramkumar.M
Ramkumar.M

Reputation: 691

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)));
}

Upvotes: 1

Jhaman Das
Jhaman Das

Reputation: 1114

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(...)
 }

Upvotes: 0

Related Questions