谷歌地图未显示在我的应用程序中,并显示在另一个应用程序中



我在谷歌地图上遇到了一个奇怪的问题, 我已经按照所有说明进行操作,但地图没有出现在我的应用程序中并说以下消息:

E/Google Maps Android API:无法加载地图。联系 Google 服务器时出错。这可能是身份验证问题(但可能是由于网络错误(。

我使用相同的API密钥(不受限制(在用于测试目的的新项目上执行的相同步骤,并且显示地图。

你能帮我解决可能的原因吗?

编辑:

以下是地图活动:

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
Button select ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
select = (Button) findViewById(R.id.select);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK,intent);
finish();
}
});


}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.addMarker(new MarkerOptions().position(HAMBURG).title("Marker in Delhi"));
map.moveCamera(CameraUpdateFactory.newLatLng(HAMBURG));
}}

和 XML :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MapActivity">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@color/coldGreen"
android:text="@string/select_this_place"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.551"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"></Button>
</androidx.constraintlayout.widget.ConstraintLayout>

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="*.*.app" 
android:versionCode="30"
android:versionName="1.2.9">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

<dist:module dist:instant="true" />
<application
android:name=".Dayir"
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"
android:usesCleartextTraffic="true">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity android:name=".activities.MapActivity" />
</application>
</manifest>

那里的应用程序 格拉德尔 :

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "*.*.app"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android- 
optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.android.volley:volley:1.1.0'
implementation 'de.hdodenhof:circleimageview:2.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.karumi:dexter:5.0.0'
implementation 'com.google.firebase:firebase-messaging:17.3.3'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-base:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.mxn.soul:flowingdrawer-core:2.1.0'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'com.flaviofaria:kenburnsview:1.0.7'
implementation 'com.github.developer-shivam:Crescento:1.2.1'
implementation 'com.github.bumptech.glide:glide:4.3.1'
implementation 'com.wang.avi:library:2.1.3'
implementation 'com.github.DanielMartinus:Stepper-Touch:1.0.1'
implementation 'com.github.sephiroth74:NumberSlidingPicker:v.1.1.1'
implementation 'com.github.amlcurran.showcaseview:library:5.4.3'
implementation project(path: ':searchablespinner')

}

这是 日志猫 :

2019-12-16 13:42:23.149 23386-23386/*.*.app I/Timeline: Timeline: 
Activity_launch_request time:1606688503
2019-12-16 13:42:23.181 23386-23386/*.*.app W/ActivityThread: 
handleWindowVisibility: no activity for token android.os.BinderProxy@b1c360f
2019-12-16 13:42:23.243 23386-23386/*.*.app I/Google Maps Android API: 
Google Play services package version: 19629028
2019-12-16 13:42:35.549 23386-23386/*.*.app E/SchedPolicy: 
set_timerslack_ns write failed: Operation not permitted
2019-12-16 13:42:35.553 23386-23386/*.*.app D/onActivityResult: try to 
set location
2019-12-16 13:42:35.553 23386-23386/*.*.app D/result: ok excuted 
2019-12-16 13:42:37.715 23386-23386/*.*.app I/Timeline: Timeline: 
Activity_launch_request time:1606703069
2019-12-16 13:42:37.745 23386-23386/*.*.app W/ActivityThread: 
handleWindowVisibility: no activity for token android.os.BinderProxy@c18f5a1
2019-12-16 13:42:37.819 23386-23386/*.*.app I/Google Maps Android API: 
Google Play services package version: 19629028
2019-12-16 13:42:37.999 23386-23888/*.*.app 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).

编辑:

我发现合作是由于我在应用程序中使用的SSL验证器,我在例外列表中添加了以下域clients4.google.com。 我希望这可以帮助某人:)

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
//  Log.d("verify_url","is => "+arg0);
if(arg0.equals("www.yourMainDomain.com") || 
arg0.equals("clients4.google.com") ){
return true;
}else{
return false;
}
}
});

我刚刚测试了您的代码,它使用模拟器在我的终端上没有问题。我所做的只是将活动设置为启动器并删除第三方依赖项。

请测试下面的代码,它基本上是你自己的代码,没有依赖:

地图活动

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
Button select;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
select = (Button) findViewById(R.id.select);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.addMarker(new MarkerOptions().position(HAMBURG).title("Marker in Delhi"));
map.moveCamera(CameraUpdateFactory.newLatLng(HAMBURG));
}
}

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.551"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"></Button>
</androidx.constraintlayout.widget.ConstraintLayout>

build.gradle

apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.android.volley:volley:1.1.0'
implementation 'de.hdodenhof:circleimageview:2.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.karumi:dexter:5.0.0'
implementation 'com.google.firebase:firebase-messaging:17.3.3'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-base:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.mxn.soul:flowingdrawer-core:2.1.0'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'com.flaviofaria:kenburnsview:1.0.7'
implementation 'com.wang.avi:library:2.1.3'
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.example.myapplication"
android:versionCode="30"
android:versionName="1.2.9">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<dist:module dist:instant="true" />
<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"
android:usesCleartextTraffic="true">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

如果上面的代码适合您,那么您可以排除与网络相关或模拟器/设备相关的问题(如果不是,请查看这些问题(。

因此,根本原因是我删除的那些库依赖项或您的 API 密钥。确保您已将不受限制的 API 密钥添加到google_maps_api.xml文件(在调试和发布文件夹中(,确保在您的项目中启用了适用于 Android 的地图 SDK,并且您的结算帐号信誉良好。

希望对您有所帮助!

相关内容

  • 没有找到相关文章

最新更新