I have problems to understand the documentation of ItemizedOverlay, so
I tried to build a clickable overlay with onTouchEvent. Drawing a
overlay over my googlemap works fine and add a onTouchEvent too. Now I
want that the onTouchEvent only works, when I touch the overlay. In
the moment it starts wherever I touch the map.


    package de.bodprod.rettinfo;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

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


    import android.content.Intent;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.Point;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.widget.Toast;


    public class BOSLstItemDetail extends MapActivity {
     /** Called when the activity is first created. */
        private List<Overlay> mapOverlays;
        private MapController mc;
        ArrayList<HashMap<String, Object>> boslst;
        MyOverlay polygon;
        private MapView mapView;
        private GeoPoint gP;
        //private MyOverlay myoverlay;


    @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.boslst_item_detail);

                //SQL ID der Leitstelle abfragen
                Intent in = getIntent();
                String id = in.getStringExtra("SQL_ID");
                int sql_id = Integer.valueOf(id.toString());

                // Daten der Leitstelle abfragen
                boslst = new ArrayList<HashMap<String,Object>>();
                DatabaseHandler db = new
DatabaseHandler(getApplicationContext());
                boslst = db.getBOSLst(sql_id);

                //Name der Leistelle
                String bosname = boslst.get(0).get("name").toString();

                //GPS Koordinaten der Leistelle
                String lat_s = boslst.get(0).get("lat").toString();
                String lng_s = boslst.get(0).get("lng").toString();

                //Umwandeln der GPS Koordinaten
                double lat = Double.parseDouble(lat_s);
                double lng = Double.parseDouble(lng_s);

                //Geo Point setzten für Startansicht
                gP = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));

                mapView = (MapView) findViewById(R.id.mapview);//Creating an
instance of MapView
                mapView.setBuiltInZoomControls(true);//Enabling the built-in
Zoom Controls

                //Initializing the MapController and setting the map to center
at the
                mc = mapView.getController();
                mc.setCenter(gP);
                mc.setZoom(10);

                //Dateiname für GeopPoints festlegen
                String gps = bosname.toLowerCase();

                //GeoPoints für malen abfragen
                ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
                try{
                        InputStream koord = getAssets().open(gps);
                        if (koord != null) {
                                InputStreamReader input = new 
InputStreamReader(koord);
                                BufferedReader buffreader = new 
BufferedReader(input);
                                String line;
                                while (( line = buffreader.readLine()) != null) 
{
                                        String[] point_t = line.split(",");
                                                double y = 
Double.parseDouble(point_t[0]);
                                                double x = 
Double.parseDouble(point_t[1]);
                                                points.add(new 
GeoPoint((int)(x*1e6), (int)(y*1e6)));
                                }
                                koord.close();
                                polygon = new MyOverlay(points);
                        }
                }catch (Exception e) {
                        Log.e("APP","Failed", e);
                }

                mapOverlays = mapView.getOverlays();
                mapOverlays.add(polygon);

        }

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

        class MyOverlay extends Overlay{
                ArrayList<GeoPoint> geoPoints;

                public MyOverlay(ArrayList<GeoPoint> points){
                    geoPoints = points;
                }


                public void draw(Canvas canvas, MapView mapv, boolean shadow){
                    Paint paint = new Paint();
                    paint.setColor(Color.parseColor("#88ff0000"));
                    paint.setAlpha(50);
                paint.setStyle(Paint.Style.FILL_AND_STROKE);

                    //Create path and add points
                    Path path = new Path();
                    Point firstPoint = new Point();
                    mapView.getProjection().toPixels(geoPoints.get(0),
firstPoint);
                    path.moveTo(firstPoint.x, firstPoint.y);

                    for(int i = 1; i < geoPoints.size(); ++i){
                        Point nextPoint = new Point();
                        mapView.getProjection().toPixels(geoPoints.get(i),
nextPoint);
                        path.lineTo(nextPoint.x, nextPoint.y);
                    }

                    //Close polygon
                    path.lineTo(firstPoint.x, firstPoint.y);
                    path.setLastPoint(firstPoint.x, firstPoint.y);
                    canvas.drawPath(path, paint);
                    super.draw(canvas, mapView, shadow);
                }
                public boolean onTouchEvent(MotionEvent event, MapView mapView)
                {
                    //---when user lifts his finger---
                    if (event.getAction() == 1) {
                        GeoPoint pp = mapView.getProjection().fromPixels(
                            (int) event.getX(),
                            (int) event.getY());
                            Toast.makeText(getBaseContext(),
                                pp.getLatitudeE6() / 1E6 + "," +
                                pp.getLongitudeE6() /1E6 ,
                                Toast.LENGTH_SHORT).show();
                    }
                    return false;
                }

        }
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to