Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:24:54+00:00 2026-06-18T12:24:54+00:00

I’m trying to develop some algorythm for clustering markers on map. Amount of displayed

  • 0

I’m trying to develop some algorythm for clustering markers on map. Amount of displayed markers should depends on current zoom level. If I show one marker from group of 10 I want to set it’s title to “10”. The problem is that now sometimes visible markers doesn’t have title at all, I’ve no idea how it’s possible.
Here is my code:

public class MainActivity extends FragmentActivity {
private ArrayList<Marker> markers = new ArrayList<Marker>();
private Bitmap markerImage;
private float oldZoom = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    markerImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher);

    setContentView(R.layout.activity_main);

    final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    map.getUiSettings().setMyLocationButtonEnabled(true);
    map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            if (cameraPosition.zoom != oldZoom) {
                checkMarkers(map);
            }
            oldZoom = cameraPosition.zoom;
        }
    });
    createMarkers(map);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private void createMarkers(GoogleMap map) {
    double initLat = 48.462740;
    double initLng = 35.039572;
    for (float i = 0; i < 2; i += 0.2) {
        LatLng pos = new LatLng(initLat + i, initLng);
        Marker marker = map.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
        markers.add(marker);
    }
    for (float i = 0; i < 2; i += 0.2) {
        LatLng pos = new LatLng(initLat, initLng + i);
        Marker marker = map.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
        markers.add(marker);
    }

    initLat = 40.462740;
    initLng = 30.039572;
    for (float i = 0; i < 2; i += 0.2) {
        LatLng pos = new LatLng(initLat + i, initLng + i);
        Marker marker = map.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
        markers.add(marker);
    }

}


private void checkMarkers(GoogleMap map) {
    Projection projection = map.getProjection();
    LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
    HashMap<Marker, Point> points = new HashMap<Marker, Point>();
    for (Marker marker : markers) {
        if (bounds.contains(marker.getPosition())) {
            points.put(marker, projection.toScreenLocation(marker.getPosition()));
            marker.setVisible(false);
        }
    }
    CheckMarkersTask checkMarkersTask = new CheckMarkersTask();
    checkMarkersTask.execute(points);
}

private class CheckMarkersTask extends AsyncTask<HashMap<Marker, Point>, Void, HashMap<Point, ArrayList<Marker>>> {


    private double findDistance(float x1, float y1, float x2, float y2) {
        return Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
    }

    @Override
    protected HashMap<Point, ArrayList<Marker>> doInBackground(HashMap<Marker, Point>... params) {
        HashMap<Point, ArrayList<Marker>> clusters = new HashMap<Point, ArrayList<Marker>>();
        HashMap<Marker, Point> points = params[0];
        boolean wasClustered;
        for (Marker marker : points.keySet()) {
            Point point = points.get(marker);
            wasClustered = false;
            for (Point existingPoint : clusters.keySet()) {
                if (findDistance(point.x, point.y, existingPoint.x, existingPoint.y) < 25) {
                    wasClustered = true;
                    clusters.get(existingPoint).add(marker);
                    break;
                }
            }
            if (!wasClustered) {
                ArrayList<Marker> markersForPoint = new ArrayList<Marker>();
                markersForPoint.add(marker);
                clusters.put(point, markersForPoint);
            }
        }
        return clusters;
    }

    @Override
    protected void onPostExecute(HashMap<Point, ArrayList<Marker>> clusters) {
        for (Point point : clusters.keySet()) {
            ArrayList<Marker> markersForPoint = clusters.get(point);
            Marker mainMarker = markersForPoint.get(0);
            mainMarker.setTitle(Integer.toString(markersForPoint.size()));
            mainMarker.setVisible(true);
        }
    }

}
}

As you can see all visible markers should have title, but actually often they doesn’t. Any ides what is wrong?

UPD: I’ve just found that if call map.clear() and readd markers on each CameraChange (insteed of replacing title and visibility) everything works fine. It looks strange for me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:24:56+00:00Added an answer on June 18, 2026 at 12:24 pm

    I’ve solved this problem by clearing map on each CameraChange event and then adding needed markers. It works fine for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.