Tuesday 15 February 2011

android - Creating OnDragListener for Google Map v2 Fragment -


I'm trying to set up an onDrag listener for a Google map slice but can not get drag event to Because in the fire, the map does not support drag events directly, so I am trying to apply the drag listener to the scene. I know about an Honor drag drag event, but it does not do me good because I am not trying to move the markers.

The reason for trapping the map drag event is two times:

Firstly determine if the user has painted the map. If they are not, I like to keep my place on the screen and to alert them to the place when they are not on the map. If they have mapped the map, then I think they know what they want to see and force the place on the screen. (This seems to be something that should be included in the API)

Another reason to trap is to allow users to attract lines and polygons on the map. I can do it with a tap event (by adding points), but I also want them to be able to free it for free.

My code looks like this:

Main activation:

  mapp = (MapFragment) getFragmentManager (). FindFragmentById (R.id. Map)). GetMap (); SetUpMapIfNeeded (); MMapView = findViewById (R.id.map); MyMapDragListener = New MapDragListener (); MMapView.setOnDragListener (myMapDragListener);   

Listener:

  implements the public class MapRgrinor Onorag Listener {/ ** * * / Public MapListener () {super}; Say.logCat ("MapraGrister: I'm alive !!!!"); } Private AkamaiAnnounce say = new AkamaiAnnounce (); Private boolean in drag; Private boolean = false; @ Override Public Boolean On Drag (see V, Dragravent Event) {say.logCat ("MapDragListener.onDrag:" + event.toString ()); // string dot TAG = getTag (string); If (event.getLocalState ()! = This) {return false; } Boolean myResult = true; Int action = event.getAction (); Float x = event .getX (); Float y = event .getY (); Switch (action) {case drag event Action_rag_STARTED: inDrag = true; break; Case dragage Action_rag_LOCATION: break; Case dragage Action_rag_ENTERED: break; Case dragage Action_rag_EXITED: break; Case dragage Action_DROP: myResult = false; break; Case dragage Action_RAAG_ENDED: inDrag = false; // The color of the original dot back has changedPaint = true; break; Default: myResult = false; break; } return false; // Send my return; } Has been kept in public boolean () {return; } Public Zero Satellusband (Boolean keypadding) {This. HasPanned = haspanned; }}   

The map works fine, the listener gets instantaneous, but my ordeal is never organized. Any ideas?

1) Create the wrapper class:

  expanded the public square MapWrapperLayout FrameLayout {Public Interface onDragListener {public void onDrag (MotionEvent motionEvent); } Private Indoor Listener MoDDrag Listener; Public MapWrapperLayout (reference reference) {super (reference); } @ Override Public Boolean Dispatch Touch Event (Motion Avenger Eva) {if (mOnDragListener! = Null) {mOnDragListener.onDrag (ev); } Return super. Descapture Event (Eve); } Public Zero SetOnDrag Listener (Ondrag Listener Møndrg Listener) {this.mOnDragListener = mOnDragListener; }}   

2) Create a subclass of the Mapfragment class:

  The public class extends CustomMapFragment SupportMapFragment {see private {mriginalView; Private MapWrapperLayout mMapWrapperLayout; @ Override Public View Crate View (LayoutInflator Inflator, View Group Container, Bundle Saved InstantState) {mOriginalView = super.onCreateView (inflater, container, savedInstanceState); MMapWrapperLayout = New MapWrapperLayout (getActivity ()); MMapWrapperLayout.addView (mOriginalView); Return mMapWrapperLayout; } @ Override Public View getView () {return mOriginalView; } Public Zero SetOnDrag Listener (MappDraper Layout.OnDrGlister on DragListener) {mMapWrapperLayout.setOnDragListener (OnDaga Listener); }   

3.1) Finally, in your activity:

  // Google map init blocks CustomMapFragment customMapFragment = (GetContentFragmentManager (CustomMapFragment). FindFragmentById ( R) .id.map)); CustomMapFragment.SetOnDrag Listener (new map-wired layout.OnDrag LISTENER) {@ Override Public Wide On Drag (Motion Event Motion Event) {Log D. ("ONRDRAG", string.format ("ME:% S", Speed ​​Event )); // Handle Motion Event:}}); GoogleMap Map = customMapFragment.getMap ();   

3.2) ... and in your layout:

  Android: id = "@ + id / map" Android: layout_width = "Match_parent" android : Layout_height = "match_parent" android: name = "your.package.CustomMapFragment" />    

No comments:

Post a Comment