1

@Nikoが解決した質問。ありがとう =)

以下のコードを見てください =) =)

元の質問: マップ内の気球から 1 つのアクティビティしか開くことができません。気球に応じてさまざまな活動を開きたいです。出来ますか?


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 に答える