【问题标题】:Google map not showing in android activity?谷歌地图没有显示在安卓活动中?
【发布时间】:2017-06-05 09:05:15
【问题描述】:

在我的 android 应用程序中,我想要一个 MainActivity 和一个占据整个屏幕的谷歌地图。

我的问题是google map不显示地图,只有左下角的google logo:

以下是主要活动的相关部分,其中应显示地图:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback,
        NavigationView.OnNavigationItemSelectedListener {

    private GoogleMap mMap;

    private FragmentManager mFragmentManager;

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

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        setupLayout();

        mFragmentManager = getSupportFragmentManager();
    }

    /**
     * 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));
    }

这是包含谷歌地图片段的主要活动内容的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.purringcat.stray.view.activity.MainActivity"/>

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.purringcat.stray">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:name=".Stray"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".view.activity.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".view.activity.LoginActivity"
            android:label="@string/title_activity_login"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
        <activity android:name=".view.activity.SignUpActivity">
        </activity>
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key"/>

    </application>

</manifest>

【问题讨论】:

  • 这通常是由不正确的地图集成(错误的令牌/密钥/证书)引起的
  • 会不会和我没有使用FragmentActivity有关?
  • 1) 确保您已在项目中添加 API 密钥。 2) 确保您的 api 在 Google 控制台上启用。 3) 确保您提供的 SHA-1 是正确的。
  • FragmentActivity 我不认为这是相关的。我确定您使用了错误的 API 密钥,或者为地图生成了错误的指纹。
  • 似乎没有问题与 api_key 或与地图集成有关。我对正在发生的事情感到非常困惑,因为我似乎没有错误或错误。

标签: android android-fragments google-maps-android-api-2


【解决方案1】:

您需要从 google 控制台为您的项目启用 google map api。而且你还需要从控制台下载json文件。

1.转至google console

2。点击您的项目。

3.单击左侧菜单面板中的库部分。见下图

  1. 在 google maps api 中,点击 Google maps android api

  2. 比启用它。

  3. 现在转到firebase console

  4. 点击您的项目并转到项目设置并下载 json 文件。

  5. 在您的应用程序中添加该 json 文件。

【讨论】:

  • 我知道,我只是在谷歌控制台上启用了它,然后将 api 密钥添加到清单中。
  • 如何启用地图?
  • 你也下载了json文件?
  • 我在项目的 app 文件夹中有 google-services.json 文件。
  • 那么它应该可以工作你的代码是完美的。一旦尝试将 appcompactActivity 更改为 FragmentActivity。我认为它不会影响,但请尝试一次
【解决方案2】:

将此权限添加到清单

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />

或在 google 控制台中检查是否启用 API

或检查您的 google_api_key

【讨论】:

  • 我已经包含了包含清单的问题。我已启用 API 并添加了以 AIz 开头的 api 密钥
【解决方案3】:

我刚刚检查了像你这样的谷歌地图。

只需将您的代码与我的代码进行比较即可。

MainActivity.java

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));
}

}

和清单

<?xml version="1.0" encoding="utf-8"?>

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

注意:你必须在google中创建一个项目

click here to visit the google console

在 google_maps_api 中替换您的键值

<resources>


<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Your Key</string>

希望这对你有用。

PS:您可以从 android studio 生成谷歌地图活动。

【讨论】:

    【解决方案4】:

    AndroidManifest.xml 文件的application 部分中包含以下代码:

    <uses-library
        android:name="org.apache.http.legacy"
        android:required="false" />
    

    请参阅文档here

    【讨论】:

      【解决方案5】:

      Google 地图喜欢获取不同的活动生命周期状态。所以你应该发送它:map_view.onCreate(savedInstanceState)map_view.onResume()。它还喜欢onPause()onLowMemory()onSaveInstanceState(outState)onDestroy()(如果它们出现)。

      【讨论】:

        【解决方案6】:

        点击限制键,然后在API限制->创建api时不要限制键

        【讨论】:

          【解决方案7】:

          1) 请更新谷歌播放服务

          2) 请在 url 中附加您的 api 密钥

          例如:-"https://maps.googleapis.com/maps/api/directions/" + 输出 + "?" + 参数 + "&key=" + MY_API_KEY;

          【讨论】:

            【解决方案8】:

            确保您已授予这些权限

            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
                <uses-permission android:name="android.permission.INTERNET" />
            
            

            并检查 AVD 的 WiFi 是否打开并且您的 PC 是否已连接到互联网。

            我遇到了同样的问题,按照上述步骤我能够解决我的问题。

            【讨论】:

              【解决方案9】:

              如果活动正在打开并且即使您提供 Google_map_key 地图也不可见,那么对我有用的一个解决方案是我通过复制 google_maps_api.xml 中的链接并粘贴来创建一个新的 Google 地图 API KEY浏览器。选择一个项目或创建一个项目,它将使您进入准备好 google API KEY 的页面,将其复制并粘贴到说明中给出的位置。它会起作用。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-05-23
                相关资源
                最近更新 更多