Android地图上添加图层并可监听图层点击事件
2012-07-04 17:49:16 来源:WEB开发网核心提示://绘画需要显示的文本信息 canvas.drawText(locationName, left_point_x+10, bottom_right_y-15, paint); } } @Override public boolean onTap(GeoPoint p_geopoint,MapView ma
//绘画需要显示的文本信息
canvas.drawText(locationName, left_point_x+10, bottom_right_y-15, paint);
}
}
@Override
public boolean onTap(GeoPoint p_geopoint,MapView mapView){
Point point = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(p_geopoint, point);
int x = point.x;
int y = point.y;
canvas.drawText(locationName, left_point_x+10, bottom_right_y-15, paint);
}
}
@Override
public boolean onTap(GeoPoint p_geopoint,MapView mapView){
Point point = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(p_geopoint, point);
int x = point.x;
int y = point.y;
//判断当前点击的坐标位置是否在标记图层范围内,如果在则通知外部进行相应操作。
if((x >= start_x && x <= end_x) && (y>=start_y && y<=end_y)){
//System.out.println("onTap");
if(onClickListener!=null){
onClickListener.OnItemClick(data);
}
}
return super.onTap(p_geopoint, mapView);
}
}
if((x >= start_x && x <= end_x) && (y>=start_y && y<=end_y)){
//System.out.println("onTap");
if(onClickListener!=null){
onClickListener.OnItemClick(data);
}
}
return super.onTap(p_geopoint, mapView);
}
}
自定义的接口对象
public interface OnMarkItemClickListener {
public void OnItemClick(Bundle bundle);
}
public void OnItemClick(Bundle bundle);
}
在MapActivity中添加图层并监听图层是否被单击:
Bundle data = new Bundle();
data.putString("locationName",info.name);
data.putString("locationAddress", info.address);
Message message = myHandler.obtainMessage(2,info.pt);
message.setData(data);
message.sendToTarget();
data.putString("locationName",info.name);
data.putString("locationAddress", info.address);
Message message = myHandler.obtainMessage(2,info.pt);
message.setData(data);
message.sendToTarget();
使用Bundle和Handle来传递数据和添加标记图层
在自定义Handler子类中,向地图添加图层:
MarkItemOverlay overlay = new MarkItemOverlay((GeoPoint)msg.obj,msg.getData(), onMarkItemClickListener);
mvSearch.getOverlays().add(overlay);
//在UI线程中调用invalidate方法刷新界面
mvSearch.invalidate();
mvSearch.getOverlays().add(overlay);
//在UI线程中调用invalidate方法刷新界面
mvSearch.invalidate();
更多精彩
赞助商链接