【发布时间】: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
更新 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