MapView中的OnTap和LongPressListener

时间:2012-03-20 08:21:43

标签: android google-maps android-mapview itemizedoverlay

做一种基于Navigator的应用程序,我需要在mapview中包含一个OnTap和LongPress事件。制作水龙头时,它应显示lat的位置。在制作LongPress时,它应该显示一些选项,例如用作目标等。任何人请提供有关这些事件的想法。我自己是android的初学者,这个帮助会很感激。请帮助。

1 个答案:

答案 0 :(得分:3)

hii我使用2点击你看

/**
 * This Activity called when draw a fence on to the map.
 * 
 * @author ketan kalariya(ketan.kalariya@indianic.com)
 * **/
public class AddFenceActivity extends FragmentMapActivity implements
        SimpleGestureListener {



    private List<Overlay> mapOverlays;
    double[] location = new double[2];

    protected void onCreate(Bundle icicle) {
        setTheme(THEME);
        super.onCreate(icicle);
        setContentView(R.layout.addfenceactivity);

        mapView = (MapView) findViewById(R.id.addfenceactivity_mapview);
        Log.d(TAG, "AddFence Activity come from Home Activity");
        id = getIntent().getIntExtra("ID", 0);

        /**
         * Access the overlay list. Returns: The list of overlays.
         **/
        mapOverlays = mapView.getOverlays();

        myPrefs = getPreferences(MODE_PRIVATE);
        mDetector = new SimpleGestureFilter(this, this);

        MapController mapController = mapView.getController();

        mapView.setSatellite(false);
        mapView.setBuiltInZoomControls(true);

        Dataset = (GeoFencApplicationDataset) getApplicationContext();
        dbHelper = Dataset.getDbHelper();
        dbHelper.deleteTempTable();

    }

    /**
     * Destroy all fragments and loaders.
     * 
     * **/
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }




    /**
     * when it called.. The fence which is drawn on to map, it will be clear and
     * lat/long stored in to preference and database table will become
     * empty/null
     **/
    private void MapClear() {

        mapOverlays.clear();
        prefEdit = myPrefs.edit();
        prefEdit.putString("lat", "");
        prefEdit.putString("long", "");
        prefEdit.putString("srclat", "");
        prefEdit.putString("srclong", "");
        prefEdit.commit();
        mapView.invalidate();
        dbHelper.deleteTempTable();
        drawFlag = false;

    }

    /**
     * For accounting purposes, the server needs to know whether or not you are
     * currently displaying any kind of route information, such as a set of
     * driving directions.
     * 
     * @return True if route information is displayed; false otherwise.
     **/
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * Called to process touch screen events. using mDetector object of
     * SimpleGestureFilter call onTouchEvent
     * 
     * @param mMotionEvent
     *            The touch screen event.
     * @return boolean Return true if this event was consumed.
     **/
    public boolean dispatchTouchEvent(MotionEvent mMotionEvent) {
        this.mDetector.onTouchEvent(mMotionEvent);
        return super.dispatchTouchEvent(mMotionEvent);
    }

    /**
     * when double click on to screen put first pin on map, consider first
     * geoPoint. on second double click put second pin on map and draw path on
     * map mapOvelay from fist point to second and insert all lat,long in
     * database
     * **/
    public void onDoubleTap(MotionEvent event) {
        // TODO Auto-generated method stub

        GeoPoint geoPoint = mapView.getProjection().fromPixels(
                (int) event.getX(), (int) event.getY() - 120);

        srcLat = geoPoint.getLatitudeE6() / 1E6;
        srcLong = geoPoint.getLongitudeE6() / 1E6;

        Drawable marker = getApplicationContext().getResources().getDrawable(
                R.drawable.pin1);
        mapView.getController().animateTo(geoPoint);
        MapAddressOverlay itemizedOverlay = new MapAddressOverlay(marker);
        itemizedOverlay.addOverlay(AddFenceActivity.this, srcLat, srcLong);
        mapView.getOverlays().add(itemizedOverlay);
        mapView.invalidate();

        if (!String.valueOf(srcLat).equals("")
                || !String.valueOf(srcLong).equals("")) {
        ......
..
.
..

}