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());
}
....
protectedvoidcreateMarker(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:
Intenti=newIntent(FirstLocationActivity.this, MapsActivity.class);
StringkeyLatitude=""; //enter the valueStringkeyLongitude=""; //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 ="";
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
Bundle bundle = getIntent().getExtras();
latitude = bundle.getString("latitude");
longitude = bundle.getString("longitude");
}
@OverridepublicvoidonMapReady(GoogleMap googleMap) {
mMap = googleMap;
//mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();// Add a marker at the Oval and move the cameraLatLng oval = newLatLng(latitude,longitude);
mMap.addMarker(newMarkerOptions().position(oval).title("Oval Pub"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));
}
Post a Comment for "Adding Multiple Markers Google Maps"