Android,如何从 XML 布局添加 Google map 选项?

标签 android android-mapview google-maps-android-api-2

我有一个包含 MapView 的 fragment 。我在 XML 文件中添加了这个 View ,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    map:uiCompass="true"
    android:background="#00000000" />

我已经将它链接到我的代码,如下所示:

public class HotSpotsFragment extends MainFragment implements LocationListener {

    private static final String TAG = "HotSpotsFragment";
    private Context context;
    private LocationManager locationManager;
    private MapView mapView;
    private GoogleMap googleMap;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // create view
        View view = inflater.inflate(R.layout.fragment_hot_spots, container, false);

        // Getting context
        context = getActivity().getApplicationContext();

        // Make sure user's device supports Google play services
        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            Log.e(TAG, "Google play service is not available.");
            Toast.makeText(getActivity().getApplicationContext(), R.string.hot_spots_not_supported, Toast.LENGTH_LONG).show();
            return null;
        }

        Log.i(TAG, "Google play service is available.");

        mapView = (MapView) view.findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);

        googleMap = ((MapView) view.findViewById(R.id.mapView)).getMap();
        if(googleMap == null) {
            Toast.makeText(getActivity().getApplicationContext(), R.string.hot_spots_not_supported, Toast.LENGTH_LONG).show();
            return null;
        }

        Log.i(TAG, "View created");

        return view;
    }
.
.
.
}

我想添加选项到我的 map View 。基于 GoogleMapOptions 上提到的内容,“当以编程方式(而不是通过 XML)将 map 添加到您的应用程序时,可以使用这些选项。如果您使用的是 MapFragment,则可以使用静态工厂方法 newInstance(GoogleMapOptions) 传递这些选项。如果您使用一个 MapView,您可以使用构造函数 MapView(Context, GoogleMapOptions) 传递这些选项。”最后是我的案例,“如果您使用 XML 添加 map ,那么您可以使用自定义 XML 标签应用这些选项。

我没有找到任何示例来说明如何通过 XML 添加选项。我想将 zOrderOnTop="true"添加到我的 XML 代码中。

如有任何建议,我们将不胜感激。谢谢

最佳答案

就这么简单

<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    map:uiCompass="true"
    map:zOrderOnTop="true"
    map:uiZoomControls="true"
    android:background="#00000000" />

但是新问题是添加 map:zOrderOnTop="true" 会从屏幕上移除叠加对象,例如 ZoomIn/ZoomOut :( 有关详细信息,请参阅 this link .

关于Android,如何从 XML 布局添加 Google map 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16182880/

相关文章:

android - 实现 "X"以关闭标记信息窗口

android - 测试一个点是否在没有实际多边形对象的多边形中。 Android 谷歌地图 v2

java - 在 Google map 方向中将字符串距离转换为双倍

android - EditText setText 不显示

android - 如何确定当前可视区域中是否显示地理点?

ios - 删除第一个注释-MapView

android - 将数百个标记绘制到 MapView 上

android - 在 Android 智能手机中同时打开 Wifi 和 WifiHotspot

java - 使用 Gson() 创建一个 "flat"JSON

android - 在谷歌眼镜上使用 zxing