【问题标题】:Google Maps not showing in my app , and appear in another app谷歌地图未显示在我的应用程序中,并出现在另一个应用程序中
【发布时间】:2019-12-13 09:09:11
【问题描述】:

我在使用谷歌地图时遇到了一个奇怪的问题, 我已按照所有说明操作,但地图没有出现在我的应用程序中并显示此消息:

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>

App Gradle:

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


 }

这就是 LogCat:

   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 Verfier 造成的,我在例外列表中添加了以下域 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;
                }
            }
        });

【问题讨论】:

  • 您能否发布您的地图活动、清单和应用程序级 build.gradle 文件,以便我们从我们这边调查这个问题?也请分享完整的 logcat。
  • @evan .. 请检查编辑,还有别的吗?
  • 谢谢您,我会测试您的代码并在几个小时内回复您。
  • 刚刚在下面的答案中发布了我的发现,请随时关注我的测试和调查。我期待您的更新,如果需要,我很乐意从我这边进行进一步调查。

标签: java android google-maps dictionary


【解决方案1】:

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

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

地图活动

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 文件(在调试和发布文件夹中),在您的项目中启用 Maps SDK for Android 并且您的结算帐户信誉良好。

希望对您有所帮助!

【讨论】:

  • 好吧,感谢您的慷慨帮助,我确实按照您的步骤进行操作,但仍然存在同样的问题!
  • 我明白了,在这种情况下,问题必须与网络或仿真器/设备相关,因为上面的代码可以正常工作。您正在使用哪一个,并且您是否尝试在多个设备/模拟器上运行该应用程序?谷歌播放服务是最新的吗?还请使用属于已启用计费的项目的不受限制的 api 密钥。您收到任何不同的错误还是相同的错误?
  • 亲爱的,我发现了这个问题,这是由于 SSL 验证程序造成的。我已将域 clients4.google.com 添加到例外列表并解决了问题,谢谢 :)
猜你喜欢
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多