1

我想知道如何从我的自定义 ItemizedOverlay 类中的 AlertDialog 中找出哪个活动(类名)启动了我的自定义 ItemizedOverlay 类。我有不同地方的 MapView 的不同活动。加载 MapView 时,所有活动都会自动启动 ItemizedOverlay 类。因此,我不能把额外的意图。

任何人都知道这是否可能?

这是我的 ItemizedOverlay 构造函数类(请忽略注释部分和 alertdialog 消息部分):

public class CustomItemisedOverlay extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

private Context context;

public CustomItemisedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

public CustomItemisedOverlay(Drawable defaultMarker, Context context) {
    this(defaultMarker);
    this.context = context;
}

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mapOverlays.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return mapOverlays.size();
}

//AlertDialog for driving directions here
@Override
protected boolean onTap(int index) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    //Title of AlertDialog
    dialog.setTitle("Driving Directions");
    //Message of AlertDialog
    String className = getClass().getSimpleName().toString();
    dialog.setMessage(className);
    //Positive Button
    dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // Handle launch of driving directions here
            /*String tappedLong = null;
            String tappedLat = null;
            String className = this.getClass().getSimpleName().toString();
            if(className == "amkActivity") {
                tappedLong = "1.363414";
                tappedLat = "103.9370256";

            } else if (className == "bedokActivity") {
                tappedLong = "1.3248498";
                tappedLat = "103.9370256";
            }


            Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
                    Uri.parse("http://maps.google.com/maps?daddr=" + tappedLat + "," + tappedLong));
            context.startActivity(intent);*/

        }
    });

    //Negative Button
    dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.cancel();
        }
    });
    //Display AlertDialog when tapped
    dialog.show();
    return true;
}

public void addOverlay(OverlayItem overlay) {
    mapOverlays.add(overlay);
    this.populate();
}

}

4

2 回答 2

0

I'm not too sure how this works, but to get the name of the activity that started the CustomItemisedOverlay class, use

context.getClass().getSimpleName();

For example, I want to put the activity name into a String called className, I do this:

String className = context.getClass().getSimpleName().toString();

The reason why context is used is because of this line of code(can be found above):

private Context context;

Thus context here refers to the context of the activity that started the CustomItemisedOverlay class

于 2012-08-07T02:07:57.043 回答
0

利用

mapView.getContext();

在 itemizedoverlay 的构造函数中..

这将返回地图视图页面的上下文...

于 2012-08-06T08:18:35.743 回答