从 SearchView onQueryTextSubmit 创建时不显示视图
View not displayed when created from SearchView onQueryTextSubmit
我想在搜索视图中搜索位置并使用标记在地图上显示该位置。单击标记时,我想显示一个带有位置名称等的信息window。我使用 OSMDROID 和 OSMBONUSPACK。
问题:
标记按预期显示并且它的 onClickListener 确实触发,但如果我从 onQueryTextSubmit
调用我的 searchLocationOnMap
,则不会显示信息 window。如何解决?
不起作用(如果单击标记则不会显示任何信息 window):
searchViewMap?.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
val location = searchViewMap?.query.toString()
if (location != null && location != "") {
try {
searchLocationOnMap("Moosach Bhf")
} catch (e: java.lang.Exception) {
Timber.d("Error while searching for location on map")
}
}
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
return true
}
})
出于某种原因,如果我从 onViewCreated
或从常规 button.setOnClickListener{}
.
调用 searchLocationOnMap
,则会显示
有效(单击标记时显示信息window):
btn.setOnClickListener { searchLocationOnMap("Moosach Bhf") }
这是 searchLocationOnMap():
private fun searchLocationOnMap(keyword: String) = GlobalScope.launch(Dispatchers.Main) {
removeOldLocationsFromMap()
val locationAddress = withContext(Dispatchers.Default) { obtainAddressFromKeyword(keyword) }
if (locationAddress == null) {
Toast.makeText(context, "No address found for keyword $keyword", Toast.LENGTH_SHORT).show()
} else {
val marker = createMarkerFromAddress(locationAddress)
mPOIOverlayFolder.add(marker)
map.controller.animateTo(marker.position)
map.invalidate()
}
这是 obtainAdressFromKeyword:
private fun obtainAddressFromKeyword(keyword: String): Address? {
val result = GeocoderNominatim("TestUserAgent").getFromLocationName(keyword, 1).firstOrNull()
Timber.d("result: $result")
return result
}
这是 createMarkerFromAddress:
private fun createMarkerFromAddress(address: Address): Marker {
val poiMarker = Marker(map)
poiMarker.title = address.getAddressLine(0)
poiMarker.snippet = "Lat : ${address.latitude},\n Long ${address.longitude}"
poiMarker.position = GeoPoint(address.latitude, address.longitude)
poiMarker.icon = ContextCompat.getDrawable(applicationContext, R.drawable.poi_black)
poiMarker.infoWindow = MarkerInfoWindow(R.layout.marker_info_bubble, map)
poiMarker.setOnMarkerClickListener { marker, map -> onMarkerClick(marker) }
return poiMarker
}
这是 onMarkerClick:
private fun onMarkerClick(marker: Marker) : Boolean {
marker.showInfoWindow()
marker.infoWindow.view.bringToFront()
Timber.d("Clicked Marker ${marker.title}, infowindowshown: ${marker.isInfoWindowShown}")
return true
木材结果(在两种情况下):
Clicked Marker Memminger Platz, infowindowshown: true
我是这样解决的
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Log.i("MyApp", searchView.getQuery().toString());
consultarListaParadas(searchView.getQuery().toString());
getActivity().runOnUiThread(new Runnable() {
public void run() {
adaptadorListaParadasBus.setListaParadas(listaParadas);
adaptadorListaParadasBus.notifyDataSetChanged();
}
});
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if(newText.equals("")){
this.onQueryTextSubmit("");
}
return true;
}
});
map.parent 视图必须失效。
我想在搜索视图中搜索位置并使用标记在地图上显示该位置。单击标记时,我想显示一个带有位置名称等的信息window。我使用 OSMDROID 和 OSMBONUSPACK。
问题:
标记按预期显示并且它的 onClickListener 确实触发,但如果我从 onQueryTextSubmit
调用我的 searchLocationOnMap
,则不会显示信息 window。如何解决?
不起作用(如果单击标记则不会显示任何信息 window):
searchViewMap?.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
val location = searchViewMap?.query.toString()
if (location != null && location != "") {
try {
searchLocationOnMap("Moosach Bhf")
} catch (e: java.lang.Exception) {
Timber.d("Error while searching for location on map")
}
}
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
return true
}
})
出于某种原因,如果我从 onViewCreated
或从常规 button.setOnClickListener{}
.
searchLocationOnMap
,则会显示
有效(单击标记时显示信息window):
btn.setOnClickListener { searchLocationOnMap("Moosach Bhf") }
这是 searchLocationOnMap():
private fun searchLocationOnMap(keyword: String) = GlobalScope.launch(Dispatchers.Main) {
removeOldLocationsFromMap()
val locationAddress = withContext(Dispatchers.Default) { obtainAddressFromKeyword(keyword) }
if (locationAddress == null) {
Toast.makeText(context, "No address found for keyword $keyword", Toast.LENGTH_SHORT).show()
} else {
val marker = createMarkerFromAddress(locationAddress)
mPOIOverlayFolder.add(marker)
map.controller.animateTo(marker.position)
map.invalidate()
}
这是 obtainAdressFromKeyword:
private fun obtainAddressFromKeyword(keyword: String): Address? {
val result = GeocoderNominatim("TestUserAgent").getFromLocationName(keyword, 1).firstOrNull()
Timber.d("result: $result")
return result
}
这是 createMarkerFromAddress:
private fun createMarkerFromAddress(address: Address): Marker {
val poiMarker = Marker(map)
poiMarker.title = address.getAddressLine(0)
poiMarker.snippet = "Lat : ${address.latitude},\n Long ${address.longitude}"
poiMarker.position = GeoPoint(address.latitude, address.longitude)
poiMarker.icon = ContextCompat.getDrawable(applicationContext, R.drawable.poi_black)
poiMarker.infoWindow = MarkerInfoWindow(R.layout.marker_info_bubble, map)
poiMarker.setOnMarkerClickListener { marker, map -> onMarkerClick(marker) }
return poiMarker
}
这是 onMarkerClick:
private fun onMarkerClick(marker: Marker) : Boolean {
marker.showInfoWindow()
marker.infoWindow.view.bringToFront()
Timber.d("Clicked Marker ${marker.title}, infowindowshown: ${marker.isInfoWindowShown}")
return true
木材结果(在两种情况下):
Clicked Marker Memminger Platz, infowindowshown: true
我是这样解决的
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Log.i("MyApp", searchView.getQuery().toString());
consultarListaParadas(searchView.getQuery().toString());
getActivity().runOnUiThread(new Runnable() {
public void run() {
adaptadorListaParadasBus.setListaParadas(listaParadas);
adaptadorListaParadasBus.notifyDataSetChanged();
}
});
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if(newText.equals("")){
this.onQueryTextSubmit("");
}
return true;
}
});
map.parent 视图必须失效。