我有一个包含 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/