在android中的谷歌地图上获取位置触摸的坐标

use*_*397 2 android google-maps

我想获取被触摸的位置坐标并将其传递给另一个活动.谷歌地图显示在我的活动中,但其触摸事件无效.我想得到触摸位置的坐标,并开始将这些坐标传递给另一个活动的另一个活动.它是我的代码:

public class MapActivity extends Activity {
    protected LocationManager locationManager;
    String stlatitude = null, stlongitude = null;
    private GoogleMap googleMap;
    String locationName = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle b = getIntent().getExtras();
        stlatitude = b.getString("latitude");
        stlongitude = b.getString("longitude");
        locationName = b.getString("location_name");

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        try {

            initilizeMap();

            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.setMyLocationEnabled(true);
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(Float.parseFloat(stlatitude), Float
                            .parseFloat(stlongitude))).title(locationName);
            googleMap.addMarker(marker);
            if (stlatitude != null || stlongitude != null) {
                LatLng myLatLng = new LatLng(Float.parseFloat(stlatitude),
                        Float.parseFloat(stlongitude));
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                        myLatLng, 12.0f));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }


    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    public boolean onTouchEvent(MotionEvent event, MapView mapView)
     {
     //---when user lifts his finger---
     if (event.getAction() == 1) {
     GeoPoint p = mapView.getProjection().fromPixels(
     (int) event.getX(),
     (int) event.getY());
     Toast.makeText(getBaseContext(),
     p.getLatitudeE6() / 1E6 + "," +
     p.getLongitudeE6() /1E6 ,
     Toast.LENGTH_SHORT).show();
     }
     return false;
     }
     }
Run Code Online (Sandbox Code Playgroud)

小智 7

Google Maps提供了一个采用LatLng的OnMapClickListener.首先,将onMapClickListener设置为您的地图,然后在侦听器中执行您的操作.从头顶看,这看起来应该是这样的

googleMap.setOnMapClickListener(new OnMapClickListener() {
   @Override
   public void onMapClick(LatLng point) {
      Log.d("DEBUG","Map clicked [" + point.latitude + " / " + point.longitude + "]");
      //Do your stuff with LatLng here
      //Then pass LatLng to other activity
   }
});
Run Code Online (Sandbox Code Playgroud)

确认后,将此设置在initilizeMap()中,您的googleMap不为空.

我目前无法测试代码,但我很确定它应该是这样的.否则,有关OnMapClickListenerLatLng的 Google文档应该会有所帮助.

希望这能帮到你,让我知道!