谷歌地图片段显示地图容器,但没有地图显示

时间:2018-12-08 11:03:00

标签: java android google-maps fragment

在制作此项目时,我跟踪了一个视频,但是该视频已经存在3年了,因为预期的依赖关系已经过时,所以我进行了一些更改,没有发现错误。

该应用程序运行平稳,但我要定位的唯一功能却没有运行,那就是Google地图。

这是我的礼物,以防万一,

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation files('libs/aapt2-3.2.1-4818971-windows.jar')
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'

Android清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.blogspot.skooltchdev.tabbedapplicationmaster">
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/MyMaterialTheme">

            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value=" My_API_KEY"/>
        </application>
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>
</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new FirstFragment(), "Map");
    adapter.addFragment(new SecondFragment(), "fragment two");
    adapter.addFragment(new ThirdFragment(), "fragment three");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
}

FirstFragment.java

public class FirstFragment extends Fragment implements OnMapReadyCallback {

GoogleMap mGoogleMap;
MapView mMapView;
View mView;

public FirstFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


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

    mView = inflater.inflate(R.layout.first_fragment, container, false);
    return mView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mMapView = (MapView) mView.findViewById(R.id.map);
    if(mMapView !=null){
        mMapView.onCreate(null);
        mMapView.onResume();
        mMapView.getMapAsync(this);
    }

}

@Override
public void onMapReady(GoogleMap googleMap) {
    MapsInitializer.initialize(getActivity());
    LatLng Home = new LatLng(10.263392,123.82489109999999);
    mGoogleMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions().position(Home).title("my home"));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(Home));
}

}

first_fragment.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"
tools:context=".FirstFragment">

<com.google.android.gms.maps.MapView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map">

</com.google.android.gms.maps.MapView>

</FrameLayout>

我已经尝试过在Google云端平台上启用结算功能(因为它们最近做了很多更改),多次更改了api(在console.dev ...上),清理并重建了。仍然会显示没有标记和地图的Google地图(只是纯白色,底部没有google,只是纯白色)。

1 个答案:

答案 0 :(得分:0)

尝试一下 首先启动地图 在mapview.oncreate上不要发送null

MyUserControl1

建议:与其使用moveCamera而不是animateCamera,不如使用它