我希望能够获取触摸位置的坐标并将其传递给另一个活动。谷歌地图显示在我的活动中,但是它的触摸事件不起作用。我想要获取触摸位置的坐标,并将这些坐标传递给另一个活动,启动该活动。以下是我的代码:
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;
}
}