【问题标题】:How to zoom in on the actual location of the device?如何放大设备的实际位置?
【发布时间】:2019-09-23 16:47:21
【问题描述】:

我无法放大设备的当前位置,有人可以帮我吗? 欢迎任何帮助。

override fun onMapReady(googleMap: GoogleMap) {
    Log.i("MAP READY", "READY")
    val position = if (currentLocation != null) LatLng(currentLocation!!.latitude, currentLocation!!.longitude) else null
    this.map = googleMap
    
    getFineLocationPermission()
    this.map!!.setOnMarkerClickListener(this)
    this.map!!.uiSettings.isRotateGesturesEnabled = true
    this.map!!.uiSettings.isZoomGesturesEnabled = true
    this.map!!.setOnInfoWindowClickListener(this)
    this.map!!.setOnMapLongClickListener(this)


}

【问题讨论】:

    标签: google-maps android-studio kotlin


    【解决方案1】:

    通常,您可以通过更改相机位置来更改地图的缩放级别。据此documentation

    To change the position of the camera, you must specify where you want to move the camera, using a CameraUpdate. The Maps API allows you to create many different types of CameraUpdate using CameraUpdateFactory.

    由于您没有提供完整的代码,让我在下面插入示例:

    • 如果您想保持所有其他地图属性相同并仅更改缩放级别,则可以使用此行:

      //where zoomLevel is your preferred zoom level
      mMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel)) 
      
    • 如果您想在放置标记的位置缩放地图,那么在您的addMarker() 函数下方,您可以添加此行以在City 级别上缩放地图:

      // zoomLevel is set at 10 for a City-level view
      val zoomLevel = 10
      // latLng contains the coordinates where the marker is added
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,zoomLevel)) 
      

      有趣的提示:你可以 动画 如果你使用相机视图更新 animateCamera() 而不是使用 moveCamera()

    有关 Android 版 Maps SDK - 相机和视图 设置的更多信息,请参阅https://developers.google.com/maps/documentation/android-sdk/views

    【讨论】:

      【解决方案2】:

      你可以更好地优化你的代码

      override fun onMapReady(googleMap: GoogleMap) {
      Log.i("MAP READY", "READY")
      val position = if (currentLocation != null) LatLng(currentLocation!!.latitude, 
      currentLocation!!.longitude) else null
      this.map = googleMap
      
      getFineLocationPermission()
      this.map?.let{
      it.setOnMarkerClickListener(this)
      it.uiSettings.isRotateGesturesEnabled = true
      it.uiSettings.isZoomGesturesEnabled = true
      it.setOnInfoWindowClickListener(this)
      it.setOnMapLongClickListener(this)
      //zooming into the map
      it.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,zoomLevel)) 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-11
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        • 2021-03-18
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多