我如何直接缩放到Android应用程序地图中的当前位置

时间:2016-01-05 08:47:48

标签: android

test

该代码缩放到班加罗尔地区,但我需要我当前的位置

3 个答案:

答案 0 :(得分:0)

使用此代码 location.getLatitude(),location.getLongitude()来获取当前位置

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

答案 1 :(得分:0)

在您放置标记的此功能中传递当前位置。

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

答案 2 :(得分:0)

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