How to detect my position is no longer shown on map (after navigation)?
Asked Answered
T

2

6

I want to add a button in map that center the map on user current position, but it should be activated only if the user navigate in map and his current position is no longer displayed on map. to detect the navigation I used the onTouchEvent method.

 @Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {

Log.e("Touch", Integer.toString(event.getAction()));

int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
    touchStarted = true;
} else if (action == MotionEvent.ACTION_MOVE) {

   if (event.getPointerCount() > 3)
     moveStarted = true;
    return true;

}
return true;
}

but how I detect that my current position is no longer on screen?

Tasset answered 22/10, 2013 at 23:13 Comment(0)
T
9

Actually i found a better solution:

private void CheckVisibility(Marker myPosition)
{
    if(googleMap != null)
    {
        //This is the current user-viewable region of the map
        LatLngBounds bounds = googleMap.getProjection().getVisibleRegion().latLngBounds;

            if(bounds.contains(myPosition.getPosition()))
                   //If the item is within the the bounds of the screen

            else
                  //If the marker is off screen
    }
}
Tasset answered 24/10, 2013 at 15:26 Comment(0)
E
0

My best idea on how to do this would be to first find out what the camera (ie. what the user can see) is pointed at. Then, do some math to figure out exactly what is in view of the camera in terms of a LatLng and compare it to the user's current location.

  1. Get CameraPosition data on current LatLng, Zoom and Tilt - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/CameraPosition
  2. Figure out what is actually displayed on the screen. Check here for a starting point: https://developers.google.com/maps/documentation/android/views#the_camera_position
  3. Get current location from your LocationListener through a LocationProvider.
  4. Math some more to see if the user's location is view-able on the current screen.

An alternate suggestion would be to just use the included My Location Button as part of the API. It will always be visible, but it will be the same as the Google Maps App, so your users will already understand how to interact with it.

Egyptian answered 22/10, 2013 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.