3

Folks, I built a fragment that has a SupportMapFragment I want to be able to drag the map and detect new "area" the camera is showing so I can load my markers within that area.

 ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);

On the mapReadycall back I added a onCameraChangeListener:

 @Override
public void onMapReady(GoogleMap googleMap) {

    googleMap.getUiSettings().setZoomControlsEnabled(false);
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

    googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {

            showItemsinMap(cameraPosition.target.latitude, cameraPosition.target.longitude);

        }
    });

}

I have another feature on my map that I can drag a polygon on top of it and get the markers within an area by using the getProjection method:

LatLng latLng = map.getProjection().fromScreenLocation(x_y_points);

where x_y_points are the x,y coordinates on the screen then I create a polygon and add get SW, SE, NW and NE points to get an area to search for markers.

What would be the best approach to handle a new area on the map?

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Im Rick James
  • 255
  • 3
  • 15
  • Check this SO question [13702117](http://stackoverflow.com/questions/13702117/how-can-i-handle-map-move-end-using-google-maps-for-android-v2) and [17223155](http://stackoverflow.com/questions/17223155/creating-ondraglistener-for-google-map-v2-fragment) if it can help you. – KENdi Aug 15 '16 at 02:55
  • 1
    Actually on Google Service 9.4.0 onCameraChangeListener is deprecated, now you have to implement onCameraIdle, problem is that is called every time, I am wondering if its called during OnStart, onResume, since I am making constant calls to this eventhough I dont move the map at all. – Im Rick James Aug 15 '16 at 13:33

1 Answers1

1

callback onCameraMoveStarted in map v2 onwards is your key to start.

   @Override
   public void onMapReady(GoogleMap map) {
      mMap = map;

      mMap.setOnCameraIdleListener(this);
      mMap.setOnCameraMoveStartedListener(this);
      mMap.setOnCameraMoveListener(this);
      mMap.setOnCameraMoveCanceledListener(this);

    // Show Sydney on the map.
      mMap.moveCamera(CameraUpdateFactory
            .newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
    }

@Override
public void onCameraMoveStarted(int reason) {

    if (reason == OnCameraMoveStartedListener.REASON_GESTURE) {
        Toast.makeText(this, "The user gestured on the map.",
                       Toast.LENGTH_SHORT).show();
      } else if (reason == OnCameraMoveStartedListener
                            .REASON_API_ANIMATION) {
        Toast.makeText(this, "The user tapped something on the map.",
                       Toast.LENGTH_SHORT).show();
      } else if (reason == OnCameraMoveStartedListener
                            .REASON_DEVELOPER_ANIMATION) {
        Toast.makeText(this, "The app moved the camera.",
                       Toast.LENGTH_SHORT).show();
      }
  }
Hassi
  • 120
  • 8