Adding Multiple Markers in Google Maps API V2 Android

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

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.

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

Show multiple marker on google map (API V2 ) from database

In map version 2 for google map to show multiple marker you just have to add

mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(lat, longitude)).title("Pin title"));

in for loop for different lat/long values. where mGoogleMap is the object of GoogleMap in V2

Please check that your are doing as accordingly or not.

Adding multiple markers to google maps API from volley repose in android Java

After numerous searches and following some of the helpful advise in the comments, I solved the problem and here is the code that worked just incase someone has the same problem in future.
Once the map is ready, retrieve the serve information using volley, then iterate through the response array and add markers to the markersoption arraylist

public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
banPoint =BitmapDescriptorFactory.fromResource(R.drawable.pincode);

final String TAG = ItemisedMapsActivity.class.getSimpleName();

StringRequest strReq = new StringRequest(Request.Method.POST, Link.URL_PROVIDER, response -> {
try {
JSONObject jsonObject;
JSONObject drinkObject = new JSONObject(response);
JSONArray jsonArray = drinkObject.getJSONArray("vendors");
for(int i=0; i<jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
LatLng vnr = new LatLng(jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude"));
MarkerOptions vnrMarker = new MarkerOptions();
vnrMarker.position(vnr);
vnrMarker.title(jsonObject.getString("name"));
vnrMarker.snippet("Mama fua service provider");
vnrMarker.icon(banPoint);
markerList.add(vnrMarker);
}
showAllMarkers(mMap);
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
Log.e(TAG, "Request Error: " + error.getMessage());
Toast.makeText(this, " An error has occurred "+error.getMessage(), Toast.LENGTH_LONG).show();
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<>();
params.put("town", town);
return params;
}
};
// Adding request to request queue
RequestQueue providerRequestQue = Volley.newRequestQueue(this);
providerRequestQue.add(strReq);
}

Then create a method to use to calculate the position of all the markers and zoom to their points showing all of them in the map

      private void showAllMarkers(GoogleMap mMap) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (MarkerOptions m : markerList) {
builder.include(m.getPosition());
// add the parkers to the map
mMap.addMarker(m);
}
LatLngBounds bounds = builder.build();
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.30);
// Zoom and animate the google map to show all markers
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
mMap.animateCamera(cu);
}


Related Topics



Leave a reply



Submit