我已经尝试了一千种配置和代码,但我的地图活动坚持不工作。我尝试过的事情:
的AndroidManifest.xml
文件:
<manifest package="com.myapp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:anrdroid="http://schemas.android.com/apk/res-auto">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/name"
android:supportsRtl="true"
android:name="android.support.multidex.MultiDexApplication"
android:theme="@style/Default">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key"/>
<activity
android:name=".view.MainActivity"
android:icon="@drawable/logo"
android:label="@string/name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".view.ActivityMap"
android:icon="@drawable/logo_row"
android:label="@string/name">
</activity>
...
</application>
</manifest>
的build.gradle
文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
applicationId "com.myapp"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "0.0.1"
multiDexEnabled true
}
signingConfigs {
keystore {
storeFile file("path_to_keystore")
storePassword "my_password"
keyAlias "my_key_alias"
keyPassword "my_password"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.keystore
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/io.netty.versions.properties'
exclude 'META-INF/INDEX.LIST'
}
dexOptions {
javaMaxHeapSize "2g"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.datastax.cassandra:cassandra-driver-mapping:3.0.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.5.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.4'
}
代码
文件:
public class ActivityMap extends FragmentActivity implements OnMapReadyCallback
{
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.activityMapID);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap)
{
map = googleMap;
map.addMarker(new MarkerOptions().position(new LatLng(-34, 151)).title("Marker in Sydney"));
}
}
activity_map.xml:
<fragment android:id="@+id/activityMapID"
android:name="com.google.android.gms.maps.SupportMapFragment"
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:layout_width="match_parent"
android:layout_height="match_parent"/>
其他
在第一次通话中,我有这个日志:
03-21 09:34:28.478 25570-26174/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.gms.maps.auth.ApiTokenService
03-21 09:34:29.770 25570-25570/com.myapp I/Choreographer: Skipped 127 frames! The application may be doing too much work on its main thread.
03-21 09:34:30.020 25570-27391/com.myapp I/b: Sending API token request.
03-21 09:34:30.090 25570-25633/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.location.internal.GoogleLocationManagerService
03-21 09:34:31.231 25570-27391/com.myapp I/b: Received API Token: AH0uPGGIx9Ygap4ZXq8T8vK7qcPxgyDi5NRIaIPyPauG4xWFn1lI7KQF9IuY2yqbpYMhWitKnTUiU-sZD9tFdJvr1naCVhz3c9APmLbueopuhcD6K5LHpZKnkYCQDEefhrTeGPNOR0fdz0QmC4WD8rkgQTbLmfbnOIA7cvZZBOmn7hFmcyavIVRQZlaY9_OLaPMFxIVFVBml / Expires in: 432000000ms
03-21 09:34:31.241 25570-27391/com.myapp I/c: Scheduling next attempt in 431700 seconds.
03-21 09:34:31.251 25570-27391/com.myapp I/d: Saved auth token
03-21 09:34:31.832 25570-25582/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.gms.clearcut.service.ClearcutLoggerService
然后在第二次通话后,我收到了我的密钥无效的消息:
03-21 09:35:22.596 25570-25633/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.gms.clearcut.service.ClearcutLoggerService
03-21 09:35:37.762 25570-25570/com.myapp I/Google Maps Android API: Google Play services package version: 8703034
03-21 09:35:37.973 25570-28472/com.myapp E/Google Maps Android API: Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).
03-21 09:35:38.363 25570-25582/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.location.internal.GoogleLocationManagerService
我真的不知道该怎么做了。有人知道会发生什么吗?
提前致谢。
答案 0 :(得分:1)
最后我发现了问题。
在我的代码的另一部分中,我正在调用
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
简化我为HTTPS所做的所有调用。问题出在谷歌地图试图调用其服务器时:它使用的是我之前设置的相同SSL上下文,因此无法在地图服务器上连接。
我刚刚评论了该行并将我的代码更改为:
URLConnection urlConn = new URL(myAddress).openConnection();
HttpsURLConnection https = (HttpsURLConnection) urlConn;
https.setSSLSocketFactory(sslContext.getSocketFactory());
一切都像魔术一样!
谢谢大家!
答案 1 :(得分:0)
尝试在build.radle
申请插件:'com.google.gms.google-services'
将其添加到manifest
标记内:
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" >
</uses-feature>