我只想让一个mapview识别出一个点击,以便调用另一个活动。
到目前为止,我尝试了常规的“onClick”,它总是在常规视图中为我工作,然后覆盖“onTouchEvent”,最后但并非最不重要的是使用onClickListener。
我将所有三种方法都输入以下代码中。任何帮助都非常感谢。
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class HelloMapView extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
final MapView map;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById(R.id.mapview);
map.setOnClickListener(new MapView.OnClickListener() {
public void onClick(View v){
System.out.println("I listened!");
}
});
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
System.out.println("onTouchEvent happened!");
super.onTouchEvent(arg0);
return super.onTouchEvent(arg0);
}
public void onClick(){
System.out.println("onClick entered!");
}
}
答案 0 :(得分:10)
我知道这是一个非常古老的问题,但是:
mMapView.getMap().setOnMapClickListener(new OnMapClickListener()
{
@Override
public void onMapClick(LatLng arg0)
{
android.util.Log.i("onMapClick", "Horray!");
}
});
会做的伎俩。
答案 1 :(得分:6)
在地图视图中添加Overlay并处理OnTouchEvent。尝试这样的事情:
public class MyMapActivity extends MapActivity {
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView)
{
if (e.getAction() == MotionEvent.ACTION_UP) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) e.getX(),
(int) e.getY());
MyMapActivity this.startActivityForResult(intent, requestCode);
}
return false;
}
}
// MyMapActivity methods
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = (MapView) findViewById(R.id.mapview);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
}
}
答案 2 :(得分:0)
我使用类似于kgiannakakis的回答,使用内部MapOverlay类,但我重载了OnTap()方法以提供Exit_Dialog来关闭活动。此实现仍允许在MapView上进行缩放和拖动控制,而不会影响Overlay控件。但是您必须在AlertDialog.Builder构造中使用mMapView.getContext()。见代码:
public class TripDataMapActivity extends MapActivity {
....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview_activity);
// Set result CANCELED in case the user backs out
setResult(Activity.RESULT_CANCELED);
mMapView = (MapView)findViewById(R.id.mapview);
mMapView.setBuiltInZoomControls(true);
mMapCntrl = mMapView.getController();
mMapCntrl.setZoom(14); // World View is Zoom 1
// Set-up Drawable Overlays
mMapOverlays = mMapView.getOverlays();
mMapOverlays.clear();
mDrawable = this.getResources().getDrawable(R.drawable.direction_arrow);
mItemizedOverlay = new MyItemizedOverlays(mDrawable);
updateMapView();
}
.....
protected void updateMapView() {
// Convert Location into GeoPoint.
int lat = (int)(locatn.getLatitude() * 1E6);
int lng = (int)(locatn.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
OverlayItem overlayitem = new OverlayItem(point, "someTitle!", "someSnippet!");
mMapCntrl.setCenter(point);
mItemizedOverlay.addOverlay(overlayitem);
mMapOverlays.add(mItemizedOverlay);
mMapView.invalidate();
}
....
// Inner Class Implementation
class MyItemizedOverlays extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlays(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
@Override
protected OverlayItem createItem(int ith) { return mOverlays.get(ith); }
@Override
public int size() { return mOverlays.size(); }
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate(); // will call createItem(int) above
}
@Override
protected boolean onTap(int index) {
// EXIT Dialog
AlertDialog.Builder exitDialog =
new AlertDialog.Builder(mMapView.getContext());
exitDialog.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent();
//intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
// Set result and finish this Activity
setResult(Activity.RESULT_OK, intent);
TripDataMapActivity.this.finish();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
exitDialog.show();
return true;
}
// End of Inner Class
}
}
答案 3 :(得分:0)
我更愿意覆盖onTap方法。
class MapOverlay extends Overlay
{
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
//DO YOUR STUFF
return super.onTap(p, mapView);
}
}
在onTouch事件中,您必须通过等待MotionEvent.ACTION_DOWN
和MotionEvent.ACTION_UP
来自行定义标签。
而且你必须过滤掉MotionEvent.ACTION_MOVE
以避免在用户移动地图时做你的东西。
过滤移动事件也可能导致在开发旧版Android版本时出现问题。正如我测试的那样,即使我没有移动地图,移动事件也会被我的Android 2.3.3手机抛出。所以我无法区分正常点击(向下和向上)和移动(向下,移动和向上)