向MapView添加触摸事件监听器

时间:2022-11-28 00:02:56

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

所以我想返回我在地图上触摸到的任何点的geo位置,但是我的代码在移动/点击屏幕上没有任何作用。我对Java和Android都不熟悉,所以我认为这与我对Java编程知识的缺乏有关。这是我的代码

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?

我如何让它工作?

2 个解决方案

#1


10  

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.

您没有注册自定义覆盖类mapOverlay(顺便说一下)。在Java中,类名总是以大写字母开头)到MapView。通过创建类的实例并将其添加到您MapView的覆盖集合中来完成此操作。

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

可以通过将以下代码附加到活动的onResume()方法来实现这一点。

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.

现在您的覆盖被注册,触摸事件应该被处理。

#2


3  

public class GoogleMap extends MapActivity {

    MapView mapView; 
    MapController mc;
    GeoPoint p;

    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setStreetView(true);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 

        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

        mc = mapView.getController();
        String coordinates[] = {"23.0504926", "72.528938925"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));


        mc.animateTo(p);
        mc.setZoom(17); 

        //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        mapView.invalidate();
    }

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

    class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }

        ****@Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());

                Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                        p.getLatitudeE6()  / 1E6, 
                        p.getLongitudeE6() / 1E6, 1);

                    String add = "";
                    if (addresses.size() > 0) 
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                             i++)
                           add += addresses.get(0).getAddressLine(i) + "\n";
                    }

                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {                
                    e.printStackTrace();
                }   
                return true;
            }
            else                
                return false;
        }**        
    }** 
}

#1


10  

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.

您没有注册自定义覆盖类mapOverlay(顺便说一下)。在Java中,类名总是以大写字母开头)到MapView。通过创建类的实例并将其添加到您MapView的覆盖集合中来完成此操作。

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

可以通过将以下代码附加到活动的onResume()方法来实现这一点。

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.

现在您的覆盖被注册,触摸事件应该被处理。

#2


3  

public class GoogleMap extends MapActivity {

    MapView mapView; 
    MapController mc;
    GeoPoint p;

    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setStreetView(true);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 

        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

        mc = mapView.getController();
        String coordinates[] = {"23.0504926", "72.528938925"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));


        mc.animateTo(p);
        mc.setZoom(17); 

        //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        mapView.invalidate();
    }

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

    class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }

        ****@Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());

                Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                        p.getLatitudeE6()  / 1E6, 
                        p.getLongitudeE6() / 1E6, 1);

                    String add = "";
                    if (addresses.size() > 0) 
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                             i++)
                           add += addresses.get(0).getAddressLine(i) + "\n";
                    }

                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {                
                    e.printStackTrace();
                }   
                return true;
            }
            else                
                return false;
        }**        
    }** 
}