Show Current Location inside Google Map Fragment

前端 未结 2 400
迷失自我
迷失自我 2020-11-28 15:08

I am currently working on a Fragment with a Google Map in it. Once a user visits that fragment his map should zoom and focus on his location. However, it shows map of the wo

2条回答
  •  情深已故
    2020-11-28 15:23

    Sorry but that's just much too much overhead (above), short and quick, if you have the MapFragment, you also have to map, just do the following:

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                googleMap.setMyLocationEnabled(true)
    } else {
        // Show rationale and request permission.
    }
    

    zoom works like that:

    val cameraPosition = CameraPosition.Builder().target(LatLng(location.latitude, location.longitude)).zoom(17.0.toFloat()).build()
                val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition)
                googleMap?.moveCamera(cameraUpdate)
    

    Code is in Kotlin, hope you don't mind. 17.0f is some quite detailed zoom level. Just try different values.

    have fun

提交回复
热议问题