【问题标题】:Changing StreetView<->Satellite Google Maps Android更改街景<->卫星谷歌地图 Android
【发布时间】:2014-05-08 19:29:16
【问题描述】:

我想通过菜单按钮在街景和卫星之间切换我的 GoogleMaps 视图。

这是我的代码:

public boolean onCreateOptionsMenu(Menu menu){

    menu.add(0, 0, 0, "StreetView");
    menu.add(0, 0, 1, "Satellite");

    return true;
}

public boolean onOptionsItemSelected (MenuItem item){

    switch (item.getItemId()){
        case 0:
            mapView.setStreetView(true);
        return true;

        case 1 :
            mapView.setSatellite(true);
        return true;

    }

    return false;
}

不会工作..我做错了什么?

谢谢, 前缀

【问题讨论】:

    标签: android view maps


    【解决方案1】:

    当您说它不起作用时,我们真的需要更多信息来尝试帮助您!它是否崩溃,它是否停留在街道/卫星视图或只是法线地图等,尝试提供更多信息,如果它崩溃,则发布 logcat 的副本。

    我认为你所缺少的只是这条线:

    (编辑:我只是尝试过没有调用 invalidate 并且它可以工作,所以它必须是菜单按钮 ID)

    mapView.invalidate();
    

    您需要调用它以使 mapView 自行刷新,因此每次更改 mapView 设置时都要调用它。


    如果这不起作用,则可能是您的开关中无法识别按钮的 ID,因此请尝试将您的菜单设置为 xml 文件 int res/menu/,例如:

    <?xml version="1.0" encoding="utf-8"?>
    <menu
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:title="Street View" android:numericShortcut="1" android:id="@+id/mapStreet" ></item>
      <item android:title="Sat View" android:numericShortcut="2" android:id="@+id/mapSat"></item>
    </menu>
    

    然后将你的代码修改为:

    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        MenuInflater oMenu = getMenuInflater();
        oMenu.inflate(R.menu.mapsmenu, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.mapStreet:
             mapView.setStreetView(true);
             mapView.setSatellite(false);
             mapView.invalidate();
             return true;
    
        case R.id.mapSat:
             mapView.setSatellite(true);
             mapView.setStreetView(false);
             mapView.invalidate();
             return true;
        }
        return false;
    }
    

    【讨论】:

    • Uff...我将我的问题发布到快速...您的第二个提示,以其他方式设置菜单。我只是“烤”了 --item.getItemId()-- 并认识到,它在两个菜单按钮上都是 0 .. 我的错在这里:menu.add(0, 1, 1, "Satellite");对不起,伙计们..:/谢谢你,肯尼
    • 使用 xml 设置菜单会让生活更轻松,因为 id 都已注册,如果您想添加额外的按钮,这非常简单。
    【解决方案2】:

    不要使用 MapView。 使用 GoogleMap 并做

    GoogleMap map;
    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    

    【讨论】:

    • 迟到了 3 年,但无论如何都是 ty。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多