简体   繁体   中英

how i can zoom directly to my current location in android application maps

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap.setMyLocationEnabled(true);
    LatLng bangalore = new LatLng(12.9667, 77.5667);
    mMap.addMarker(new MarkerOptions().position(bangalore).title("Bengaluru"));
    float zoomLevel = 12;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bangalore, zoomLevel));
}

that code zooming to bangalore area but i need for my current location

Use this code location.getLatitude(), location.getLongitude() used to get current location

                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));
                }

pass your current location in this function where you have placed your marker.

private void moveToCurrentLocation(LatLng currentLocation)
  {   
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,15));
  // Zoom in, animating the camera.
    mMap.animateCamera(CameraUpdateFactory.zoomIn());
 // animating with a duration of 2 seconds.
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
  }
   LatLngBounds.Builder   builder = new LatLngBounds.Builder();

  mMap.getMap().setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
             @Override
             public void onMyLocationChange(Location location) {
                if (location != null) {
                   LatLng latlng = new LatLng(location.getLatitude(), location.getLatitude());
                   builder.include(latlng);

                   LatLngBounds bounds = builder.build();
                   int padding = getScreenWidth(activity) / 10;
                   CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
                   mMap.getMap().animateCamera(cu);
                }

             }
          });

public static int getScreenWidth(Activity activity) {
      Display display = activity.getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      return size.x;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM