0

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;
    }
}
rozag
  • 3
  • 4
  • 1
    This behaviour of jerky panning and zooming is known for the maps api 2 if a popup was in front of the map, when it was replaced by another fragment and then shown again. If that's also your case, then refer e.g. to http://stackoverflow.com/questions/25181860/mapfragment-bad-performance-after-using-the-back-button. – user2808624 Oct 09 '14 at 14:31
  • 1
    @user2808624 Thank you for your answer, but it's not that case. – rozag Oct 09 '14 at 20:26
  • I have got the case. Have you solved the problem? I am facing same problem. – Shamsul Arafin Mahtab Apr 16 '19 at 15:09

0 Answers0