我想在mapview中打开两个气球来打开不同的活动。这可能吗?

时间:2012-04-19 04:20:02

标签: android google-maps

@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;
    }
}

1 个答案:

答案 0 :(得分: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());