0

如何在 Android MapView 中从 Pin/Marker 打开一个新的 Activity?

像这样,然后我想点击一个标记,它应该打开一个新活动,我该怎么做?

我的代码:

 private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
                      (int)(lon*1000000.0)));

}

私有类 SitesOverlay 扩展 ItemizedOverlay { private List items=new ArrayList();

public SitesOverlay(Drawable marker) {
  super(marker);

  boundCenterBottom(marker);

  items.add(new OverlayItem(getPoint(47.17612,
          8.499727),
                            "TCS", "TCS Camping Brüggli"));
  items.add(new OverlayItem(getPoint(47.57612,
          8.499727),
                            "",
                            ""));
  items.add(new OverlayItem(getPoint(00.765136435316755,
                                      00.97989511489868),
                            "",
          ""));
  items.add(new OverlayItem(getPoint(00.0686417491799,
                                      00.01572942733765),
                            "",
                    ""));

  populate();
}

@Override
protected OverlayItem createItem(int i) {
  return(items.get(i));
}




@Override
protected final boolean onTap(int i) {
         //Do you job here.
       startActivity(new Intent(MaptestActivity.this,SettingsActivity.class));
    return false;
4

1 回答 1

1

https://github.com/jgilfelt/android-mapviewballoons

在这个链接中,我们有一个包“mapviewballoons.example.simple”

在这个“mapviewballoons.example.simple”包中有一个类“SimpleItemizedOverlay.java”对这段代码进行了一些修改。

包mapviewballoons.example.simple;

公共类 SimpleItemizedOverlay 扩展 BalloonItemizedOverlay {

private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
private Context c;

public SimpleItemizedOverlay(Drawable defaultMarker, MapView mapView) {
    super(boundCenter(defaultMarker), mapView);
    c = mapView.getContext();
}

public void addOverlay(OverlayItem overlay) {
    m_overlays.add(overlay);
    populate();

}

@Override
protected OverlayItem createItem(int i) {
    return m_overlays.get(i);
}

@Override
public int size() {
    return m_overlays.size();
}

@Override
protected boolean onBalloonTap(int index, OverlayItem item) {
    Toast.makeText(c, "onBalloonTap for overlay index " + index,
            Toast.LENGTH_LONG).show();

    c.startActivity(new Intent(c.getApplicationContext(), NewActivity.class));

    return false;
}

}

这是修改代码,

@Override
protected boolean onBalloonTap(int index, OverlayItem item) {
    Toast.makeText(c, "onBalloonTap for overlay index " + index,
            Toast.LENGTH_LONG).show();

    c.startActivity(new Intent(c.getApplicationContext(), NewActivity.class));

    return false;
}


  Call different activities,


@Override
protected boolean onBalloonTap(int index, OverlayItem item) {
    Toast.makeText(c, "onBalloonTap for overlay index " + index,
            Toast.LENGTH_LONG).show();
            if (index == 0)
            {
    c.startActivity(new Intent(c.getApplicationContext(), NewActivity.class));
             } else {
              c.startActivity(new Intent(c.getApplicationContext(), secondNewActivity.class));

    return false;
}
于 2012-08-02T10:48:06.180 回答