Get number of markers shown in Maps Android - android

I am showing a list of n markers in a map, then I make zoom to my location and I would like to know if its possible to count just how many markers are displayed in my area.
Thanks.

You can call below method for each marker to see if they are inside the Maps visible area.
map.getBounds().contains(marker.getPosition())
If the getBounds method is not available try this:
VisibleRegion region = map.getProjection().getVisibleRegion();
LatLngBounds mapBound = region.latLngBounds;
int count 0;
for(Marker marker : makers) { // markers is the List of marker you have
if(mapBound.contains(marker.getPosition()){
count = count + 1;
}
}

You can keep a List of marker within your Activity or Fragment and then do a loop on it to see if its in the visible area List.size() to get the number of marker.
private List<Marker> mMarkerArray = new ArrayList<Marker>();
///to get number of marker
int marker_count = = 0;
for(int i =0;i<=mMarkerArray.size();i++){
if(mMap.latLngBounds.contains(new LatLng(mMarkerArray.get(i).getLocation().getLongitude(), mMarkerArray.get(i).getLocation().getLatitude())){
marker_count++;
}
}
just make sure you add the marker in the list after you add it to your map object

Related

Google map with 1000 marker

I am adding google map marker with 1000 marker in small boundery box, while moving map arround, app frezzing, didn't know why,
Also i have tried marker cluster . but client has refuesed it, he need all marker to be shown on map, as marker is mandetory to show to user...!
is there any way to do so with better preformance
i figure that may i zoom in to custom point and show marker only with boundry box map, and while user move map he remove marker around and put new marker
but this will come on looping in array with 1000 element in it, and this is n't better solution, any one know how to do so !!!?
for (int i = 0; i < stopDetailsModelResponses.size(); i++) {
mapHelper.setMarkerPinWithLayoutToImage(
stopDetailsModelResponses.get(i).getStopDetailsLat(),
stopDetailsModelResponses.get(i).getStopDetailsLong(),
stopDetailsModelResponses.get(i).getStopDetailsName(),
String.valueOf(stopDetailsModelResponses.get(i).getStopDetailsId()),
stopDetailsModelResponses.get(i).getStopDetailsLogoUrl(),
stopDetailsModelResponses.get(i).getStopDetailsType(),
markerOptions, googleMap, getContext()
);
}
googleMap.setOnMarkerClickListener(onMarkerClickListener);
this for looping inside list to set element to map marker
and this for adding marker to map
try {
markerOptions = new MarkerOptions()
.position(new LatLng(lat, longitude))
.title(locationName)
.draggable(false);
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(defineLayoutOfPin(context, imageType, locationId)));
googleMap.addMarker(markerOptions);
} catch (Exception e) {
Log.e("setMarkerWithImage: ", e.getMessage());
}

Find Marker by Tag Google Maps API (Android)

How/Where could I get a reference of all marker objects current on the map, in order to check something like this:
if (Markers.getTag().equals("something"))
By reading the documentation on Marker it said "This is easier than storing a separate Map", so I don't want to use HashMap unless someone say I absolutely have to.
Thanks, the following is a pseudo-pseudo code
// The uid is the Marker's tag
// 1) Someway to Check if the tag exists for the current profile
// 2) If it exists, then just move the marker, just set a new position of this marker.
// 3) If it doesn't, then create a new marker, add a marker.
// 4) Set profile uid as the Marker's Tag, via .setTag()
// 5) Animate move camera to the latlng position
3-5 is okay, just 1-2
// 3) Create a new marker
// Marker to show on the map
Marker friendMarker;
// Add a marker when the image is loaded
friendMarker = googleMap.addMarker(new MarkerOptions()
.position(friendLatLng)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.title(friendProfile.getName()));
// Set the tag on this friend marker, so we can retrieve or update it later
friendMarker.setTag(friendProfile.getUid());
// 5) Animate the camera to that location
CameraPosition cameraPosition = new CameraPosition.Builder().target(friendLatLng).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Create a List of Marker
List<Marker> markers = new ArrayList<>();
Then add your marker in your markers list
// Marker to show on the map
Marker friendMarker;
// Add a marker when the image is loaded
friendMarker = googleMap.addMarker(new MarkerOptions()
.position(friendLatLng)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.title(friendProfile.getName()));
//Add now the marker in markers list
markers.add(friendMarker);
Then to access all markers
for (Marker marker : markers) {
if (marker.getTag().equals("something")) { //if a marker has desired tag
//Do something in the way. Hmmmm. Yeah
}
}

Getting Number of Markers of visible region of google maps android

How can we calculate the number of those markers shown on the visible region only on google maps android?
var markers; //your markers
var map; //your map
var countInBoundingBox = 0; //the counter for the markers in bounding box
for (var i = markers.length, bounds = map.getBounds(); i--;) {
if (bounds.contains(markers[i].getPosition())) {
countInBoundingBox++;
}
}
Next time try to provide a Minimal, Complete, and Verifiable example. :-)

