onTouchEvent方法在Android中不起作用

时间:2014-02-28 04:42:12

标签: android ontouchevent

我正在尝试使用onTouchevent获取位置。当我点击地图时,它应该给出该位置的纬度和经度,但它不起作用。我坚持这个。

package com.mamun.tasktest;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity<GeoPoint, OverlayItem> extends Activity implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    private LocationManager manager;

    private TextView tvAddress;
    private Button btnSearch;
    private EditText etSearch;
    private LocationClient locationClient;
    private GoogleMap googleMap;
    private MapFragment mapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.map);

        manager = (LocationManager) getSystemService(LOCATION_SERVICE);

        tvAddress = (TextView) findViewById(R.id.tvaddress);
        btnSearch = (Button) findViewById(R.id.btnSearch);
        etSearch = (EditText) findViewById(R.id.etSearch);
        mapFragment = (MapFragment) getFragmentManager().findFragmentById(
                R.id.maps);
        googleMap = mapFragment.getMap();
        locationClient = new LocationClient(this, this, this);

    }
    MapView mapView;
     com.google.android.maps.GeoPoint p;

    class MapOverlay extends com.google.android.maps.Overlay {

        @Override
        public void draw(Canvas canvas, com.google.android.maps.MapView mapView,
                boolean shadow) {
            // TODO Auto-generated method stub
            super.draw(canvas, mapView, shadow);

            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);
        }
        @Override
        public boolean onTouchEvent(MotionEvent e,
                com.google.android.maps.MapView mapView) {
            // TODO Auto-generated method stub
            if (e.getAction() == 1) {
                com.google.android.maps.GeoPoint p =  mapView.getProjection().fromPixels(
                        (int) e.getX(), (int) e.getY());
                Toast.makeText(
                        getBaseContext(),
                        "Lat: " +  p.getLatitudeE6() / 1E6 + ", Lon: "
                                +  p.getLongitudeE6() / 1E6, Toast.LENGTH_SHORT)
                        .show();
            }

            return false;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

onTouchEvent()应该在班级MapActivity内。

现在不在班级MapOverlay中。