0

For the need of my application, I need a Mapview should act like an ImageView.

The small map that would not have the default behavior (user is not allowed to move it) and I need to launch Google Maps when clicked on it.

I have tried to use code like this:

mapView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("geo:<lat>,<long>?q=<lat>,<long>(Label+Name)"));
        startActivity(intent);
    }
});

But it seems that the OnClickListener is never called, and I have not found how to disable the mapview scroll.

Thank for any help.

EDIT: I already tried to use clickable=false, taht's a good thing, because the map is now "disabled" and user cannot move it, but I an not able to launch GMaps when clicked...

Waza_Be
  • 39,407
  • 49
  • 186
  • 260

4 Answers4

2

When you set clickable(false) you can't use onClick, when you set clickable(true) then onTouch will take focus. So, override onTouch and then check inside it if your map view is not clickable, and if it is not clickable => start the activity.

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • Yeah, well I just saw your question and thought of this on top of my head. Maybe there is a better alternative for this. – Carnal Oct 15 '12 at 14:20
2

to launch Google Maps use onTouchListener because onClickListenere is never called when clicked on mapview. I tried like this :

mapView.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
            startActivity(intent);
            return false;
        }
    });

where NextActivity is another activity which extends MapActivity.

sarabhai05
  • 578
  • 1
  • 4
  • 12
0

You need to after mapView ready set onClick listener

mapView?.getMapAsync {googlemap->

     googlemap. setOnMapClickListener(object : GoogleMap.OnMapClickListener {
         override fun onMapClick(p0: LatLng?) {
            //...
         }

})
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
-2

make android:clickable="false" in your xml file can help you

Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24