2009-11-05 25 views
23

मैप्यूव्यू पर मैं लंबे समय तक कैसे क्लिक करूं ताकि मानचित्र पर उस बिंदु पर कोई स्थान मार्कर दिखाई दे?एंड्रॉइड मानचित्र: मानचित्र पर लंबे समय तक कैसे क्लिक करें?

मैं सफलता के बिना कुछ तरीके की कोशिश की:

1) MapvView जो longclicks का पता चला कभी नहीं पर setOnLongClickListener का उपयोग करना।

2) मेरा दूसरा विचार MapView को dispatchTouchEvent ओवरराइड करने के लिए विस्तार करना था .. लम्बाई कॉलबैक का जवाब देने के लिए एक जेस्चर डिटेक्टर बनाएं। लेकिन मैं यहां मिडवे फंस गया था क्योंकि मुझे अपने उप-वर्गीकृत मानचित्रदृश्य में एक संभाल नहीं मिल सका। यानी

MyMapview mymapview; //MyMapView extends MapView 

//results in a classcast exception 
mymapView = (MyMapView) findViewById(R.id.map); 

3) केवल अन्य तरीके से मैं जानता हूँ कि यह कैसे की कोशिश करने के लिए है: एक MotionEvent.ACTION_DOWN का पता लगाने और हैंडलर के लिए एक देरी runnable पोस्ट और पाठ और ईमेल का पता लगाने अगर दो अन्य घटनाओं: acton_move या एक action_up, नहीं है हो गई।

क्या कोई लंबी प्रेस का पता लगाने के लिए इनमें से किसी भी तरीके पर विचार प्रदान कर सकता है?

उत्तर

8

सबसे अच्छा तरीका है मैं यह करने के पता खुला स्रोत mapview-overlay-manager का उपयोग करें और अपने इशारे श्रोता जिसके लिए

public void onLongPress(MotionEvent e, ManagedOverlay overlay) 
+0

आपके उत्तर के लिए धन्यवाद .. मैंने आपके द्वारा वर्णित लाइब्रेरी का उपयोग किया .. मुझे लम्बाई के बजाय डबल टैप का उपयोग करना पसंद आया। – vamsibm

+1

मैं सहमत हूं; लंबी प्रेस समस्याग्रस्त है क्योंकि जब आप मानचित्र को स्क्रॉल कर रहे हों तो यह पॉप अप हो सकता है। – I82Much

22

मैं एक और भी आसान तरीका मिल गया है एक कॉलबैक प्रदान करता है का उपयोग करें। सूची में पहले ओवरले के रूप में केवल ओवरले बनाएं जो कुछ भी नहीं खींचता है और जेस्चरडेक्टर का उपयोग करके इशारों को पहचानने के लिए इसका उपयोग करता है। अगर यह घटना को संभाला जाता है तो इसे सत्य वापस करना चाहिए ताकि इसे प्रचारित न किया जाए।

public class MapGestureDetectorOverlay extends Overlay implements OnGestureListener { 
private GestureDetector gestureDetector; 
private OnGestureListener onGestureListener; 

public MapGestureDetectorOverlay() { 
    gestureDetector = new GestureDetector(this); 
} 

public MapGestureDetectorOverlay(OnGestureListener onGestureListener) { 
    this(); 
    setOnGestureListener(onGestureListener); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event, MapView mapView) { 
    if (gestureDetector.onTouchEvent(event)) { 
    return true; 
    } 
    return false; 
} 

@Override 
public boolean onDown(MotionEvent e) { 
    if (onGestureListener != null) { 
    return onGestureListener.onDown(e); 
    } 
    return false; 
} 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
    float velocityY) { 
    if (onGestureListener != null) { 
    return onGestureListener.onFling(e1, e2, velocityX, velocityY); 
    } 
    return false; 
} 

@Override 
public void onLongPress(MotionEvent e) { 
    if (onGestureListener != null) { 
    onGestureListener.onLongPress(e); 
    } 
} 

@Override 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
    float distanceY) { 
    if (onGestureListener != null) { 
    onGestureListener.onScroll(e1, e2, distanceX, distanceY); 
    } 
    return false; 
} 

@Override 
public void onShowPress(MotionEvent e) { 
    if (onGestureListener != null) { 
    onGestureListener.onShowPress(e); 
    } 
} 

@Override 
public boolean onSingleTapUp(MotionEvent e) { 
    if (onGestureListener != null) { 
    onGestureListener.onSingleTapUp(e); 
    } 
    return false; 
} 

public boolean isLongpressEnabled() { 
    return gestureDetector.isLongpressEnabled(); 
} 

