Google Maps API加载Google徽标但没有地图

时间:2019-03-27 14:02:09

标签: android google-maps

我正在使用大约2年前编写的代码库(因此,我想知道API端的某些内容是否发生了更改而导致此问题)。还知道它已经工作了两年,所以我无法想象这是代码本身的问题...我想我只是没有正确设置API。

无论如何,我有Google Maps API在Fragment内创建地图。我创建了一个新的API密钥,并相信已按照这些说明正确设置了新密钥。每当我运行该应用程序时,Google徽标就会加载到该片段的左下角。这向我表明,与API进行了一些通信。但是,没有地图显示。只有米色的屏幕。

我尝试过几次API设置过程,但运气不佳。阅读了其他文章,指出AndroidManifest.xml中的某些更改后,我尝试更改存储在标记中的信息,但没有任何改变。

MapViewFragment.java

public class MapViewFragment extends Fragment {

    MapView mMapView;
    private GoogleMap googleMap;
    final int MY_MAPS_REQUEST = 31415;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.map_view, container, false);

        mMapView = (MapView) rootView.findViewById(R.id.mapView);
        //final ImageButton returnButton = (ImageButton) rootView.findViewById(R.id.returnLocationButton);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume(); // needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;
                mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                // For showing a move to my location button
                /*String[] permissionRequests = new String[1];
                permissionRequests[0] = Manifest.permission.ACCESS_FINE_LOCATION;
                ActivityCompat.requestPermissions(getActivity(),permissionRequests,0);*/
                if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // Should we show an explanation?
                    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                            Manifest.permission.ACCESS_FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                            Manifest.permission.ACCESS_COARSE_LOCATION)) {

                        // Show an explanation to the user *asynchronously* -- don't block
                        // this thread waiting for the user's response! After the user
                        // sees the explanation, try again to request the permission.

                    } else {
                        // No explanation needed, we can request the permission.
                        ActivityCompat.requestPermissions(getActivity(),
                                new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                                MY_MAPS_REQUEST);

                        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                        // app-defined int constant. The callback method gets the
                        // result of the request.
                    }
                }
                else{
                    googleMap.setMyLocationEnabled(true);
                }

                /* some points dropped here, but removed because its not relevant */ 


                // For zooming automatically to the location of the marker
                final CameraPosition cameraPositionWetlands = new CameraPosition.Builder().target(centerPoint).zoom(15).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPositionWetlands));
            }
        });

        return rootView;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_MAPS_REQUEST: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay!
                    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        googleMap.setMyLocationEnabled(true);
                    }
                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mMapView != null) {
            mMapView.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(mMapView != null) {
            mMapView.onPause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mMapView != null) {
            mMapView.onDestroy();
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if(mMapView != null) {
            mMapView.onLowMemory();
        }
    }
}

map_view.xml

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

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MapViewFragment"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <ImageButton
        android:id="@+id/returnLocationButton"
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:layout_marginTop="60dp"
        android:layout_marginRight="12dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/rounded_rectangle"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/ic_action_return_location" />

</RelativeLayout>

google_maps_api.xml

<resources>
    <!--
    TODO: Before you run your application, you need a Google Maps API key.

    To get one, follow this link, follow the directions and press "Create" at the end:


    You can also add your credentials to an existing key, using these values:

    Package name:
    ***
    SHA-1 certificate fingerprint:
    ***

    Alternatively, follow the directions here:
    https://developers.google.com/maps/documentation/android/start#get-key

    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
    string in this file.
    -->
    <string name="google_maps_key" translatable="false">AIza************************************</string>
</resources>

更新:刚刚发现此堆栈跟踪隐藏在我的运行时控制台中。

E/Google Maps Android API: Authorization failure.  Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
E/Google Maps Android API: In the Google Developer Console (https://console.developers.google.com)
    Ensure that the "Google Maps Android API v2" is enabled.
    Ensure that the following Android Key exists:
        API Key: AIza***********************************
        Android Application (<cert_fingerprint>;<package_name>): cert:finger:print;com.project.name

0 个答案:

没有答案