I'm making an app which contains a map with multiple markers that always move. The number of markers is not constant and depends on data from Firebase live database. I have a loop that creates the needed amount of markers on coordinates from database and deletes them on old positions. This is the only way of moving the markers I found.
The problem is that map is always being centred by the last created marker. I have to get rid of that in order to make free, smooth navigation over the map while the markers update every 2 seconds.
I have tried memorising the center of the map before drawing a marker and returning to it after. This solved the problem, but it still creates some sort of bumps and sometimes map gets moved away when markers update while the map is being moved.
public void run()
{
try
{
map = findViewById(R.id.mapview);
map.getOverlayManager().clear();
int i = 0;
while (i <= buses.size()-1)
{
GeoPoint center = (GeoPoint) map.getMapCenter();
List<String> inputSplit = new ArrayList<String>(Arrays.asList(buses.get(i).split("=")));
String label = inputSplit.get(0);
List<String> markerCoords = new ArrayList<String>(Arrays.asList(inputSplit.get(1).split(";")));
markerLat = Float.parseFloat(markerCoords.get(0));
markerLon = Float.parseFloat(markerCoords.get(1));
GeoPoint point = new GeoPoint(markerLat, markerLon);
Marker startMarker = new Marker(map);
startMarker.setPosition(point);
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(startMarker);
map.getController().setCenter(point);
startMarker.setTitle(label);
map.getController().setCenter(center);
i++;
}
}
finally
{
mHandler.postDelayed(mStatusChecker, mInterval);
}
}
Does anyone know how to create a marker without moving the center of the map to it?
I'm new to Android Studio and Java in general, so if you see any mistakes in my code that are not related to the question, please tell me, I'll be thankful for you feedback. Thanks in advance.
"The problem is that map is always being centred by the last created marker"
=> This is only because you do it explicitely in your code. Here:
map.getController().setCenter(point);
Twice!