【问题标题】:Kotlin Google Maps doesnt StartKotlin 谷歌地图无法启动
【发布时间】:2020-03-06 13:46:10
【问题描述】:
lass MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)
        // 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)
        val permissionx= arrayOf(android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)
        ActivityCompat.requestPermissions(this, permissionx,0)

    }

    /**
     * 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 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))
    }
}

我在那里设置了permıssıon代码,因为我想检查程序是否到那里并且程序永远不会去那里并且想要permıssion我也用Toast尝试过,但程序从不打开它

地图片段的主要活动Xml

<fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"

            android:layout_width="match_parent"
            android:layout_height="259dp"
            tools:context="com.example.siparis.MapsActivity" />

我确定我输入的密钥是正确的。

这是我的程序清单。

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

    <!--
         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_COARSE_LOCATION" />

    <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.AppCompat.NoActionBar">

        <!--
             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"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Logcat 中唯一的错误是:

2020-03-06 16:52:50.323 20804-20804/? E/example.sipari:未知位 在 runtime_flags 中设置:0x8000

【问题讨论】:

  • 有什么例外吗??
  • 也不例外。让我用 logcat 编辑

标签: android google-maps kotlin


【解决方案1】:

请检查:

https://developer.android.com/reference/android/Manifest.permission#ACCESS_BACKGROUND_LOCATION

Allows an app to access location in the background. If you're requesting this permission, you must also request either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. Requesting this permission by itself doesn't give you location access.

我认为您缺少权限。也请在使用前提出要求。

【讨论】:

  • 我已经获得了两个permıssıon 而且程序不会在后台运行。
  • 那么请更新您的问题,因为它目前只请求 ACCESS_BACKGROUND_LOCATION
  • 请注意,根据 android 版本在 manifest 中声明是不够的。
  • ı 尝试在 onmap 创建 Toast,它甚至不显示 Toast。或者它甚至不检查 permıssıon 即使我在 oncreatemap 中对其进行编码
【解决方案2】:

您应该在onMapReady() 内调用mMap.onResume()

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

    //map is the mapview in the layout
    (map as SupportMapFragment).onResume() //this line is very necessary
}

【讨论】:

  • 没有接口mMap.onResume()
  • 不,没有mMap.onResume()
  • @OğuzKurukaya 如果您在布局中使用fragment 而不是MapView,则将您的地图转换为SupportMapFragment
  • 我在所有代码中都用作 SupportMapFragment 我错过了某个地方
猜你喜欢
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多