Reputation: 5
I have a fragment in my app which has a MapFragment inside. When I load this fragment I start to read about 2000 marker data from server and after reading them I will loop through each marker and add it to the Map. At first it takes 4 seconds for the map to show these 2000 markers. but each time after loading the map again ( like changing orientation or changing the drawermenu item and backing to the map again ) it takes more and more to load the markers. for example after 8 times changing the orientation, it takes 40 seconds for the map to load the same 2000 markers !
here is how I initialize the map :
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
CameraPosition cameraPosition = new CameraPosition.Builder().target( new LatLng(50.941303, 6.958166)).zoom(10).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
and here I add the markers to the map :
for (int i = 0; i<mapVehicles.size(); i++)
{
// latitude and longitude
double latitude = mapVehicles.get(i).latitude;
double longitude = mapVehicles.get(i).longitude;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude))
.title(mapVehicles.get(i).lineTripIdentification).snippet(destinationShort + ": " + mapVehicles.get(i).destination);
// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.vehicleannotation));
// adding marker
vehicleMarkers.put(googleMap.addMarker(marker), mapVehicles.get(i));
}
I also used the following in the onCreateView of the Fragment which has the MapFragment :
this.setRetainInstance(true);
any help would be much appreciated :)
Upvotes: 0
Views: 1791
Reputation: 36
Instead of removing and adding all markers again and again you should change the position of each marker by setPosition (LatLng latlng).
Upvotes: 2
Reputation: 7108
Only load the markes if not loaded before:
public void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState != null) {
// do nothing
} else {
// add markers
}
}
Upvotes: 0