Android 地图 v2 - 信息 Window 尽管存在监听器,但点击不起作用
Android Maps v2 - Info Window click not working despite listener present
我正在尝试根据单击的 InfoWindow
显示 Toast
。我已经为每个 InfoWindow
实施了点击侦听器,但由于 none 的原因,它们在单击时会执行任何操作。我还分别尝试了 2 种不同的方法 (mGoogleMap.setOnInfoWindowClickListener
) 和 (override fun onInfoWindowClick
),但仍然没有成功。
class MyFragment : androidx.fragment.app.Fragment(), GoogleMap.OnInfoWindowClickListener, OnMapReadyCallback {
private lateinit var mGoogleMap: GoogleMap
private lateinit var mMapView: MapView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.my_fragment, container, false)
mMapView = v.map_location
mMapView.onCreate(savedInstanceState)
mMapView.getMapAsync(this)
return v
}
override fun onMapReady(googleMap: GoogleMap) {
mGoogleMap = googleMap
mGoogleMap.mapType = GoogleMap.MAP_TYPE_NORMAL
val locationA = LatLng(44.862390, -0.550287)
mGoogleMap.addMarker(
MarkerOptions()
.position(locationA)
.title("Location A")
)
val locationB = LatLng(44.863089, -0.551417)
mGoogleMap.addMarker(
MarkerOptions()
.position(locationB)
.title("Location B")
)
mGoogleMap.setOnInfoWindowClickListener { marker ->
if (marker.equals(locationA)) {
Toast.makeText(activity, "A", Toast.LENGTH_SHORT).show()
} else if (marker.equals(locationB)) {
Toast.makeText(activity, "B", Toast.LENGTH_SHORT).show()
}
}
val cameraUpdate = CameraUpdateFactory.newLatLngZoom(LatLng(44.862716, -0.550763), 18f)
}
override fun onInfoWindowClick(marker: Marker?) {
if (marker != null) {
if (marker.id == "locationA") {
Toast.makeText(activity, "A", Toast.LENGTH_SHORT).show()
}
else if (marker.id == "locationB") {
Toast.makeText(activity, "B", Toast.LENGTH_SHORT).show()
}
}
}
override fun onResume() {
super.onResume()
mMapView.onResume()
}
override fun onPause() {
super.onPause()
mMapView.onPause()
}
override fun onDestroy() {
super.onDestroy()
mMapView.onDestroy()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mMapView.onSaveInstanceState(outState)
}
override fun onLowMemory() {
super.onLowMemory()
mMapView.onLowMemory()
}
}
您正在将 LatLng 与标记进行比较,它不起作用。
例如,您应该比较标记标题:
override fun onMapReady(googleMap: GoogleMap) {
mGoogleMap = googleMap
mGoogleMap.mapType = GoogleMap.MAP_TYPE_NORMAL
val locationA = LatLng(44.862390, -0.550287)
// markerA is a variable of your fragment
markerA = mGoogleMap.addMarker(
MarkerOptions()
.position(locationA)
.title("Location A")
)
val locationB = LatLng(44.863089, -0.551417)
// markerB is a variable of your fragment
markerB = mGoogleMap.addMarker(
MarkerOptions()
.position(locationB)
.title("Location B")
)
mGoogleMap.setOnInfoWindowClickListener { marker ->
onMarkerClick(marker)
}
val cameraUpdate = CameraUpdateFactory.newLatLngZoom(LatLng(44.862716, -0.550763), 18f)
}
override fun onInfoWindowClick(marker: Marker?) {
if (marker != null) {
onMarkerClick(marker)
}
}
private fun onMarkerClick(marker: Marker) {
if (marker.id == markerA.id) {
Toast.makeText(activity, "A", Toast.LENGTH_SHORT).show()
} else if (marker.id == markerB.id) {
Toast.makeText(activity, "B", Toast.LENGTH_SHORT).show()
}
}
}
我正在尝试根据单击的 InfoWindow
显示 Toast
。我已经为每个 InfoWindow
实施了点击侦听器,但由于 none 的原因,它们在单击时会执行任何操作。我还分别尝试了 2 种不同的方法 (mGoogleMap.setOnInfoWindowClickListener
) 和 (override fun onInfoWindowClick
),但仍然没有成功。
class MyFragment : androidx.fragment.app.Fragment(), GoogleMap.OnInfoWindowClickListener, OnMapReadyCallback {
private lateinit var mGoogleMap: GoogleMap
private lateinit var mMapView: MapView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.my_fragment, container, false)
mMapView = v.map_location
mMapView.onCreate(savedInstanceState)
mMapView.getMapAsync(this)
return v
}
override fun onMapReady(googleMap: GoogleMap) {
mGoogleMap = googleMap
mGoogleMap.mapType = GoogleMap.MAP_TYPE_NORMAL
val locationA = LatLng(44.862390, -0.550287)
mGoogleMap.addMarker(
MarkerOptions()
.position(locationA)
.title("Location A")
)
val locationB = LatLng(44.863089, -0.551417)
mGoogleMap.addMarker(
MarkerOptions()
.position(locationB)
.title("Location B")
)
mGoogleMap.setOnInfoWindowClickListener { marker ->
if (marker.equals(locationA)) {
Toast.makeText(activity, "A", Toast.LENGTH_SHORT).show()
} else if (marker.equals(locationB)) {
Toast.makeText(activity, "B", Toast.LENGTH_SHORT).show()
}
}
val cameraUpdate = CameraUpdateFactory.newLatLngZoom(LatLng(44.862716, -0.550763), 18f)
}
override fun onInfoWindowClick(marker: Marker?) {
if (marker != null) {
if (marker.id == "locationA") {
Toast.makeText(activity, "A", Toast.LENGTH_SHORT).show()
}
else if (marker.id == "locationB") {
Toast.makeText(activity, "B", Toast.LENGTH_SHORT).show()
}
}
}
override fun onResume() {
super.onResume()
mMapView.onResume()
}
override fun onPause() {
super.onPause()
mMapView.onPause()
}
override fun onDestroy() {
super.onDestroy()
mMapView.onDestroy()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mMapView.onSaveInstanceState(outState)
}
override fun onLowMemory() {
super.onLowMemory()
mMapView.onLowMemory()
}
}
您正在将 LatLng 与标记进行比较,它不起作用。
例如,您应该比较标记标题:
override fun onMapReady(googleMap: GoogleMap) {
mGoogleMap = googleMap
mGoogleMap.mapType = GoogleMap.MAP_TYPE_NORMAL
val locationA = LatLng(44.862390, -0.550287)
// markerA is a variable of your fragment
markerA = mGoogleMap.addMarker(
MarkerOptions()
.position(locationA)
.title("Location A")
)
val locationB = LatLng(44.863089, -0.551417)
// markerB is a variable of your fragment
markerB = mGoogleMap.addMarker(
MarkerOptions()
.position(locationB)
.title("Location B")
)
mGoogleMap.setOnInfoWindowClickListener { marker ->
onMarkerClick(marker)
}
val cameraUpdate = CameraUpdateFactory.newLatLngZoom(LatLng(44.862716, -0.550763), 18f)
}
override fun onInfoWindowClick(marker: Marker?) {
if (marker != null) {
onMarkerClick(marker)
}
}
private fun onMarkerClick(marker: Marker) {
if (marker.id == markerA.id) {
Toast.makeText(activity, "A", Toast.LENGTH_SHORT).show()
} else if (marker.id == markerB.id) {
Toast.makeText(activity, "B", Toast.LENGTH_SHORT).show()
}
}
}