Android Google Maps Utils Cluster Manager marker above clusters - android

I'm using Google Maps Utils library for clustering markers.
I'm adding most of markers to cluster manager. The rest I'm adding to map (I don't want them to be clustered with others). My problem is that clusters are drawn above standard markers and cover them. Is there any way to change drawing order i.e. first clusters and markers above them?
Here's part of my code:
clusterManager = new ClusterManager<>(getActivity(), map);
clusterManager.setRenderer(new MyObjectRenderer(getActivity(), map, clusterManager));
clusterManager.setOnClusterItemClickListener(this);
map.setOnMarkerClickListener(clusterManager);
for (MyObject object : list) {
clusterManager.addItem(object);
}
clusterManager.cluster();
for (MyOtherObject object : otherList) {
map.addMarker(new MarkerOptions().position(object.getLatLng()).title(object.getName()));
}

I had a similar scenario: I was adding several locations to my ClusterManager and I needed a marker with the user position on top of all the other markers.
This worked for me:
Add user marker to map with zIndex to 1000 to ensure that it will be placed on top.
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(userLocation.getLatitude(), userLocation.getLongitude())).zIndex(1000);
map.addMarker(markerOptions);
Add markers to cluster manager.
for (MyObject object : objects) {
mClusterManager.addItem(object);
}
mClusterManager.cluster();

Related

how to create a markers zoom animation when is created?

I would like create effect described in the title on my markers when they are added. I see for ios exists method marker.appearAnimation but nothing similar for android. Now I will just add a BitmapDescription to MarkerOptions
final Marker marker = mMap.addMarker(new MarkerOptions()
.position(pos)
.icon(getIconForCluster(new BitmapDescriptor(getObjectWrapped()))
);
any suggestions?
You can use the animateCamera() method from GoogleMaps object.
Example:
mGoogleMap.setOnMapLoadedCallback(()
-> mGoogleMap.animateCamera(CameraUpdateFactory
.newLatLngZoom(mMapBoundary.getCenter(), 17)));
with mMapBoundary is a LatLngBounds object. you can set the markers position here, and 17 is the zoom level.

Reveal markers around a selected location on Google Maps

I am currently coding on Android Studio for Google Maps.
I made the search function working based on the text searched on the search bar. And once the user presses "Search" the map will zoom into that selected location and place a coloured marker.
However, I would like to do it like this:
Once you select the location, you will reveal several markers based on a certain radius/ certain area.
You can add the markers to your map as invisible and compute the distance between your desired location and each marker to show those nearer than a given distance.
private List<Marker> markers = new ArrayList<>(); // List to hold your markers
Add the markers to the map and to the list:
Marker marker = mMap.addMarker(new MarkerOptions().position(yourPosition).visible(false));
markers.add(marker);
And then create a function that, given a LatLng computes the distance to each marker (I'm using SphericalUtil.computeDistanceBetween from the Google Maps Android API Utility Library) and shows the desired markers:
private void showMarkers(LatLng location, float distance) {
for(Marker marker : markers) {
if (SphericalUtil.computeDistanceBetween(marker.getPosition(), location) <= distance) {
marker.setVisible(true);
} else {
marker.setVisible(false);
}
}
}

Android Google Map multiple markers on same location

I have a simple app with Google Map and dynamicly loading markers. In case 2 or more markers have same lat and long only one is presented. Is there any solution for this? This is how I plot markers to map
private void plotMarkers(ArrayList<MyMarker> markers, String markerIcon)
{
if(markers.size() > 0)
{
for (MyMarker myMarker : markers)
{
// Create user marker with custom icon and other options
// Toast.makeText(getApplicationContext(), myMarker.getmLatitude().toString(), Toast.LENGTH_SHORT).show();
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
markerOption.icon(BitmapDescriptorFactory.fromResource(manageMarkerIcon(markerIcon)));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
Even if you could, how would you use these two overlapping markers? only the top one would be visible.
Instead, if two markers overlap, you could express the data they share in one marker that contains them both.
this answer might assist: Android Google Maps v2 - Add object to marker
Both markers are shown, but both on the same location and it seems to be only one, and if you click on them, only one info window will be presented.
If you really need to add two markers at the same location you may want to take a look at Google Maps Android Marker Clustering Utility. It's part of the Google Maps Android API Utility Library.
A possible workaround could be to check if there is another marker in the position you are trying to draw the marker into, and if so, move the new marker a little bit appart.

Android Google Maps V2 - Indicate if there are markers outside of the current VisibleRegion on screen

I've been trying to work out a method for my application to indicate when there are markers on the map that are outside of the current view/screen/bound/VisibleRegion.
For example, if there are current 5 markers on the map but the user is zoomed into a part of the map where they can't see any of the markers then I would like to be able to flag up to the user that there are markers on the map that they cannot see or something along those lines (it would be nice to be able to indicate in which direction the markers are from the current position).
I thought of using
LatLngBounds currentScreen = googleMap.getProjection()
.getVisibleRegion().latLngBounds;
but this would only be able to tell me which markers are in the current visibleRegion.
any help would be appriciated, thanks.
First, you should save all your markers somewhere. For example, in list
List<Marker> markers = new ArrayList<>();
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(55.123, 36.456)));
markers.add(marker);
After this you can check, are there markers outside your screen
LatLngBounds currentScreen = mMap.getProjection().getVisibleRegion().latLngBounds;
for(Marker marker : markers) {
if(currentScreen.contains(marker.getPosition())) {
// marker inside visible region
} else {
// marker outside visible region
}
}

How to add a marker on Google map v2 using overlay?

i am working on a project, where i have to add a marker using overlay to existing Google map.
i have multiple markers along with infoWindowAdapter,for these i must pass title and snippet. I need one more marker to show only title(i can't directly add a marker with only title,since i used infoWindow for markers it will asks for snippet and ended up with null pointer exception)
I tried this
LatLng currenLoc = new LatLng(location.getLatitude(), location.getLongitude());
Toast.makeText(getApplicationContext(), ""+currenLoc, Toast.LENGTH_SHORT).show();
GroundOverlayOptions curLoc = new GroundOverlayOptions().image(BitmapDescriptorFactory.fromResource(R.drawable.mrk1)).position(currenLoc, 350f, 350f);
gMap.addGroundOverlay(curLoc);
but it shows only a marker like picture not an exact marker. So how to add a marker using ground overlay.?

Categories

Resources