public void setIsLongpressEnabled(boolean isLongpressEnabled) { 
    gestureDetector.setIsLongpressEnabled(isLongpressEnabled); 
} 

public OnGestureListener getOnGestureListener() { 
    return onGestureListener; 
} 

public void setOnGestureListener(OnGestureListener onGestureListener) { 
    this.onGestureListener = onGestureListener; 
} 
} 
+0

शांत .. यह बहुत स्मार्ट है। शायद मैं इसे कभी भी कोशिश करूँगा, अब मैं इस परियोजना के साथ कर रहा हूं। :) – vamsibm

+0

धन्यवाद डेविड .. मेरे लिए भी उपयोगी – indira

+1

सभी कोड – coyotte508

2

एम्ब्रोस,

मैं पुस्तकालय mapview-overlay-manager के डेमो संशोधित:

List<Overlay> overlays = mapView.getOverlays(); 
    overlays.clear(); 
    overlays.add(new MapGestureDetectorOverlay(new MyOnGestureListener())); 

और यहाँ क्लास है। इस कोड को डबल टैप जेस्चर के साथ चलाने के लिए:

package de.android1.overlaymanager.demo; 

import android.os.Bundle; 
import android.widget.Toast; 
import android.graphics.drawable.Drawable; 
import android.view.MotionEvent; 
import com.google.android.maps.MapActivity; 

import com.google.android.maps.MapView; 
import com.google.android.maps.MapController; 
import com.google.android.maps.GeoPoint; 

import de.android1.overlaymanager.*; 


public class DemoView extends MapActivity { 

    MapView mapView; 
    MapController mapController; 

    OverlayManager overlayManager; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     mapController = mapView.getController(); 

     overlayManager = new OverlayManager(getApplication(), mapView); 
    } 

    @Override 
    public void onWindowFocusChanged(boolean b) { 

     createOverlayWithListener(); 

    } 


    public void createOverlayWithListener() { 
     //This time we use our own marker 
     final ManagedOverlay managedOverlay = overlayManager.createOverlay("listenerOverlay", getResources().getDrawable(R.drawable.marker)); 
     for (int i = 0; i < 40; i = i + 3) { 
      managedOverlay.createItem(GeoHelper.geopoint[i], "Item" + i); 
     } 
     managedOverlay.setOnOverlayGestureListener(new ManagedOverlayGestureDetector.OnOverlayGestureListener() { 


      public boolean onZoom(ZoomEvent zoom, ManagedOverlay overlay) { 
       return false; 
      } 


      public boolean onDoubleTap(MotionEvent e, ManagedOverlay overlay, GeoPoint point, ManagedOverlayItem item) { 
       Drawable defaultmarker = getResources().getDrawable(R.drawable.marker);  

       ManagedOverlay managedOverlay = overlayManager.createOverlay(defaultmarker); 

       //creating some marker: 
       managedOverlay.createItem(point); 

       //registers the ManagedOverlayer to the MapView 
       overlayManager.populate(); 
       Toast.makeText(getApplicationContext(), "You created a Marker!", Toast.LENGTH_LONG).show(); 

       return true; 
      } 


      public void onLongPress(MotionEvent arg0, ManagedOverlay arg1) { 
       // TODO Auto-generated method stub 

      } 


      public void onLongPressFinished(MotionEvent arg0, 
        ManagedOverlay arg1, GeoPoint arg2, ManagedOverlayItem arg3) { 
       // TODO Auto-generated method stub 

      } 


      public boolean onScrolled(MotionEvent arg0, MotionEvent arg1, 
        float arg2, float arg3, ManagedOverlay arg4) { 
       // TODO Auto-generated method stub 
       return false; 
      } 


      public boolean onSingleTap(MotionEvent arg0, ManagedOverlay arg1, 
        GeoPoint arg2, ManagedOverlayItem arg3) { 
       // TODO Auto-generated method stub 
       return false; 
      }   
     }); 
     overlayManager.populate(); 
    } 


    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 
} 

आशा है कि इससे मदद मिलती है।

3

यह नक्शादृश्य-ओवरले-प्रबंधक लाइब्रेरी सुपर है। 'स्क्रॉलिंग' होने पर लॉन्गप्रेस ट्रिगर हो जाता है क्योंकि लाइब्रेरी मल्टीटाउच को ध्यान में रखती नहीं है।

public void onLongPress(MotionEvent motionEvent, ManagedOverlay arg1) { 

    if (motionEvent.getPointerCount() > 1) return; 

    ... your logic here ... 
} 
+0

लिखने के लिए समय निकालने के लिए धन्यवाद, लेकिन विधि प्राप्त करें पॉइंटर काउंटर केवल एपीआई स्तर 5 में उपलब्ध है। –

