简体   繁体   中英

Google maps not showing Android

I want to implement google maps in my app. I added a google maps activity and created a key. I didn't change anything in the code elsewhere. I should work but it doesn't.

MapsActivity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kevin.map.MapsActivity" />

Even after you follow steps from Official documentation or you're done with steps from Android Studio, and you're still not able to see the map, follow these steps to ensure whether it is a problem with API key or not:

  1. Filter with 'Google Maps' in your logcat.
  2. You will see an error message, which contains the correct package name & SHA key which should be present as it is in the API Key
  3. Go to Google Cloud Console -> API -> Credentails, and cross-verify both the above, correct if necessary.
  4. Wait for 10-15 minutes for the API Key changes to be reflected

In addition to other posts, you may refer with this thread . The reason may be the API key used was created with the wrong keystore. You need to make sure you use your debug keystore when you create an API key in the Google API console. Based from this link , when using GoogleMaps for Android, you need two keys - debug and release. The "debug" key is kind of a misleading term. This key is also to be used when you develop the app. So essentially, use the debug key for development, testing, debugging. When you're ready to launch the app to Market, set the android:debuggable="false" in the AndroidManifest.xml and use the Signed API key.

To work with Google Maps you must:

  1. Add logic to the Fragment or Activity.
  2. Add the map fragment element to the layer.
  3. Obtain an API Key (you probably need to associate the key to your debug version and your signed version of the app, everyone will have a different signature but both can share the same key).
  4. Configure the application manifest. Here, to use Google Maps you will need: Internet Access, Open GL specifications, if the map will be stored locally you sill need access to the device storage too, as the map libraries are included as part of the google play services you need to configure this too.

At the manifest level:

<!-- Location if you will use this -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

At the application level inside the manifest:

    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="<YOUR MAP API KEY>" />
  1. Check your google API key that you have entered is correct or not and that key is enabled or not
  2. Did you allow Location access permission, if not then go in the mobile app setting and click on the permission option and allow location access permission and then run your code it will run

确保您也已将地图密钥添加到发布文件夹中(app> src> release> res> values> google_map_api.xml)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM