我希望有人可以帮助解决这个问题。我创建了一个地图应用程序,该应用程序在 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”打开一个新页面,这样我就知道点击了哪个位置。我在网上阅读了很多关于这样做的内容,但没有任何效果。任何可以让我更接近完成这项工作的信息都非常感谢。