I simply want a mapview to recognize a click, in order to call another activity.
Until now, i tried the regular "onClick",that always worked for me in regular Views, followed by overriding "onTouchEvent" and last but not least using an onClickListener.
I out all three ways into the following code. Any help is highly appreciated.
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!");
}
}
Add an Overlay in your map view and handle the OnTouchEvent. Try something like this:
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);
}
}