4

यहां नक्शे पर दबाए रखें से निपटने के लिए एक कार्यान्वयन है:: आप इस के आसपास onLongPress खारिज करता है, तो एक से अधिक सूचक इतनी के रूप में शामिल किया गया है द्वारा काम कर सकते हैं

http://www.kind-kristiansen.no/2011/android-handling-longpresslongclick-on-map-revisited/

4

यह साथ काम करता है आपके

  1. क्या नक्शे पर एक लंबे प्रेस का गठन के लिए अपने स्वयं के समय सीमा (0.8सेट करें: MapActivity और आप के लिए अनुमति देगा 0 सेकंड मेरे लिए अच्छा है)।
  2. एक लंबी प्रेस के रूप में स्क्रॉल, मल्टी-टच, या गैर-मल्टी-टच ईवेंट की व्याख्या नहीं करें। यह भी आपको किसी भी उंगली के लिए सहिष्णुता सेट करने के लिए स्क्रीन पर इतनी थोड़ी सी चाल के लिए सेट करता है क्योंकि वे एक लंबी प्रेस दबा रहे हैं।

यह समाधान Roger Kristiansen's solution पर आधारित है। मैंने एक्स और वाई पॉइंट डिटेक्शन जोड़ा ताकि स्क्रॉलिंग को लंबे प्रेस के रूप में नहीं देखा जा सके। यह समाधान रोजर के रूप में सुरुचिपूर्ण नहीं है क्योंकि मैं क्लास वेरिएबल का उपयोग करता हूं और मैप व्यू को विस्तारित करने और उसके जैसा श्रोता बनाने के बजाय कोड को अपनी मौजूदा मानचित्र सक्रियता में डालता हूं। लेकिन अगर आप अपने कोड से चिपकना चाहते हैं लेकिन बेहतर गैर-मल्टी-टच समर्थन है, तो बस एक्स और वाई पॉइंट सामान को मेरे बाहर ले जाएं और इसे अपने साथ जोड़ें।


कक्षा चर मेरी MapActivity के शीर्ष पर सेट करें:

//variable for determining long press and then automatically adding a pin to the map 
private int minMillisecondThresholdForLongClick = 800; 
private long startTimeForLongClick = 0; 
private float xScreenCoordinateForLongClick; 
private float yScreenCoordinateForLongClick; 
private float xtolerance=10;//x pixels that your finger can be off but still constitute a long press 
private float ytolerance=10;//y pixels that your finger can be off but still constitute a long press 
private float xlow; //actual screen coordinate when you subtract the tolerance 
private float xhigh; //actual screen coordinate when you add the tolerance 
private float ylow; //actual screen coordinate when you subtract the tolerance 
private float yhigh; //actual screen coordinate when you add the tolerance 

अपने MapActivity में इस समारोह में जोड़ें:

@Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 
     /* We want to capture the place the user long pressed on the map and add a marker (pin) on the map at that lat/long. 
     * This solution: 
     * 1. Allows you to set the time threshold for what constitutes a long press 
     * 2. Doesn't get fooled by scrolling, multi-touch, or non-multi-touch events 

     * Thank you Roger Kind Kristiansen for the main idea 
     */  

     //get the action from the MotionEvent: down, move, or up 
     int actionType = ev.getAction(); 

     if (actionType == MotionEvent.ACTION_DOWN) { 
      //user pressed the button down so let's initialize the main variables that we care about: 
      // later on when the "Action Up" event fires, the "DownTime" should match the "startTimeForLongClick" that we set here 
      // the coordinate on the screen should not change much during the long press 
      startTimeForLongClick=ev.getEventTime(); 
      xScreenCoordinateForLongClick=ev.getX(); 
      yScreenCoordinateForLongClick=ev.getY(); 

     } else if (actionType == MotionEvent.ACTION_MOVE) { 
      //For non-long press actions, the move action can happen a lot between ACTION_DOWN and ACTION_UP      
      if (ev.getPointerCount()>1) { 
       //easiest way to detect a multi-touch even is if the pointer count is greater than 1 
       //next thing to look at is if the x and y coordinates of the person's finger change. 
       startTimeForLongClick=0; //instead of a timer, just reset this class variable and in our ACTION_UP event, the DownTime value will not match and so we can reset.       
      } else { 
       //I know that I am getting to the same action as above, startTimeForLongClick=0, but I want the processor 
       //to quickly skip over this step if it detects the pointer count > 1 above 
       float xmove = ev.getX(); //where is their finger now?     
       float ymove = ev.getY(); 
       //these next four values allow you set a tiny box around their finger in case 
       //they don't perfectly keep their finger still on a long click. 
       xlow = xScreenCoordinateForLongClick - xtolerance; 
       xhigh= xScreenCoordinateForLongClick + xtolerance; 
       ylow = yScreenCoordinateForLongClick - ytolerance; 
       yhigh= yScreenCoordinateForLongClick + ytolerance; 
       if ((xmove<xlow || xmove> xhigh) || (ymove<ylow || ymove> yhigh)){ 
        //out of the range of an acceptable long press, reset the whole process 
        startTimeForLongClick=0; 
       } 
      } 

     } else if (actionType == MotionEvent.ACTION_UP) { 
      //determine if this was a long click: 
      long eventTime = ev.getEventTime(); 
      long downTime = ev.getDownTime(); //this value will match the startTimeForLongClick variable as long as we didn't reset the startTimeForLongClick variable because we detected nonsense that invalidated a long press in the ACTION_MOVE block 

      //make sure the start time for the original "down event" is the same as this event's "downTime" 
      if (startTimeForLongClick==downTime){ 
       //see if the event time minus the start time is within the threshold 
       if ((eventTime-startTimeForLongClick)>minMillisecondThresholdForLongClick){ 
        //make sure we are at the same spot where we started the long click 
        float xup = ev.getX();     
        float yup = ev.getY(); 
        //I don't want the overhead of a function call: 
        xlow = xScreenCoordinateForLongClick - xtolerance; 
        xhigh= xScreenCoordinateForLongClick + xtolerance; 
        ylow = yScreenCoordinateForLongClick - ytolerance; 
        yhigh= yScreenCoordinateForLongClick + ytolerance; 
        if ((xup>xlow && xup<xhigh) && (yup>ylow && yup<yhigh)){ 

         //**** safe to process your code for an actual long press **** 
         //comment out these next rows after you confirm in logcat that the long press works 
         long totaltime=eventTime-startTimeForLongClick; 
         String strtotaltime=Long.toString(totaltime);        
         Log.d("long press detected: ", strtotaltime); 


         /* 
         //Now get the latitude/longitude of where you clicked. Replace all the code below if you already know how to translate a screen coordinate to lat/long. I know it works though. 

         //***************** 
         //I have my map under a tab so I have to account for the tab height and the notification bar at the top of the phone. 
         // Maybe there are other ways so just ignore this if you already know how to get the lat/long of the pixels that were pressed. 
         int TabHeightAdjustmentPixels=tabHost.getTabWidget().getChildAt(0).getLayoutParams().height; 
         int EntireTabViewHeight = tabHost.getHeight(); 
         Display display = getWindowManager().getDefaultDisplay(); 
         int EntireScreenHeight = display.getHeight(); 
         int NotificationBarHeight=EntireScreenHeight-EntireTabViewHeight; 
         //the projection is mapping pixels to where you touch on the screen. 
         Projection proj = mapView.getProjection(); 
         GeoPoint loc = proj.fromPixels((int)(ev.getX(ev.getPointerCount()-1)), (int)(ev.getY(ev.getPointerCount()-1)-TabHeightAdjustmentPixels-NotificationBarHeight)); 
         int longitude=loc.getLongitudeE6();    
         int latitude=loc.getLatitudeE6(); 
         //***************** 

         //**** here's where you add code to: 
         // put a marker on the map, save the point to your SQLite database, etc 
         */ 

        } 
       } 
      } 

     } 


     return super.dispatchTouchEvent(ev); 
    } 
+1

यह मेरे लिए एक उत्कृष्ट समाधान था! –

0

मैं भी एक ClassCastException मिला जब मैं MyMapView उपयोग करने की कोशिश। एक कामकाज के रूप में, मैंने MapView ऑब्जेक्ट के बजाय एक MyMapView ऑब्जेक्ट बनाया और प्रोग्रामेटिक रूप से इसे लेआउट में जोड़ा, और यह बहुत अच्छा काम करता है।

MyMapView mapView = new MyMapView(this, this.getString(R.string.APIMapKey)); 
    mapView.displayZoomControls(false); 
    mapView.setClickable(true); 
    FrameLayout item = (FrameLayout) findViewById(R.id.getlocationmap); 
    item.addView((MapView)mapView); 
0

आप एक हैंडलर (एक समाधान मैं अपने आप को उपयोग करें) करने के लिए देरी पोस्ट/संदेश के साथ जा रहे हैं, यह परीक्षण करने के लिए उपयोगी है, तो नक्शा ले जाया गया है हो सकता है, यानी अगर mapView.getMapCenter(), mapView.getLatitudeSpan() और mapView.getLongitudeSpan() वापसी वही जब सूचक नीचे चला गया।

संबंधित मुद्दे