0
public class Maps_location extends AppCompatActivity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps_location);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
        .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        MarkerOptions m = new MarkerOptions();
        LatLng TutorialsPoint = new LatLng(13.0827, 80.2707);
        mMap.addMarker(new
        MarkerOptions().position(new LatLng(0.0,0.0)));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(TutorialsPoint));
        float zoomLevel = (float) 16.5;
        mMap.moveCamera(CameraUpdateFactory.newLatLng(TutorialsPoint));
        mMap.setOnCameraIdleListener(Maps_location.this);
        // To Move the map
        mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
            @Override
            public void onCameraMove() {
                marker.setPosition(finalGoogleMap.getCameraPosition().target);//to center in map
            }
        });

        googleMap.setOnCameraIdleListener(new OnCameraIdleListener(){
            @Override
            public void onCameraIdle() {
                double CameraLat = mMap.getCameraPosition().target.latitude;
                double CameraLong = mMap.getCameraPosition().target.longitude;
                String abc = getCompleteAddressString(CameraLat, CameraLong);
            }
        });
    }

}

The app hangs alot and crashes which states Background concurrent copying GC freed 169606(11MB) AllocSpace objects, 45(11MB) LOS objects, 49% free, 19MB/39MB, paused 260us total 118.624ms

Arthur Attout
  • 2,701
  • 2
  • 26
  • 49
venkatesh
  • 319
  • 2
  • 10
  • Could you try commenting out your code from `onCameraIdle()` ? If your method `getCompleteAddressString` does something heavy, it could be the cause (because it will be called dozens of times) – Arthur Attout Apr 02 '19 at 10:36
  • Thanks alot, I tried it before reading your comment. It works like charm. Can you provide some suggestions where to get the complete address each time user scrolls the map. I have a non movable marker at the center and get the latitude & longitude and set the address in the textview below map. – venkatesh Apr 02 '19 at 11:29
  • It depends what you have inside that method. Given the name, I guess you are querying some Google API. First of all you should double check this method is efficient, and that you do not make huge loops, or manual JSON parsing, etc. I'd suggest to put a timer on your `setOnCameraIdleListener`, that triggers the request **only** if the user hasn't moved for 2 seconds for example. This should lighten the weight of the operation. – Arthur Attout Apr 02 '19 at 11:37
  • In previous comment you have specified that `setOnCameraIdleListener` will call dozens of time. But even if we set the timer to call after 2 secs will it call dozens of time once the screen is idle – venkatesh Apr 02 '19 at 11:42
  • Yes, the method will still be called a lot, but most of the time, it won't do anything. The trick is to trigger your request only if the camera has been idle for a long time. What you could do is use [some equivalent of SetTimeout](https://stackoverflow.com/questions/4817933/what-is-the-equivalent-to-a-javascript-setinterval-settimeout-in-android-java), store the handler somewhere, store the current timestamp, and kill your handler if the camera moves before your delay – Arthur Attout Apr 02 '19 at 11:47
  • (This is something from the top of my head, it may turn out not to be the most efficient solution, but it's still better than calling it over and over) – Arthur Attout Apr 02 '19 at 11:48

0 Answers0