标记未在 Android 中的地图上显示
Posted
技术标签:
【中文标题】标记未在 Android 中的地图上显示【英文标题】:Marker not showing on map in Android 【发布时间】:2021-12-18 21:48:05 【问题描述】:我刚刚创建了一个 Google 地图片段。标记 sydney 没有显示...我忘了添加什么吗?我已经添加了我的 Google 地图密钥
class fragment_map : Fragment()
private val callback = OnMapReadyCallback googleMap ->
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera.
* In this case, we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to
* install it inside the SupportMapFragment. This method will only be triggered once the
* user has installed Google Play services and returned to the app.
*/
val sydney = LatLng(-34.0, 151.0)
googleMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View?
return inflater.inflate(R.layout.fragment_map, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(callback)
【问题讨论】:
【参考方案1】:您的 onMapReady 似乎不完整。试试这个吧?
class fragment_map : Fragment()
private val callback = OnMapReadyCallback googleMap ->
override fun onMapReady(googleMap: GoogleMap)
mMap = googleMap
val boundsBuilder = LatLngBounds.Builder()
val sydney = LatLng(-34.0, 151.0)
boundsBuilder.include(latlng)
Map.addMarker(MarkerOptions().position(latlng).title("Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 1000, 1000, 0))
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View?
return inflater.inflate(R.layout.fragment_map, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(callback)
【讨论】:
我需要创建一个新函数吗?因为它似乎不起作用。 尝试按照本教程进行操作 ->https://www.youtube.com/watch?v=m_H3JuybsJ0
了解发生了什么。但本质上,您需要在代码中具有该功能。您可以将其复制并粘贴到您的 onCreate
函数上方。我将编辑我的答案,以反映它。以上是关于标记未在 Android 中的地图上显示的主要内容,如果未能解决你的问题,请参考以下文章
React Native mapview:标记未在IOS上显示
在我的屏幕上显示给我的区域中的android上的地图中显示标记