Rus*_*oso 14 android google-maps android-fragments supportmapfragment android-studio
我在Fragment中实现Google Map时遇到了麻烦.
这是我的片段类的一部分:
public class FragmentStoreFinderMap extends Fragment implements OnMapReadyCallback {
// Google Map
private GoogleMap googleMap;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(
R.id.map)).getMapAsync(this);
Run Code Online (Sandbox Code Playgroud)
我在getMapAsync(this)中收到错误.我告诉不兼容的类型.
它说:
必需:com.google.android.gms.map.GoogleMap
发现:无效
顺便说一句,这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Les*_* L. 51
在您的XML布局中,您应该这样做
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在你的片段里面实现这个
private MapView mapView;
private GoogleMap googleMap;
Run Code Online (Sandbox Code Playgroud)
如果没有onCreateView,则覆盖onCreateView,就像下面的代码一样
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.your_layout, container, false);
}
Run Code Online (Sandbox Code Playgroud)
在onViewCreated()内部覆盖onViewCreated()
mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment
Run Code Online (Sandbox Code Playgroud)
当你已经在你的片段中实现OnMapReadyCallback时,将这个/代码放在这样的代码中
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
}
Run Code Online (Sandbox Code Playgroud)
希望这能帮助你.
| 归档时间: |
|
| 查看次数: |
33147 次 |
| 最近记录: |