【问题标题】:Google Map doesn't load in Fragment谷歌地图不加载片段
【发布时间】:2016-07-31 02:35:19
【问题描述】:

我有一个带有两个窗口的滑动标签片段,第一个包含谷歌地图视图。片段工作正常,谷歌片段也显示出来,但地图没有加载。仅显示带有网格线和谷歌徽标的空白地图。我的代码有什么问题?

public class CardFragment extends Fragment implements OnMapReadyCallback{

private static final String ARG_POSITION = "position";
private int position;

GoogleMap mMap;
Geocoder coder;

public static CardFragment newInstance(int position) {
    CardFragment f = new CardFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

public static MapsFragment newInstance(String s) {
    Bundle args = new Bundle();
    args.putString("value", s);
    MapsFragment result = new MapsFragment();
    result.setArguments(args);
    return result;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    position = getArguments().getInt(ARG_POSITION);      
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(position == 0){
        View rootView = inflater.inflate(R.layout.maps_frag,container,false);
        ButterKnife.inject(this, rootView);
        ViewCompat.setElevation(rootView, 50);

        if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getContext()) == ConnectionResult.SUCCESS) {

            MapView mapView = (MapView) rootView.findViewById(R.id.mymap);
            mapView.onCreate(savedInstanceState);
            mapView.getMapAsync(this);
        }

        return rootView;
    } else {
        View rootView = inflater.inflate(R.layout.list_frag,container,false);
        ViewCompat.setElevation(rootView, 50);
        return rootView;
}


@Override
public void onMapReady(final GoogleMap googleMap) {

    mMap = googleMap;
    coder = new Geocoder(this.getContext());
    p1 = null;
}

layout maps_frag

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardBackgroundColor="@android:color/white">

<com.google.android.gms.maps.MapView     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swagishmap"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.locustapps.ingredientsapp.MapsActivity"/>

</android.support.v7.widget.CardView>

</FrameLayout>

【问题讨论】:

  • 白你必须使用SupportMapFragment 答案是正确的。

标签: java android xml google-maps android-fragments


【解决方案1】:

我不明白您要对 MapView 执行什么操作,名称为 com.google.android.gms.maps.SupportMapFragment。这没有任何意义。

如果你想在 Fragment 中放置一个 Map,你应该使用 MapFragmentSupportMapFragment 如果使用 Fragment 的兼容性库。这些片段将管理片段空间中地图的显示。

ApiDemos 示例项目提供了如何使用它的示例。 Here is a layout 声明了一个 SupportMapView 片段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多