Android 调用getMapAsync()后,从未调用ViewPager:onMapReady()中片段中的MapView
我尝试在我的应用程序中使用地图,更准确地说是在Android 调用getMapAsync()后,从未调用ViewPager:onMapReady()中片段中的MapView,android,google-maps,android-viewpager,Android,Google Maps,Android Viewpager,我尝试在我的应用程序中使用地图,更准确地说是在ViewPagerpage/fragment中。我想要的是从google工作表中获取数据,然后用google工作表数据生成的PIN更新地图 我已经有了一个运行完美的应用程序,但是有一个活动和一个MapFragment。当我尝试将其转换为带有MapView和片段的ViewPager时,我尝试在片段的onViewCreated中调用getMapAsync() 但是,在此之后,将永远不会调用onMapReady()。我错过什么了吗?我试图覆盖片段的生命周期
ViewPager
page/fragment中。我想要的是从google工作表中获取数据,然后用google工作表数据生成的PIN更新地图
我已经有了一个运行完美的应用程序,但是有一个活动和一个MapFragment。当我尝试将其转换为带有MapView
和片段的ViewPager
时,我尝试在片段的onViewCreated
中调用getMapAsync()
但是,在此之后,将永远不会调用onMapReady()。我错过什么了吗?我试图覆盖片段的生命周期方法以插入MapView
的方法,但它没有改变任何东西
如果有人遇到这种问题,欢迎提供帮助,我将在下面介绍我的代码
StoreLocator片段:
private lateinit var mMap: GoogleMap
private val markers = mutableListOf<ShopMarkerData>()
private lateinit var fusedLocationClient: FusedLocationProviderClient
private var lastLocation: Location? = null
private lateinit var lastMarker: ShopMarkerData
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
enovap_mv.getMapAsync(this)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)
fusedLocationClient.lastLocation
.addOnSuccessListener { location : Location? ->
lastLocation = location
}
[...]
}
override fun onResume() {
super.onResume()
// Function launching the data retrieving on the Google Sheets
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(context, R.raw.maps_style))
mMap.setOnMapClickListener {
if (layout_selected_marker.visibility == View.VISIBLE) {
layout_selected_marker.startAnimation(
AnimationUtils.loadAnimation(
context,
R.anim.maps_card_out
)
)
}
layout_selected_marker.visibility = View.GONE
}
mMap.setOnMarkerClickListener {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(it.position.latitude, it.position.longitude), 14f))
for (shop in markers) {
if (shop.lat == it.position.latitude && shop.lng == it.position.longitude) {
layout_selected_marker.visibility = View.VISIBLE
layout_selected_marker.startAnimation(AnimationUtils.loadAnimation(context, R.anim.maps_card_in))
lastMarker = shop
tv_shop_name.text = shop.name
tv_shop_address.text = shop.street
tv_shop_city.text = getString(R.string.city_assemble, shop.postcode, shop.city)
tv_shop_country.text = shop.country
tv_shop_phone.text = shop.phone
}
}
true
}
}
private fun getRequestValues(values: List<List<Any>>?) {
if (values == null)
return
for (row in values) {
val shop = ShopMarkerData("", "", "", "", "", "", "", 0.0, 0.0)
for (i in row.indices) {
when (i) {
0 -> shop.name = row[i] as String
1 -> shop.street = row[i] as String
3 -> shop.postcode = row[i] as String
4 -> shop.city = row[i] as String
5 -> shop.country = row[i] as String
6 -> shop.phone = row[i] as String
7 -> shop.address = row[i] as String
8 -> shop.lat = (row[i] as String).toDouble()
9 -> shop.lng = (row[i] as String).toDouble()
}
}
markers.add(shop)
}
for (place in markers) {
val pos = LatLng(place.lat, place.lng)
val bitmapDescriptor = bitmapDescriptorFromVector(context!!, R.drawable.ic_pin)
mMap.addMarker(MarkerOptions().position(pos).title(place.address).icon(bitmapDescriptor))
}
mMap.isMyLocationEnabled = true
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(lastLocation!!.latitude, lastLocation!!.longitude), 14f))
}
private lateinit var mMap:GoogleMap
private val markers=mutableListOf()
私有lateinit变量fusedLocationClient:FusedLocationProviderClient
私人var lastLocation:位置?=无效的
私有lateinit var lastMarker:ShopMarkerData
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
super.onViewCreated(视图,savedInstanceState)
enovap_mv.getMapAsync(此)
fusedLocationClient=LocationServices.getFusedLocationProviderClient(上下文!!)
fusedLocationClient.lastLocation
.addOnSuccessListener{位置:位置?->
lastLocation=位置
}
[...]
}
重写onResume(){
super.onResume()
//函数在Google工作表上启动数据检索
}
在mapready上覆盖乐趣(谷歌地图:谷歌地图){
mMap=谷歌地图
mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(context,R.raw.maps_style))
mMap.setOnMapClickListener{
如果(布局\选定\标记.可见性==视图.可见){
布局\u选定\u标记。开始动画(
AnimationUtils.loadAnimation(
上下文
R.anim.maps\u card\u out
)
)
}
布局\u选定\u标记。可见性=视图
}
mMap.setOnMarkerClickListener{
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(it.position.latitude,it.position.longitude),14f))
适用于(商店标记){
if(shop.lat==it.position.lation&&shop.lng==it.position.longitude){
布局\选定\标记.可见性=视图.可见
布局\u选定\u标记.startAnimation(AnimationUtils.loadAnimation(context,R.anim.maps\u card\u in))
lastMarker=商店
tv\u shop\u name.text=shop.name
tv\u shop\u address.text=shop.street
tv\u shop\u city.text=getString(R.string.city\u汇编,shop.postcode,shop.city)
tv\u shop\u country.text=shop.country
tv\u shop\u phone.text=shop.phone
}
}
真的
}
}
private fun getRequestValues(值:列表?){
如果(值==null)
返回
用于(值中的行){
val shop=ShopMarkerData(“,”,“,”,“,”,“,”,0.0,0.0)
对于(第i行索引){
何时(i){
0->shop.name=行[i]作为字符串
1->shop.street=行[i]作为字符串
3->shop.postcode=行[i]作为字符串
4->shop.city=行[i]作为字符串
5->shop.country=行[i]作为字符串
6->shop.phone=行[i]作为字符串
7->shop.address=行[i]作为字符串
8->shop.lat=(行[i]作为字符串).toDouble()
9->shop.lng=(行[i]作为字符串).toDouble()
}
}
标记。添加(商店)
}
用于(放置在标记中){
val pos=LatLng(place.lat,place.lng)
val bitmapDescriptor=bitmapDescriptorFromVector(context!!,R.drawable.ic_pin)
mMap.addMarker(MarkerOptions().position(pos).title(place.address).icon(bitmapDescriptor))
}
mMap.isMyLocationEnabled=真
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(lastLocation!!.lation,lastLocation!!.longitude),14f))
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.gms.maps.MapView
android:id="@+id/enovap_mv"
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"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="@id/guideline1"
/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_selected_marker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/guideline1"
app:layout_constraintBottom_toBottomOf="parent"
android:padding="16dp"
android:layout_margin="16dp"
android:orientation="vertical"
android:background="@drawable/button_shape_default"
android:elevation="2dp"
android:clickable="true"
android:focusable="true">
<TextView
android:id="@+id/tv_shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
tools:text="Green and Vape"
/>
<LinearLayout
android:id="@+id/layout_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@id/tv_shop_name"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="@+id/tv_shop_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
tools:text="23 rue du Roule"
android:textSize="16sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/tv_shop_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
tools:text="75001 PARIS"
android:textSize="16sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/tv_shop_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
tools:text="France"
android:textSize="16sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/tv_shop_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:drawableEnd="@drawable/ic_phone"
android:drawablePadding="4dp"
android:gravity="bottom"
android:textSize="16sp"
android:textStyle="bold"
tools:text="01 40 13 09 57" />
</LinearLayout>
<Button
android:id="@+id/btn_direction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/layout_address"
app:layout_constraintEnd_toEndOf="@id/layout_address"
android:text="Route"
android:elevation="2dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.65"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
我在片段中遇到过同样的问题,我应用了以下代码。我希望它能帮助你
public override fun onResume() {
mapView.onResume()
super.onResume()
}
public override fun onPause() {
super.onPause()
mapView.onPause()
}
public override fun onDestroy() {
super.onDestroy()
if (mapView != null) {
mapView.onDestroy()
}
}
override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
我曾经在片段中遇到过同样的问题,我应用了以下代码。我希望它能帮助你
public override fun onResume() {
mapView.onResume()
super.onResume()
}
public override fun onPause() {
super.onPause()
mapView.onPause()
}
public override fun onDestroy() {
super.onDestroy()
if (mapView != null) {
mapView.onDestroy()
}
}
override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
我已经试过了,它不起作用,谢谢你的建议!我已经试过了,它不起作用,谢谢你的建议!我两个都试过了,都没用,谢谢你的建议!我两个都试过了,都没用,谢谢你的建议!