首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OSMDroid: onTap示例

OSMDroid: onTap示例
EN

Stack Overflow用户
提问于 2012-10-21 01:59:31
回答 1查看 8.6K关注 0票数 3

我几周前就开始学习Android了,现在我需要你们的帮助。我的任务是创建离线地图(使用OSMDroid和Mobile Atlas Creator),上面有两个标记,它们之间的路径和点击这个标记后的一些活动。我做了地图,标记和路径。代码如下(Android 2.3.3):

公共类MainActivity扩展了Activity {

代码语言:javascript
复制
private MapView mapView;
LocationManager locationManager;
ArrayList<OverlayItem> overlayItemArray;

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

    mapView = new MapView(this, 256);
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(true);
    mapView.getController().setZoom(15); 
    mapView.getController().setCenter(new GeoPoint(54.332, 48.389));  
    mapView.setUseDataConnection(false);

    overlayItemArray = new ArrayList<OverlayItem>();        
    OverlayItem olItem = new OverlayItem("Here", "SampleDescription", new GeoPoint(54.332, 48.389));       
    overlayItemArray.add(olItem);
    overlayItemArray.add(new OverlayItem("Hi", "You're here", new GeoPoint(54.327, 48.389)));

    PathOverlay myPath = new PathOverlay(Color.RED, this);
    myPath.addPoint(new GeoPoint(54.327, 48.389));
    myPath.addPoint(new GeoPoint(54.332, 48.389));
    mapView.getOverlays().add(myPath);

DefaultResourceProxyImpl defaultResourceProxyImpl = new DefaultResourceProxyImpl(this);
ItemizedIconOverlay<OverlayItem> myItemizedIconOverlay  = new ItemizedIconOverlay<OverlayItem>(overlayItemArray, null, defaultResourceProxyImpl);
    mapView.getOverlays().add(myItemizedIconOverlay);

    setContentView(mapView); //displaying the MapView
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}    

}问:如何实现该标记的onClick-method?教授的另一个问题是:如何做对(我的意思是如何将这个程序划分为类)?非常感谢!=)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-25 01:00:18

您需要创建新类,如下所示:

代码语言:javascript
复制
    public class MyOwnItemizedOverlay extends ItemizedIconOverlay<OverlayItem> {
    protected Context mContext;

    public MyOwnItemizedOverlay(final Context context, final List<OverlayItem> aList) {
         super(context, aList, new OnItemGestureListener<OverlayItem>() {
                @Override public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        return false;
                }
                @Override public boolean onItemLongPress(final int index, final OverlayItem item) {
                        return false;
                }
              } );
        // TODO Auto-generated constructor stub
         mContext = context;
    }

    @Override 
    protected boolean onSingleTapUpHelper(final int index, final OverlayItem item, final MapView mapView) {
        //Toast.makeText(mContext, "Item " + index + " has been tapped!", Toast.LENGTH_SHORT).show();
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }
}

下面是如何在第一个(main)类中使用它:

代码语言:javascript
复制
MapView mapView = new MapView(this, 256); //constructor
//some code from te question

ArrayList<OverlayItem> overlayItemArray = new ArrayList<OverlayItem>();                
OverlayItem olItem = new OverlayItem("Here", "SampleDescription", new GeoPoint(54.332, 48.389));//marker

MyOwnItemizedOverlay overlay = new MyOwnItemizedOverlay(this, overlayItemArray);
mapView.getOverlays().add(overlay);        
setContentView(mapView); //displaying the MapView

就这样。祝好运!链接:http://code.google.com/p/osmdroid/issues/detail?id=245#makechanges

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12991175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档