我想在搜索视图中搜索位置,并使用标记在地图上显示该位置。单击标记后,我想显示一个带有位置名称等的信息窗口。我使用OSMDROID和OSMBONUSPACK。
问题:
标记显示为预期状态,并且其onClickListener确实触发,但是如果我从searchLocationOnMap
调用onQueryTextSubmit
,则不会显示信息窗口。该如何解决?
不起作用(如果单击标记,则不会显示任何信息窗口):
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
}
})
由于某些原因,显示了我是从searchLocationOnMap
还是常规onViewCreated
呼叫button.setOnClickListener{}
。
有效(如果单击标记,则显示信息窗口):
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()
}
这是getAdressFromKeyword:
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
木材结果(在两种情况下):
单击标记的Memminger Platz,信息窗口显示:是
答案 0 :(得分:0)
我用类似的方法解决它
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;
}
});
答案 1 :(得分:0)
必须禁用map.parent视图。