Sending screen boundaries to the server, and the identifiers of currently visible markers on the screen is your best bet. But still there are a couple of problems. How do you find all the markers contained in the range indicated by the borders of the screen? What if new tokens arrive on your server or some tokens are removed? Can you come with a support structure in this situation or will you check each point that matches the marker in the database one by one to determine if it is in the range or not? Given all this, you need to find a way to optimize storage and point requests, in other words, the latitude and longitude pairs that markers indicate. You must perform spatial indexing using one of the general spatial index methods.
There are many spatial indexing methods available, and one may be slightly better than the other depending on the use case. To make a long story short, since you need to request a range in this scenario, you must implement a quadrant. A square is called a tree data structure in which each inner node has exactly four children (northwest, northeast, southwest, southeast). If you are not aware of this structure, I believe that you can understand its basics in an hour, but explaining it in detail from scratch will lead to the loss of too much time for me. Therefore, I skip implementation details about quadrants. There are several sources that have already explained this structure much better than I could, and you can easily find an open source library.
I will only give the Java pseudo-hash method for your quadrant to find all the points that are displayed within the borders of the screen, excluding those that are already on the previous screen:
ArrayList<LatLng> queryRange(QuadLevel level, float[] screenBounds, ArrayList<LatLng> prevPoints) { // Initialize a list to hold the found points ArrayList<LatLng> pointsInRange = new ArrayList<>(); if (!quadtreeBounds.intersects(screenBounds)) return pointsInRange; // Find the points that are in the current quad level for (LatLng point : level.getPoints()) { // If the current point is in screen bounds and it is not contained by prevPoints if (point.isInRange(screenBounds) && !prevPoints.contains(point)) pointsInRange.add(point); } // If there are no children, return if (level.hasNoChildren()) return pointsInRange; // Else, continue to look up children pointsInRange.addAll(queryRange(level.northwest, screenBounds, prevPoints)); pointsInRange.addAll(queryRange(level.northeast, screenBounds, prevPoints)); pointsInRange.addAll(queryRange(level.southwest, screenBounds, prevPoints)); pointsInRange.addAll(queryRange(level.southeast, screenBounds, prevPoints)); return pointsInRange; }
source share