【发布时间】:2019-11-27 11:12:51
【问题描述】:
我尝试在我的应用程序中使用地图,更准确地说是在ViewPager 页面/片段中。我想要的是从谷歌表中获取数据,然后使用从谷歌表数据生成的图钉更新地图。
我已经有一个可以完美运行的应用程序,但是有一个 Activity 和一个 MapFragment。当我尝试将其转换为带有MapView 和片段的ViewPager 时,我尝试在片段的onViewCreated 中调用getMapAsync()。
然而,onMapReady() 此后再也不会被调用。我错过了什么吗?我试图覆盖片段的生命周期方法以插入 MapView 的方法,但它没有改变任何东西。
如果有人遇到过这种问题,欢迎帮助,我让我的代码在下面。
StoreLocatorFragment:
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))
}
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>
【问题讨论】:
标签: android google-maps android-viewpager