Android Google Maps API V2 Zoom to Current Location

前端 未结 9 1715
时光说笑
时光说笑 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 ))
            }
        }
    
    0 讨论(0)
  • 2020-11-28 05:27
    private void setUpMapIfNeeded(){
        if (mMap == null){
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();//invoke of map fragment by id from main xml file
    
         if (mMap != null) {
             mMap.setMyLocationEnabled(true);//Makes the users current location visible by displaying a blue dot.
    
             LocationManager lm=(LocationManager)getSystemService(LOCATION_SERVICE);//use of location services by firstly defining location manager.
             String provider=lm.getBestProvider(new Criteria(), true);
    
             if(provider==null){
                 onProviderDisabled(provider);
                  }
             Location loc=lm.getLastKnownLocation(provider);
    
    
             if (loc!=null){
                 onLocationChanged(loc);
          }
             }
         }
    }
    
        // Initialize map options. For example:
        // mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    
    @Override
    public void onLocationChanged(Location location) {
    
       LatLng latlng=new LatLng(location.getLatitude(),location.getLongitude());// This methods gets the users current longitude and latitude.
    
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));//Moves the camera to users current longitude and latitude
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,(float) 14.6));//Animates camera and zooms to preferred state on the user's current location.
    }
    
        // TODO Auto-generated method stub
    

    0 讨论(0)
  • 2020-11-28 05:30

    After you instansiated the map object (from the fragment) add this -

    private void centerMapOnMyLocation() {
    
        map.setMyLocationEnabled(true);
    
        location = map.getMyLocation();
    
        if (location != null) {
            myLocation = new LatLng(location.getLatitude(),
                    location.getLongitude());
        }
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation,
                Constants.MAP_ZOOM));
    }
    

    if you need any guidance just ask but it should be self explantory - just instansiate the myLocation object for a default one...

    0 讨论(0)
  • 2020-11-28 05:30

    This is working Current Location with zoom for Google Map V2

     double lat= location.getLatitude();
     double lng = location.getLongitude();
     LatLng ll = new LatLng(lat, lng);
     googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
    
    0 讨论(0)
  • 2020-11-28 05:31
    youmap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentlocation, 16));
    

    16 is the zoom level

    0 讨论(0)
  • 2020-11-28 05:33

    Try this coding:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    
    Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
    if (location != null)
    {
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));
    
        CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
            .zoom(17)                   // Sets the zoom
            .bearing(90)                // Sets the orientation of the camera to east
            .tilt(40)                   // Sets the tilt of the camera to 30 degrees
            .build();                   // Creates a CameraPosition from the builder
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));       
    }
    
    0 讨论(0)
提交回复
热议问题