GoogleMap已连接但未显示任何内容(KotlinAndroid Studio)

xt0899hw  于 2023-11-21  发布在  Android
关注(0)|答案(2)|浏览(210)

Error in the Debug Logcat
Display Screen When run the application
Google Map XML中的API密钥:<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">XXX_MY_API_KEY_XXX</string>
API key in Manifest file
主要活动

package org.classapp.test2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

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
import org.classapp.test2.databinding.ActivityMapsBinding

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap
    private lateinit var binding: ActivityMapsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMapsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        // Add a marker in Sydney and move the camera
        val sydney = LatLng(-34.0, 151.0)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }
}

字符串
清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.classapp.test2">

   
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <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/Theme.Test2">

     
        <meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY" />

        <activity
            android:name=".MapsActivity"
            android:exported="true"
            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>


Google API文件

<resources>
   
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_API_KEY</string>
</resources>


Gradle文件

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "org.classapp.test2"
        minSdk 22
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'com.google.android.gms:play-services-maps:18.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.android.gms:play-services-maps:18.0.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}


我按照谷歌Map活动给出的指示,但屏幕不会显示Map时,运行应用程序。
我只是添加了这个项目的所有代码,我不知道是什么错了.

7xzttuei

7xzttuei1#

您可以:

  • 在清单文件中添加Internet权限。缺少权限时有时会发生这种情况。
  • 在清单中手动添加密钥而不是xml文件
<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzxxxxxxxxxxxxxxxxxxxxxxxkey" />

字符串

i1icjdpr

i1icjdpr2#

你必须做一些改变。首先在你的主活动文件。你必须创建标记选项的对象替换此代码
第一个月

  • 与这一个 *
val sydney = LatLng(-34.0, 151.0)
val markerOptions = MarkerOptions().position(sydney).title("Sydney")
mMap!!.addMarker(markerOptions)
mMap!!.moveCamera(CameraUpdateFactory.newLatLng(sydney))
mMap!!.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 16f))

字符串
在您的build.gradle(Module:app)文件中更改几个参数

implementation "com.google.android.gms:play-services-maps:18.0.2"
implementation "com.google.android.gms:play-services-location:21.0.1"


在你的Gradle文件中,很少有依赖会重复两次。只需删除一个

相关问题