0

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.

Ryan Scollard
  • 45
  • 1
  • 4
  • 11
  • What do you want? That it should change marker according to previous activity, ryt? You could, while calling this MapsActivity , simply put longitude and latitude extras . And get them in onCreate. And add that as marker. – D Agrawal Apr 18 '16 at 13:02
  • I want to be able to click a button within an activity which will open up Google Maps all depending on what activity the button you just clicked is in. Therefore, activity1 should bring you to Google Maps where activity 1 is marked. Not sure what you mean by getting them in the onCreate and adding them as markers from there... – Ryan Scollard Apr 18 '16 at 13:08
  • Click the answer that i have posted – D Agrawal Apr 18 '16 at 13:34
  • I suspect the issue is that onMapReady() is only called in the first of your activities because it is retained. You can check this by just printing to the log and see(see if the message you put in is printed each time you click on your button). Do you get the second location if that is the first one you click on? – nPn Apr 18 '16 at 13:41

3 Answers3

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)));
}
Ramkumar.M
  • 681
  • 6
  • 21
0

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(...)
 }
Jhaman Das
  • 1,094
  • 13
  • 28
  • Yes and I am not entirely sure what I am doing wrong but it is giving me errors. Are you able to provide an example for your code, to match in with my code provided in the original question? It's giving me errors such as unknown class for locations and Invalid method declaration for the LatLng. It may be something simple but I am not quite sure where I'm going wrong. Thanks – Ryan Scollard Apr 22 '16 at 12:40
0

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

    }
D Agrawal
  • 471
  • 4
  • 15
  • This still does the same thing where whatever location is declared first in the MapsActivity, the marker is on that for all activities – Ryan Scollard Apr 21 '16 at 16:44