Center android google map on group of markers?

I have a bunch of markers in which I load onto a google map. My problem is that, the camera is not ontop of the markers. Can I center it on top of all my markers?
I am adding my markers with this code:
for(int i = 0; i < jsonArray.length(); i++) {
String breweryName = jsonArray.getJSONObject(i).getString("brewery");
String lat = jsonArray.getJSONObject(i).getString("lat");
String lng = jsonArray.getJSONObject(i).getString("lng");
String bID = jsonArray.getJSONObject(i).getString("breweryID");
double latD = Double.parseDouble(lat);
double lngD = Double.parseDouble(lng);
m.addMarker(new MarkerOptions()
.position(new LatLng(latD, lngD))
.title(breweryName));
}
I looked on stack and found this question, which is the same as mine:
Android Google maps API V2 center markers
But there isnt much context behind it and am not sure how to implement it.
You can animate your camera within specific bounds. Let's say you have an ArrayList of markers, something like (this could also be a list of LatLngs, doubles, anything that contains the latitude/longitude for your marker)
List<MarkerOptions> markers = new ArrayList<MarkerOptions>();
You can then make sure they are all visible on your map by doing the following
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLng position;
for(int i = 0; i < markers.size(); i++){
position = markers.get(i).getPosition();
builder.include(new LatLng(position.latitude, position.longitude));
}
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));

Android set GoolgeMap bounds from database of points

Using Google Maps Android API v2, I am trying to set the bounds of the map using LatLngBounds.Builder() with points from a database. I think I am close, however the activity is crashing because I don't think I am properly loading the points. I may be just a couple of lines away.
//setup map
private void setUpMap() {
//get all cars from the datbase with getter method
List<Car> K = db.getAllCars();
//loop through cars in the database
for (Car cn : K) {
//add a map marker for each car, with description as the title using getter methods
mapView.addMarker(new MarkerOptions().position(new LatLng(cn.getLatitude(), cn.getLongitude())).title(cn.getDescription()));
//use .include to put add each point to be included in the bounds
bounds = new LatLngBounds.Builder().include(new LatLng(cn.getLatitude(), cn.getLongitude())).build();
//set bounds with all the map points
mapView.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
}
}
I think there may be an error in how I arranged the for loop to get all the cars, if I remove the bounds statements the map point are plotted correctly as I expected but not bounding the map properly.
you are creating a new LatLngBounds.Builder() everytime in the loop.
try this
private LatLngBounds.Builder bounds;
//setup map
private void setUpMap() {
bounds = new LatLngBounds.Builder();
//get all cars from the datbase with getter method
List<Car> K = db.getAllCars();
//loop through cars in the database
for (Car cn : K) {
//add a map marker for each car, with description as the title using getter methods
mapView.addMarker(new MarkerOptions().position(new LatLng(cn.getLatitude(), cn.getLongitude())).title(cn.getDescription()));
//use .include to put add each point to be included in the bounds
bounds.include(new LatLng(cn.getLatitude(), cn.getLongitude()));
}
//set bounds with all the map points
mapView.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 50));
}

Categories

Resources