简单的谷歌地图应用程序无法在模拟器中工作

时间:2017-11-10 20:59:45

标签: android google-maps

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

enter image description here

以下是我的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 Build#AI-171.4408382,建于2017年10月20日 JRE:1.8.0_152-release-915-b01 amd64 JVM:JetBrains s.r.o的OpenJDK 64位服务器VM Windows 7 6.1

Logcat消息 enter image description here

更新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 / 86

此外,当我尝试放大/缩小或向上/向下滚动时,模拟器会弹出一条弹出消息

仿真器:处理完成,退出代码为-1073741819(0xC0000005)

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您确定API密钥与

中的应用ID匹配

Google API Console - Google Cloud Platform

答案 2 :(得分:0)

参考Google Issues Tracker

已修复Google API 27图片(第3版)。它应修复Google Maps示例问题..

如果您使用的是API 27,那么Maps API在那里工作就是一个众所周知的问题。现在尝试使用API​​ 26等较低的API级别

如果问题仍然存在,请在Google issue tracker报告,然后重新审核。

相关问题