安卓系统:使叠加图像可点击



我希望有人能帮上忙。我已经创建了一个地图应用程序,它可以在xml文件中的位置上放置图标,但我需要使图标可以点击,以打开关于位置的信息页面。

目前,我在类GPSLocationListener下实现了LocationListener:

MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(point, "pointer", null);
List<Overlay> listOfOverLays = mapView.getOverlays();
listOfOverLays.clear();
// doc is data from xml file                
NodeList nodes = doc.getElementsByTagName("result");
listOfOverLays.add(mapOverlay);
for (int i = 0; i < nodes.getLength(); i++) {                       
    Element e = (Element)nodes.item(i);
    String locId = XMLfunctions.getValue(e, "id");
    String aString = XMLfunctions.getValue(e, "lat");
    double lat = Double.parseDouble(aString);
    String bString = XMLfunctions.getValue(e, "long");
    double longi = Double.parseDouble(bString);

    // here, 'point' and mapOverlay have already been created and added                                 
    GeoPoint point2 = new GeoPoint((int) (lat * 1E6), (int) (longi * 1E6));
    MapOverlay mapOverlay2 = new MapOverlay();
    mapOverlay2.setPointToDraw(point2, "place", XMLfunctions.getValue(e, "name"));
    OverlayItem item = new OverlayItem(point2, "", "");
    listOfOverLays.add(mapOverlay2);
    }   

在这个类之外,我有一个类来创建位置图标并分配位置名称:

class MapOverlay extends Overlay{
    private GeoPoint pointToDraw;
    private String pointerIcon;
    private String locName;
    public void setPointToDraw(GeoPoint point, String pointer, String locationName){
        pointToDraw = point;
        pointerIcon = pointer;
        locName = locationName;
    }
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
        super.draw(canvas, mapView, shadow);
        // convert point to pixels
        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);
        // add marker
        if(pointerIcon == "pointer"){
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pointer);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 22, null);
        }
        else if(pointerIcon == "bar"){
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pointer2);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 15, null);
            Paint paint = new Paint();
            paint.setColor(Color.BLACK);
            paint.setTextSize(20);
            canvas.drawText(locName, screenPts.x, screenPts.y-20, paint);
        }
        return true;
    }
}

我只需要让图片"pointer2"可以点击,并用"locId"打开一个新页面,这样我就知道点击了哪个位置。我在网上读了很多关于这样做的文章,但都没有奏效。任何能让我更接近完成这项工作的信息都将不胜感激。

对其进行了排序,但几乎必须从头开始并使用Itemized Overlay。以下是我引用的内容:http://blog.chrisblunt.com/android-map-view-double-taps-and-overlay-markers/

相关内容

  • 没有找到相关文章

最新更新