Android:使覆盖图像可点击
我希望有人能帮忙解决这个问题。我创建了一个地图应用程序,它将图标放在 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',这样我就知道哪个位置被点击了。我在网上阅读了很多关于这样做的内容,但没有任何效果。任何可以让我更接近完成这项工作的信息都非常感谢。
I hope someone can help with this. I have created a map app that puts an icon on locations from an xml file, but I need to make the icons clickable to open a page of info about the location.
At present I have this under the class GPSLocationListener implements 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);
}
Outside of this class I have a class to create the location icons and assign the location names:
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;
}
}
I just need to make the image 'pointer2' clickable and open a new page with the 'locId', so that I know which location has been clicked. I have read alot online about doing this, but nothing has worked. Any information that can get me closer to making this work it greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对它进行了排序,但几乎必须从头开始并使用逐项覆盖。这是我引用的内容:http://blog。 chrisblunt.com/android-map-view-double-taps-and-overlay-markers/
Sorted it, but pretty much had to start from scratch and use Itemised Overlay. Here is what I referenced: http://blog.chrisblunt.com/android-map-view-double-taps-and-overlay-markers/