android - 标记未在 Android 中的地图上显示
问题描述
我刚刚创建了一个谷歌地图片段。标记 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)
}
}
解决方案
您的 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)
}
}
推荐阅读
- r - 如何使用 R 在大型数据帧中加速 forloop grep
- python - Pygame - 从图像的 x 和 y 坐标进行 blitting
- php - dyld:Apple M1 上 php oic8 的惰性符号绑定失败
- php - 为什么这些简单的 Laravel 查询这么慢?
- kotlin - 在 kotlin 中使用 getter 和 setter 时出错
- python-3.x - 对于这个特定示例,如何在 python 中进行字符串格式化
- tensorflow - 超过 grpc 截止日期的部署 tf-serving 问题
- python - 如何编写 python 脚本以在 Window 中运行现有的任务调度程序
- javascript - Lambda@Edge 在源请求上设置 Cookie
- java - 保存 Hibernate JPA 实体后外键为空