【问题标题】:Simple google-map app not working in emulator简单的谷歌地图应用程序在模拟器中不起作用
【发布时间】:2017-11-10 20:59:45
【问题描述】:

我是 Android 开发的新手。我试图创建一个非常基本的简单应用程序来使用谷歌地图功能,但由于某种原因,地图没有按预期加载。附件是一张图片,显示了它是如何加载的。

下面是我的 google_maps_api.xml。我创建了一个新的 Google Maps API 密钥并处于活动状态。

<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:

    https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=9F:9B:0A:4E:CA:F7:7C:D3:83:42:BE:E3:91:18:00:6C:16:D7:B2:96%3Bcom.mycompany.googlemapsdemo

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

    Package name:
    9F:9B:0A:4E:CA:F7:7C:D3:83:42:BE:E3:91:18:00:6C:16:D7:B2:96

    SHA-1 certificate fingerprint:
    9F:9B:0A:4E:CA:F7:7C:D3:83:42:BE:E3:91:18:00:6C:16:D7:B2:96

    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" templateMergeStrategy="preserve" translatable="false">**mykey**</string>
</resources>

我们下面的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.mycompany.googlemapsdemo.MapsActivity" />

和 AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.googlemapsdemo">
    <!--
         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" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.
   READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <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>

</manifest>

MapsActivity.java

package com.mycompany.googlemapsdemo;

import android.os.Bundle;
import android.support.v4.app.BundleCompat;
import android.support.v4.app.FragmentActivity;


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

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

我在 Logcat 中看不到任何错误。一件有趣的事情是,每当我尝试放大/缩小或向上/向下滚动时,应用程序崩溃但又没有错误。我正在使用

Android Studio 3.0 构建 #AI-171.4408382,构建于 2017 年 10 月 20 日 JRE:1.8.0_152-release-915-b01 amd64 JVM:JetBrains s.r.o 的 OpenJDK 64 位服务器 VM 视窗 7 6.1

Logcat 消息

更新 3: 我不确定它是否与我在 Logcat 中看到的以下消息有关

11-14 11:57:18.471 2816-2974/com.example.mmuniyappa.googlemapdemonew W/DynamiteModule:找不到 com.google.android.gms.googlecertificates 的本地模块描述符类。

11-14 11:57:18.491 2816-2974/com.example.mmuniyappa.googlemapdemonew W/System:ClassLoader 引用了未知路径:/data/data/com.google.android.gms/app_chimera/m/00000001/ n/x86

另外,当我尝试放大/缩小或向上/向下滚动时,模拟器崩溃并弹出消息

模拟器:进程以退出代码 -1073741819 (0xC0000005) 结束

【问题讨论】:

  • 如果应用程序崩溃,我可以向您保证有错误。确保你没有吞下任何异常。此外,当您说它不起作用时,很难猜测。但是在模拟器中运行时要记住的一件事是,您必须向模拟器提供您预期的坐标。如果您在默认情况下期望它,它不会获取主机 PC 地址。根据我们的地图,您的地图被缩小了。尝试在 moveCamera 方法中添加缩放,例如 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(yourLocation,13))
  • 我在 samsung s7 上安装了相同的应用程序,它按预期工作。

标签: android google-maps


【解决方案1】:

清单文件中缺少版本元数据。

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

【讨论】:

  • 我尝试将它们添加到 AndroidManifest.xml 但没有成功
【解决方案2】:

参考Google Issues Tracker

它已通过 Google API 27 图像(第 3 版)修复。它应该可以解决 Google Maps 示例问题。

如果您使用的是 API 27,那么 Maps API 将无法在此处运行是一个已知问题。暂时尝试 API 26 等较低的 API 级别

如果任何问题仍然存在,请通过Google issue tracker 报告,他们将重新打开进行检查。

【讨论】:

    【解决方案3】:

    您确定 API 密钥与应用 ID 匹配吗,在

    Google API Console - Google Cloud Platform

    【讨论】:

    • 这是来自 Android Studio 的 Map 示例,可能是 API 密钥的问题或模拟器有问题。
    • 地图是部分可见的,这意味着这与 API Key 无关。这可能是网络问题,或者您可以尝试使用另一个具有另一个 Google API 系统映像的模拟器。
    • 我尝试了不同的模拟器 Nexus 4、6、6P,但没有运气。当我在三星 S7 上安装这个应用程序时,它运行良好。
    • 确定一个最终解决方案:在已安装的 SDK 中更新您的 google play 服务。
    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多