How to show multiple markers on MapFragment in Google Map API v2?
I do it like this to show car positions on the map with markers of different colors:
private void addMarkersToMap() {
mMap.clear();
for (int i = 0; i < Cars.size(); i++) {
LatLng ll = new LatLng(Cars.get(i).getPos().getLat(), Cars.get(i).getPos().getLon());
BitmapDescriptor bitmapMarker;
switch (Cars.get(i).getState()) {
case 0:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
Log.i(TAG, "RED");
break;
case 1:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);
Log.i(TAG, "GREEN");
break;
case 2:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE);
Log.i(TAG, "ORANGE");
break;
default:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
Log.i(TAG, "DEFAULT");
break;
}
mMarkers.add(mMap.addMarker(new MarkerOptions().position(ll).title(Cars.get(i).getName())
.snippet(getStateString(Cars.get(i).getState())).icon(bitmapMarker)));
Log.i(TAG,"Car number "+i+" was added " +mMarkers.get(mMarkers.size()-1).getId());
}
}
}
Cars is an ArrayList
of custom objects and mMarkers is an ArrayList
of markers.
Note : You can show map in fragment like this:
private GoogleMap mMap;
....
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
// Hide the zoom controls as the button panel will cover it.
mMap.getUiSettings().setZoomControlsEnabled(false);
// Add lots of markers to the map.
addMarkersToMap();
// Setting an info window adapter allows us to change the both the
// contents and look of the
// info window.
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
// Set listeners for marker events. See the bottom of this class for
// their behavior.
mMap.setOnMarkerClickListener(this);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMarkerDragListener(this);
// Pan to see all markers in view.
// Cannot zoom to bounds until the map has a size.
final View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
// We check which build version we are using.
@Override
public void onGlobalLayout() {
LatLngBounds.Builder bld = new LatLngBounds.Builder();
for (int i = 0; i < mAvailableCars.size(); i++) {
LatLng ll = new LatLng(Cars.get(i).getPos().getLat(), Cars.get(i).getPos().getLon());
bld.include(ll);
}
LatLngBounds bounds = bld.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 70));
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
}
And just call setUpMapIfNeeded()
in onCreate()
Android - google map - how show multiple markers on the map?
You are initializing your markerPoints
on each iteration.
Remove markerPoints = new ArrayList<LatLng>();
from the do-while loop and initialize it on the declaration ArrayList<LatLng> markerPoints = new ArrayList<LatLng>();
Multiple markers with text on Android Google Maps API v2
to do that you'll have to customize the icon of each individual marker to be what you need.
Here's the link: https://developers.google.com/maps/documentation/android/marker#customize_a_marker
Then you can put some PNG resources (a blue, a yellow and a red), and at runtime get correct Bitmap, write the text on the bitmap by code, and then set as the custom-marker with the fromBitmap (Bitmap image)
method.
Adding multiple markers in Google Maps API v2 Android
ArrayList<MarkerData> markersArray = 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 Marker createMarker(double latitude, double longitude, String title, String snippet, int iconResID) {
return googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory.fromResource(iconResID)));
}
google maps v2 not showing multiple markers
You see 2 markers because as you can see in the logcat, after second marker, all of markers have same latitute and longitude.
Related Topics
Location Needs to Be Enabled for Bluetooth Low Energy Scanning on Android 6.0
How to Open a PDF via Intent from Sd Card
Trying to Create a File in Android: Open Failed: Erofs (Read-Only File System)
What Is the Purpose of Android's <Merge> Tag in Xml Layouts
Nullpointerexception When Trying to Access Views in a Kotlin Fragment
Movecamera with Cameraupdatefactory.Newlatlngbounds Crashes
Forcing the Soft Keyboard Open
How to Create Custom Baseadapter for Autocompletetextview
What Will Happen to the Sharedpreferences on Update an Android App
Need an Example About Recyclerview.Adapter.Notifyitemchanged(Int Position, Object Payload)
How to Solve Execution Failed for Task ':App:Compileflutterbuilddebug'
How to Determine the Screen Width in Terms of Dp or Dip at Runtime in Android
Implementing User Choice of Theme
Opening Android Settings Programmatically
Check If Correct Google Play Service Available: "Unfortunately Application Has Stopped Working"