2014-02-28 117 views
0

我想要使用onTouchevent獲取位置。當我點擊地圖時,它應該給出該位置的經度和緯度,但它不起作用。我困在此。onTouchEvent方法在Android中不起作用

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; 
     } 

    } 
} 

回答

0

onTouchEvent()應該是內部類MapActivity

現在不在課堂MapOverlay

+0

覆蓋是在mapactivity – user3076959

+0

我改變了它,但現在我仍然沒有得到經緯度@賈斯汀Jasmann – user3076959