我想获取触摸位置的坐标并将其传递给另一个 Activity 。谷歌地图显示在我的 Activity 中,但它的触摸事件不起作用。我想获取触摸位置的坐标并启动另一个 Activity ,将这些坐标传递给另一个 Activity 。这是我的代码:
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;
}
}
最佳答案
Google map 提供了一个采用 LatLng 的 OnMapClickListener。 首先,将 onMapClickListener 设置为您的 map ,然后在监听器中执行您的操作。 从我的头顶看,这应该是这样的
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
}
});
在确定您的 googleMap 不为空后,将其放入 initilizeMap()。
我目前无法测试代码,但我很确定它应该看起来像那样。否则谷歌文档关于 OnMapClickListener关于LatLng应该帮忙。
希望这对你有帮助,让我知道!
关于android - 在android中获取谷歌地图上的位置触摸坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153344/