【发布时间】:2013-04-24 03:30:29
【问题描述】:
我有一个包含 MapView 的片段。我在 XML 文件中添加了这个视图,如下所示:
<?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;
}
.
.
.
}
我想添加以将选项添加到我的地图视图。根据GoogleMapOptions 中提到的内容,“这些选项可以在以编程方式(而不是通过 XML)向应用程序添加地图时使用。如果您使用的是 MapFragment,则可以在使用静态工厂时传递这些选项方法 newInstance(GoogleMapOptions)。如果您使用的是 MapView,则可以使用构造函数 MapView(Context, GoogleMapOptions) 传递这些选项。"最后是我的情况,“如果您使用 XML 添加地图,那么您可以使用自定义 XML 标记应用这些选项。”
我没有找到任何示例来展示如何通过 XML 添加选项。我想将 zOrderOnTop="true" 添加到我的 XML 代码中。
任何建议将不胜感激。谢谢
【问题讨论】:
-
我猜你还没有实现其他必需的方法。这就是它发生的原因。您无需编写“mapView.onResume();”在 onCreateView() 方法中。确保在 onCreateView() 之后有以下方法:
-
*覆盖 public void onResume() { super.onResume(); if(mapView != null) mapView.onResume(); } *覆盖 public void onPause() { super.onPause(); if(mapView != null) mapView.onPause(); } *覆盖 public void onDestroy() { super.onDestroy(); if(mapView != null) mapView.onDestroy(); }
-
*覆盖 public void onSaveInstanceState(Bundle outState) { if(mapView != null) mapView.onSaveInstanceState(outState); super.onSaveInstanceState(outState); } *覆盖 public void onLowMemory() { if(mapView != null) mapView.onLowMemory(); super.onLowMemory(); }
-
请用@替换*。我不能使用它,因为编辑说不允许超过 1 个 @ :(
-
请看stackoverflow.com/a/14114428/513413我认为它解决了你的问题。
标签: android android-mapview google-maps-android-api-2