1

@Niko 解决的问题。谢谢 =)

看看下面的代码 =) =)

原始问题:我只能从地图中的气球中打开一项活动。我想根据气球打开不同的活动。可能吗?


HelloItemizedOverlay.class:

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class HelloItemizedOverlay extends ItemizedOverlay<MyOverlayItem>
{
    private static final String TAG = null;
    private ArrayList<MyOverlayItem> mOverlays = new ArrayList<MyOverlayItem>();
    private Context mContext;
    private Activity activity;

    public HelloItemizedOverlay(Drawable defaultMarker, Context context)
    {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
        this.activity = activity;
    }

    public void addOverlay(MyOverlayItem overlayitem) 
    {
        mOverlays.add(overlayitem);
        populate();
    }

    @Override
    protected MyOverlayItem createItem(int i)
    {
        return mOverlays.get(i);
    }

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

    @Override
    protected boolean onTap(int i)
    {
        // Intent Details = new Intent(mContext, Z_HH_flames.class);
        // mContext.startActivity(Details);
        Log.v(TAG, "inside onTap!!");
        Intent Details = new Intent(mContext, mOverlays.get(i).getActivityClassToOpen());
        mContext.startActivity(Details);

        /* OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();*/

        return true;
    }
}

mapView_HH.class:

public class MapView_HH extends MapActivity{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        Drawable drawable;
        HelloItemizedOverlay itemizedoverlay;
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        List<Overlay> mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
        itemizedoverlay = new HelloItemizedOverlay(drawable,this);

        GeoPoint point = new GeoPoint(19240000,-99120000);
        MyOverlayItem overlayitem = new MyOverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!", New_activity_1.class);

        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
        //mapView.getOverlays().add((Overlay)itemizedoverlay);
        //mapOverlays.add(itemizedoverlay);

        GeoPoint point2 = new GeoPoint(17385812,78480667);

        MyOverlayItem overlayitem2 = new MyOverlayItem(point2, "Namashkaar!", "I'm in Hyderabad, India!", New_activity_2.class);
        itemizedoverlay.addOverlay(overlayitem2);
        mapOverlays.add(itemizedoverlay);
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        return false;
    }
}

MyOverlayItem.class:

public class MyOverlayItem extends OverlayItem {

    private final Class<?> activityClassToOpen;

    public MyOverlayItem (GeoPoint loc, String title, String snippet, Class<?> activityClassToOpen) {
        super(loc, title, snippet);
        this.activityClassToOpen = activityClassToOpen;
    }

    public Class<?> getActivityClassToOpen() {
        return activityClassToOpen;
    }
}
4

1 回答 1

2

扩展 OverlayItem 类并在其中添加一些信息,单击时应打开哪些活动。然后在 onTap 中,您获得项目 mOverlays.get(index).getActivityClassToOpen() 并将其用于意图。

public class MyOverlayItem extends OverlayItem {

private final Class<?> activityClassToOpen;

public MyOverlayItem (GeoPoint loc, String title, String snippet, Class<?> activityClassToOpen) {
    super(loc, title, snippet);
    this.activityClassToOpen = activityClassToOpen;
}

public Class<?> getActivityClassToOpen() {
    return activityClassToOpen;
}
}

onTap 功能:

Intent Details = new Intent(mContext, mOverlays.get(i).getActivityClassToOpen());
于 2012-04-19T04:57:11.877 回答