gpt4 book ai didi

AndroidX Fragment 添加谷歌地图

转载 作者:太空狗 更新时间:2023-10-29 14:37:39 25 4
gpt4 key购买 nike

我正在尝试使用 MVP 模式在 AndroidX fragment (androidx.fragment.app.Fragment) 中添加 Google map ,但它没有显示出来。但是当我尝试在正常的 Fragment(android.support.v4.app.Fragment) 或 Activity 中使用这段代码时,它显示正常。

两种方法我都试过了:

  1. 使用 MapView(在代码中有注释)

  2. 使用 SupportMapFragment(AndroidX ref here 不支持)

这是我试过的代码:

import android.os.Bundle
import com.google.android.gms.maps.*
import com.google.android.gms.maps.model.LatLng
import com.test.App
import com.test.R
import com.test.ui.base.BaseFragment
import javax.inject.Inject
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.SupportMapFragment

class OverviewMapFragment : BaseFragment<OverviewMapContract.View, OverviewMapContract.Presenter>(), OverviewMapContract.View, OnMapReadyCallback {
private var map: GoogleMap? = null
// private var mapView: MapView? = null

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

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

// mapView = view!!.findViewById(R.id.map_view) as MapView
// mapView!!.onCreate(savedInstanceState);
// mapView!!.getMapAsync(this)
}

/*override fun onStart() {
super.onStart()
mapView!!.onStart()
}

override fun onResume() {
super.onResume()
mapView!!.onResume()
}

override fun onDestroy() {
mapView!!.onDestroy()
super.onDestroy()
}

override fun onLowMemory() {
super.onLowMemory()
mapView!!.onLowMemory()
}

override fun onStop() {
super.onStop()
mapView!!.onStop()
}

override fun onPause() {
mapView!!.onPause()
super.onPause()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView!!.onSaveInstanceState(outState)
}*/

override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
map!!.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map!!.addMarker(MarkerOptions().position(ny));
map!!.moveCamera(CameraUpdateFactory.newLatLng(ny))
}

xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.ui.components.overviewmap.OverviewMapFragment" />

</LinearLayout>

list :

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

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

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

<uses-library android:name="com.google.android.maps" />

使用的 googleMapVersion 16.0.0:

implementation 'com.google.android.gms:play-services-maps:$googleMapVersion' 

最佳答案

最后,我通过使用 MapView(代码中有注释)并稍微延迟调用 onMapReady 找到了解决方案。现在使用 MapView 可以很好地显示 map 。

override fun initUI() {
mapView?.postDelayed({
mapView?.onCreate(Bundle())
mapView?.getMapAsync {
onMapReady(it)
}
}, 500)
}

private fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
// map?.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map?.addMarker(MarkerOptions().position(ny));
map?.moveCamera(CameraUpdateFactory.newLatLng(ny))
}

关于AndroidX Fragment 添加谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53407044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com