我正在尝试获取一个将ItemizedOverlay扩展到startActivity的类,但是有一个问题,它无法编译。
我有一个使用ItemizedOverlay类绘制叠加层的MapView,但是我想在屏幕上点击时开始和活动。

任何想法如何解决这个问题?谢谢

protected boolean onTap(int index) {
     OverlayItem item = overlays.get(index);

     String split_items = item.getSnippet();
     Intent intent = new Intent();
     intent.setClass(mainmenu,poiview.class);
     startActivity(intent);


     return true;
   }

最佳答案

我遇到了这个问题,因此我测试了以下示例。
该解决方案依赖于从MapActivity上下文调用“ startActivity”。

如果您的地图实际上正在使用叠加层,则您已经将MapView Context传递给自定义的ItemizedOverlay构造函数,并且可能已将MapView Context分配给了一个名为mContext的类变量(我假设您遵循Google的MapView示例)。因此,在您的自定义叠加层的onTap函数中,执行以下操作:

        @Override
    protected boolean onTap(int index) {

      Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
      mContext.startActivity(intent);


      return true;
    }


但是您可能希望将某些内容传递给要尝试启动的新活动,以便新活动可以对您的选择做出一些有用的事情。所以...

    @Override
protected boolean onTap(int index) {

  OverlayItem item = mOverlays.get(index);
  //assumption: you decided to store an "id" in the snippet so you can associate this map location with your new Activity
  long id = Long.parseLong(item.getSnippet()); //Snippet is a built-in String property of an Overlay object.

    //pass an "id" to the class so you can query
    Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
    String action = Intent.ACTION_PICK; //You can substitute with any action that is relevant to the class you are calling
    //I create a URI this way because I append the id to the end of the URI (lookup the NotePad example for help because there are many ways to build a URI)
    Uri uri = ContentUris.withAppendedId(Your_CONTENT_URI, id);
    //set the action and data for this Intent
    intent.setAction(action);
    intent.setData(uri);
    //call the class
    mContext.startActivity(intent);

  return true;
}

关于android - 从ItemizedOverlay类开始 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4577043/

10-10 14:00