如何在油漆覆盖图上触摸(谷歌地图)?

时间:2012-06-20 17:05:15

标签: android google-maps overlay ontouchevent

我在googlemaps视图上绘制了一个叠加层(封闭的多边形,已填充)。但是现在我不知道如何在点击叠加层时弹出祝酒词。我找到的大多数例子都使用了标记,看起来与我的代码不同。

主要活动:

public class BOSLstItemDetail extends MapActivity{
    ArrayList<HashMap<String, Object>> boslst;
    MapView mapView; 
    MapController mapcontrol;
    GeoPoint p;
    Polygon polygon;

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

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



        String coordinates[] = {"48.098056", "9.788611"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

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

        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapcontrol = mapView.getController();
        mapcontrol.animateTo(p);
        mapcontrol.setZoom(10);
        mapView.setBuiltInZoomControls(true);

        ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
        try{
            InputStream koord = getAssets().open("gps.txt");
            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 Polygon(points);
            }
        }catch (Exception e) {
            Log.e("APP","Failed", e);
        }        

        mapView.getOverlays().clear();
        mapView.getOverlays().add(polygon);
        mapView.invalidate();
    }
}

Polygon.java

public class Polygon extends Overlay {
    ArrayList<GeoPoint> geoPoints;

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

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow){
        //Set the color and style
        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);

    }
}

gps.txt:

9.34669876098644,48.2405319213867
9.36384963989269,48.2296714782715
9.3639497756958,48.2259712219238
9.87827968597418,48.2786293029785
9.87261867523205,48.2822494506837
9.87254810333263,48.2859611511232
9.88368034362787,48.2898597717285
9.8835382461549,48.2972793579102
9.72781181335461,47.9827613830566
9.72225093841558,47.9826812744141
9.72232818603527,47.9789619445801
9.71129894256597,47.9750900268555
9.70574092864985,47.9750099182129
9.70557022094732,47.9824409484864
9.69992923736572,47.9860801696778
9.69436073303234,47.9860000610352
9.33546066284174,48.2403602600099
9.34669876098644,48.2405319213867

1 个答案:

答案 0 :(得分:0)

如果您正在扩展叠加层,则可以覆盖OnTap方法:

protected boolean onTap(final int index)
{
    OverlayItem item = mOverlays.get(index);
    if(item != null){
        Toast.makeText(mContext, textToShow, Toast.LENGTH_SHORT).show();
    }       
    //return true to indicate we've taken care of it
    return true;
}
相关问题