Android Google Maps API V2 Zoom to Current Location

前端 未结 9 1776
时光说笑
时光说笑 2020-11-28 04:37

I\'m trying to mess around with the Maps API V2 to get more familiar with it, and I\'m trying to start the map centered at the user\'s current location. Using the map.

9条回答
  •  天命终不由人
    2020-11-28 05:25

    check this out:

        fun requestMyGpsLocation(context: Context, callback: (location: Location) -> Unit) {
            val request = LocationRequest()
            //        request.interval = 10000
            //        request.fastestInterval = 5000
            request.numUpdates = 1
            request.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
            val client = LocationServices.getFusedLocationProviderClient(context)
    
            val permission = ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION )
            if (permission == PackageManager.PERMISSION_GRANTED) {
                client.requestLocationUpdates(request, object : LocationCallback() {
                    override fun onLocationResult(locationResult: LocationResult?) {
                    val location = locationResult?.lastLocation
                    if (location != null)
                        callback.invoke(location)
                }
             }, null)
           }
         }
    

    and

        fun zoomOnMe() {
            requestMyGpsLocation(this) { location ->
                mMap?.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    LatLng(location.latitude,location.longitude ), 13F ))
            }
        }
    

提交回复
热议问题