Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding touch event listeners to MapView

Tags:

android

So I want to return the the geo location of whatever point I touch on the map,but my code just doesn't do anything on moving across/clicking the screen. I'm new to Java as well as Android, so I think its something to do with my lack of knowledge of coding in java. Here's my code

package sdpd.loc;

import sdpd.loc.createNote.mapOverlay;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
import android.app.Activity;

public class createNote extends MapActivity {

@Override
protected boolean isRouteDisplayed() {
    return false;
}       

@Override
public void onCreate(Bundle savedInstanceStates){       
    super.onCreate(savedInstanceStates);
    setContentView(R.layout.map);

    MapView mapview=(MapView)findViewById(R.id.MapView);
    mapview.setBuiltInZoomControls(true);


}   


class mapOverlay extends com.google.android.maps.Overlay{
    @Override

    public boolean onTouchEvent(MotionEvent event, MapView mapview){

        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;
    }
}

}

How do I get it to work?

like image 680
Achint Avatar asked Mar 13 '11 07:03

Achint


People also ask

How do you handle touch events in a fragment?

OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if(event. getAction() == MotionEvent. ACTION_MOVE){ //do something } return true; } }); //here the rest of your code return view; MotionEvent.

How are Android touch events delivered?

The touch event is passed in as a MotionEvent , which contains the x,y coordinates, time, type of event, and other information. The touch event is sent to the Window's superDispatchTouchEvent() . Window is an abstract class. The actual implementation is PhoneWindow .

How do I get touch event on android?

Try code below to detect touch events. mView. setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //show dialog here return false; } }); To show dialog use Activity method showDialog(int).

What is touch slop?

"Touch slop" refers to the distance in pixels a user's touch can wander before the gesture is interpreted as scrolling.


1 Answers

You didn't register your custom Overlay class mapOverlay (btw. class names start always with an uppercase letter in Java) to the MapView. Do this by creating an instance of the class and adding it to the overlays collection of you MapView.

You can do this by appending following code to the onResume() method of your activity.

public void onCreate(Bundle savedInstanceStates){       
    super.onCreate(savedInstanceStates);
    setContentView(R.layout.map);

    MapView mapview=(MapView)findViewById(R.id.MapView);
    mapview.setBuiltInZoomControls(true);

    mapOverlay myOverlay = new mapOverlay();
    List<Overlay> overlays = mMapView.getOverlays();        
    overlays.add(myOverlay);
}               

Now your overlay is registered and the touch events should be processed.

like image 123
Flo Avatar answered Oct 13 '22 17:10

Flo