【问题标题】:Android: setting up a google map class. bit of advice requiredAndroid:设置谷歌地图类。需要一点建议
【发布时间】:2010-05-28 17:54:35
【问题描述】:

好的,这就是我所拥有的。

Button anandabutton = (Button) findViewById(R.id.anandaAddressButton);
anandabutton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(),MapClass.class);
        startActivityForResult(myIntent,0);

    }

});

这个方法打开了我的 MapClass 类,目前我刚刚设置为显示一个地方的位置。

但是我有很多按钮,而不是为每个按钮制作很多不同的 mapClass 类,我想知道我可以只使用一个类,并且根据按下的按钮“id”,它会检查一个“ if语句',然后将正确的坐标放入方法中以显示地图。这比编写 20-30 个类要简洁得多。

我不确定我的解释是否正确,如果不告诉我。

感谢您的帮助。

这是我现在的地图类...

public class MapClass extends MapActivity {

MapView mapView;
MapController mc;
GeoPoint p;

@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    int button = getIntent().getExtras().getInt("button", -1);
    switch(button){
    case DundrumSelector.BUTTON1:
        handleCoordinates("53.288719","-6.241179");
        break;
    case DundrumSelector.BUTTON2:
        handleCoordinates("53.288719","-6.241179");
        break;
    }
}




private void handleCoordinates(String l, String b){


    mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
    View zoomView = mapView.getZoomControls();

    zoomLayout.addView(zoomView,
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

    mc = mapView.getController();
    String coordinates[] = {l, b};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
            (int) (lat*1E6),
            (int) (lng*1E6));

    mc.animateTo(p);
    mc.setZoom(17);
    mapView.invalidate();


}

然后我的其他活动是

public static final int BUTTON1 = R.id.anandaAddressButton;

Button anandabutton = (Button) findViewById(R.id.anandaAddressButton);
anandabutton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(),MapClass.class);
        myIntent.putExtra("button", BUTTON1);
        startActivityForResult(myIntent,0);

    }

});

由于某种原因,当我尝试单击列表中除 BUTTON1 之外的任何其他项目时,程序崩溃了。我不得不取出 BUTTON2、BUTTON3 等,因为如果我尝试进入他们的视野,它只会不断崩溃。但是当我注释掉这个 onClick 方法时,它们都可以正常工作吗?

你们有任何意见吗?你明白我的问题吗? 谢谢。

【问题讨论】:

    标签: android google-maps button onclick


    【解决方案1】:

    如果我理解正确,您希望始终启动相同的 Intent,但在 Intent 的接收方,即启动的 Activity 上,您希望根据按下的按钮使用不同的方法。

    我会建议to add a constant to the Intent,具体取决于您按下的按钮,并在 MapClass 中检查它是哪个常量,然后您做出反应:

    public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(),MapClass.class);
            myIntent.putExtra("button", BUTTON1);
            startActivityForResult(myIntent,0);
     }
    

    BUTTON1 和 BUTTON2 是常量 int,您已在 Acitivty 类中定义了与您要添加的按钮相对应的值。

    在 MapClass 的 onCreate() 方法中,您可以执行以下操作:

    int button = getIntent().getExtras().getIntExtra("button", -1);
    switch(button) {
       case BUTTON1:
       //do something
       break;
       case BUTTON2:
       //do method for button 2
       break;
       default:
       //do another thing
       break;
    }
    

    也涵盖您的编辑:

    如果只有坐标改变,我会做一些链接:

    int button = getIntent().getExtras().getIntExtra("button", -1);
    switch(button) {
       case BUTTON1:
       handleCoordinates("53.288719","-6.241179");
       break;
       case BUTTON2:
      handleCoordinates("23.288719","23.241179"
       break;
       default:
       //do another thing
       break;
    }
    
    
    
    private void handleCoordinates(String l, String b) {
    mapView = (MapView) findViewById(R.id.mapView);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
        View zoomView = mapView.getZoomControls();
    
        zoomLayout.addView(zoomView,
                new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT)); 
            mapView.displayZoomControls(true);
    
        mc = mapView.getController();
        String coordinates[] = {l, b};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
    
        p = new GeoPoint(
                (int) (lat*1E6),
                (int) (lng*1E6));
    
        mc.animateTo(p);
        mc.setZoom(17);
        mapView.invalidate();
    }
    

    【讨论】:

    • 这很好,但如果用户在加载 MapClass 时按下后退按钮然后按下另一个按钮,它将不起作用,第二次按下不会调用 MapClass.onCreate(),因此显示按下返回前最后显示的坐标。
    • 好的,谢谢,是的,您对我的理解是正确的。我现在正在努力解决它。我是否必须在 MapClass 以及其他活动类中定义 BUTTON1 BUTTON2 等?
    • 我只会在其他活动类中将其定义为公共常量。那么您可以通过 OtherClass.BUTTON1 @ricardo 访问它:嗯,尽管他启动了ActivityForResult()?
    • 好的,谢谢,我明白了。好的另一件事......而不是复制按下每个按钮时我将调用的长方法,我可以在每个“案例”下放入更短的方法,比如坐标更改或类似的东西?因为如果我在每个按钮案例下复制整个方法,该类将变得巨大!谢谢大家。
    • 嗯,没有代码很难判断。但尽量将所有常用代码放在一个方法中,每个按钮不同的部分代码也放在单独的方法中
    【解决方案2】:

    为了补充 Sebi 的回答和您在 cmets 中提出的问题,您要做的是创建一个处理程序(可以在您的 onCreate() 方法中):

    OnClickListener buttonListener = new OnClickListener(){
        public void onClick(View v) {
            if(!(v instanceof Button))
               //throw error, not supposed to happen
            Button b = (Button)v;
            int id = b.getId();
            int code; //used to know when your activity comes back (if you need to)
            int lat;
            int long;
            switch(id) {
               case button1.getId():
                  //set code, lat and long for button1
               case button2.getId():
                  //set code, lat and long for button2
            ...
            }
            Bundle bdl = new Bundle();
            bdl.putDouble("latitude", lat);
            bdl.putDouble("longitude", long);
            Intent myIntent = new Intent(view.getContext(),MapClass.class);
            myIntent.putExtras(bdl);
            startActivityForResult(myIntent,code);
    }
    button1.setOnClickListener(buttonListener);
    button2.setOnClickListener(buttonListener);
    ...
    

    【讨论】:

    • 谢谢。顺便提一句。如果你测试 instanceof,则不需要测试 null
    • 你们非常感谢。一旦我有机会实施它,如果我有任何问题,我会稍后回到这里! :)
    • 我又遇到了一些麻烦,我编辑了我的第一篇文章。任何帮助表示赞赏。非常感谢。
    • 看看你是如何将额外的东西传递给你的地图活动的。请参阅我的代码,了解如何创建 Bundle 并添加到意图中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多