I've got a standard code for map in my app. The problem is that map lags while scrolling/zooming and loads really slow. I think the problem is that a lot of tiles are loaded in memory. How can I make a map to load only tiles which are currently visible on the screen (like in FakeGPS app)? Thank you!
public class MapActivity extends Activity
implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener,
GoogleMap.OnMyLocationButtonClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnInfoWindowClickListener,
GoogleMap.OnMapClickListener {
private GoogleMap mMap;
private LocationClient mLocationClient;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
private ClearableEditText searchField;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
setUpActionBar();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnMapClickListener(this);
mMap.setInfoWindowAdapter(new CreatePartyInfoWindowAdapter(this));
mMap.setOnInfoWindowClickListener(this);
}
}
}
private void setUpLocationClientIfNeeded() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(getApplicationContext(),
this, // ConnectionCallbacks
this); // OnConnectionFailedListener
}
}
public void onMapLongClick(LatLng latLng) {
...
}
public void onInfoWindowClick(Marker marker) {
...
}
public void onMapClick(LatLng latLng) {
...
}
protected void onResume() {
super.onResume();
...
setUpMapIfNeeded();
setUpLocationClientIfNeeded();
mLocationClient.connect();
}
public void onPause() {
super.onPause();
if (mLocationClient != null) {
mLocationClient.disconnect();
}
}
protected void onDestroy() {
super.onDestroy();
...
}
public void onConnected(Bundle bundle) {
mLocationClient.requestLocationUpdates(
REQUEST,
this); // LocationListener
Location location = mLocationClient.getLastLocation();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 12.0f));
}
public void onDisconnected() {
}
public void onLocationChanged(Location location) {
}
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public boolean onMyLocationButtonClick() {
...
return false;
}
}