1

I have a MapView in my application. I've gotten it to work as I want it to but I'd like to handle all the onClick or an equivalent event of the MapView and open the Google Maps application.

I've read that I can open the Google Maps application but raising an Intent like this:

String uri = "geo:"+ latitude + "," + longitude;
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));

How can I trap the aforementioned event of my MapView? I haven't been able to figure this out. Thanks.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382

2 Answers2

1

You should have a class that extends com.google.android.maps.Overlay in your activity and use this class' onTouchEvent() method. Like this:

class MyOverlay extends Overlay {

    @Override
    public boolean onTouchEvent(MotionEvent e, MapView mapView) {
         ....
    }
    ....
}
faradaj
  • 3,629
  • 1
  • 22
  • 21
  • I tried this but I still can't seem to get the touch/click events. I has a custom `Overlay` which I was using to draw a custom marker onto the map. I added the method you mentioned into my custom `Overlay` class but it didn't work. Any ideas on what I'm doing wrong? – Mridang Agarwalla Sep 26 '12 at 11:23
  • Does your Activity extends MapActivity? – faradaj Sep 26 '12 at 12:14
  • yes it does. I even tried removing the `draw()` method from my `Overlay` class but that didn't help. I'm wondering if it is some issue with the layout and some other control e.g. a `Button` is stealing the focus and touch events. I've read about this kind of issue somewhere. Any ideas? – Mridang Agarwalla Sep 26 '12 at 14:02
0

I used the onTap event like this:

public boolean onTap(GeoPoint gptLocation, MapView mapMap) {

    String strCoordinates = String.format("%f,%f", gptCoordinates.getLatitudeE6() / 1E6, gptCoordinates.getLongitudeE6() / 1E6);
    String strUri = String.format("geo:%s?z=14", strCoordinates);
    Intent ittMap = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(strUri));
    ittMap.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.ctxContext.startActivity(ittMap);
    return false;

}
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382