9

मैप टैप किए जाने पर किसी स्थान के निर्देशांक प्राप्त करने के तरीके के बारे में मैंने खोज की है। हालांकि, अधिकांश, यदि सभी उदाहरणों को पैरामीटर के रूप में MapView की आवश्यकता नहीं है। उदाहरण के लिए:मैपफ्रैगमेंट (मैप व्यू नहीं) के साथ टैप पर मानचित्र के निर्देशांक कैसे प्राप्त करूं?

public boolean onTap(GeoPoint p, MapView map){ 
    if (isPinch){ 
     return false; 
    }else{ 
     Log.i(TAG,"TAP!"); 
     if (p!=null){ 
      handleGeoPoint(p); 
      return true;   // We handled the tap 
     }else{ 
      return false;   // Null GeoPoint 
     } 
    } 
} 

@Override 
public boolean onTouchEvent(MotionEvent e, MapView mapView) 
{ 
    int fingers = e.getPointerCount(); 
    if(e.getAction()==MotionEvent.ACTION_DOWN){ 
     isPinch=false; // Touch DOWN, don't know if it's a pinch yet 
    } 
    if(e.getAction()==MotionEvent.ACTION_MOVE && fingers==2){ 
     isPinch=true; // Two fingers, def a pinch 
    } 
    return super.onTouchEvent(e,mapView); 
} 

मैं कैसे इसलिए MapFragment और नहीं MapView साथ नक्शे पर एक टैप स्थिति के स्थान मिलता है?

उत्तर

24

Google Play सेवाएं एसडीके द्वारा प्रदत्त Sample Code में एक उदाहरण है। यह SupportMapFragment का उपयोग करता है, इसलिए मुझे यकीन नहीं है कि यदि आप नए MapFragment का उपयोग कर रहे हैं तो यह कितना उपयोगी होगा।

इस मानचित्र नमूना कोड में ईवेंट डेमो एक्टिविटी का उपयोग कक्षा में implement OnMapClickListener है। नीचे कुछ कोड है जिसका आप उपयोग करने में सक्षम हो सकते हैं।

EventsDemoActivity:

public class EventsDemoActivity extends FragmentActivity 
    implements OnMapClickListener, OnMapLongClickListener { 

    private GoogleMap mMap; 
    private TextView mTapTextView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.events_demo); 

     mTapTextView = (TextView) findViewById(R.id.tap_text); 

     setUpMapIfNeeded(); 
    } 

    private void setUpMap() //If the setUpMapIfNeeded(); is needed then... 
    { 
     mMap.setOnMapClickListener(this); 
     mMap.setOnMapLongClickListener(this); 
    } 

    @Override 
    public void onMapClick(LatLng point) { 
     mTapTextView.setText("tapped, point=" + point); 
    } 

    @Override 
    public void onMapLongClick(LatLng point) { 
     mTapTextView.setText("long pressed, point=" + point); 
    } 
} 


events_demo.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
    android:id="@+id/tap_text" 
    android:text="@string/tap_instructions" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
    <fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment"/> 
</LinearLayout> 
संबंधित मुद्दे