Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I handle map move end using Google Maps for Android V2?

I want to geocode address as soon as map center has been changed.

How can I handle map moveend with new Google Maps for Android V2? (I'm talking about the case then user drags map by finger)

like image 844
Alexey Zakharov Avatar asked Dec 04 '12 11:12

Alexey Zakharov


People also ask

How do you freely move on Google Maps?

To move around, hover your cursor in the direction you want to go. Your cursor becomes an arrow that shows which direction you're moving. To see where you might go next, look for the X. Click once to travel to the X.

Why can't I move in Google Maps?

You may need to update your Google Maps app, connect to a stronger Wi-Fi signal, recalibrate the app, or check your location services. You can also reinstall the Google Maps app if it isn't working, or simply restart your iPhone or Android phone.


2 Answers

Check out new maps api.

 @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();     } }  @Override public void onCameraMove() {     Toast.makeText(this, "The camera is moving.",                    Toast.LENGTH_SHORT).show(); }  @Override public void onCameraMoveCanceled() {     Toast.makeText(this, "Camera movement canceled.",                    Toast.LENGTH_SHORT).show(); }  @Override public void onCameraIdle() {     Toast.makeText(this, "The camera has stopped moving.",                    Toast.LENGTH_SHORT).show(); } 

developers.google.com sample

like image 56
punksta Avatar answered Sep 21 '22 04:09

punksta


Here is a possible workaround for determining drag start and drag end events:

You have to extend SupportMapFragment or MapFragment. In onCreateView you have to wrap your MapView in a customized FrameLayout (in example below it is the class "TouchableWrapper"), in which you intercepts touch events and recognizes whether the map is tapped or not. If your "onCameraChange" gets called, just check whether the map view is pressed or not (in example below this is the variable "mMapIsTouched").

Example code:

UPDATE 1:

  • return original created view in getView()
  • use dispatchTouchEvent() instead of onInterceptTouchEvent()

Customized FrameLayout:

private class TouchableWrapper extends FrameLayout {      @Override     public boolean dispatchTouchEvent(MotionEvent ev) {          switch (ev.getAction()) {             case MotionEvent.ACTION_DOWN:                 mMapIsTouched = true;                 break;             case MotionEvent.ACTION_UP:                 mMapIsTouched = false;                 break;         }          return super.dispatchTouchEvent(ev);      }  } 

In your customized MapFragment:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup parent,          Bundle savedInstanceState) {     mOriginalContentView = super.onCreateView(inflater, parent,              savedInstanceState);      mTouchView = new TouchableWrapper(getActivity());     mTouchView.addView(mOriginalContentView);      return mTouchView; }  @Override public View getView() {     return mOriginalContentView; } 

In your camera change callback method:

private final OnCameraChangeListener mOnCameraChangeListener =          new OnCameraChangeListener() {      @Override     public void onCameraChange(CameraPosition cameraPosition) {         if (!mMapIsTouched) {             refreshClustering(false);         }     } }; 
like image 42
AZ13 Avatar answered Sep 22 '22 04:09

AZ13