Google地图未显示Android

kev*_*998 0 java android google-maps google-maps-android-api-2

我想在我的应用程序中实现google maps。我添加了一个Google Maps活动并创建了一个密钥。我没有在其他地方更改代码中的任何内容。我应该工作,但是不可以。

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));
}
Run Code Online (Sandbox Code Playgroud)

}

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" />
Run Code Online (Sandbox Code Playgroud)

Moh*_*tar 7

即使您遵循了官方文档中的步骤,或者您已经完成了Android Studio中的步骤,但是仍然看不到地图,请按照以下步骤操作,以确保API密钥是否有问题:

  1. 使用logcat中的“ Google Maps”进行过滤。
  2. 您将看到一条错误消息,其中包含正确的程序包名称和SHA密钥,应该在API密钥中按原样显示
  3. 转到Google Cloud Console-> API-> Credentails,然后对上述两者进行交叉验证,必要时进行更正。
  4. 等待10到15分钟,以反映出API密钥更改