com.mapbox.mapboxsdk.camera.CameraPosition Java Examples

The following examples show how to use com.mapbox.mapboxsdk.camera.CameraPosition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From WhereAreTheEyes with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void recenterCamera(View view) {
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {
            Location loc = gps.getLocation();
            if( loc == null )
                return;
            CameraPosition position = new CameraPosition.Builder()
                    .target(new LatLng(loc.getLatitude(), loc.getLongitude()))
                    .zoom(17)
                    .bearing(0)
                    .build();
            mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 3000);
        }
    });
}
 
Example #2
Source File: NavigationCamera.java    From graphhopper-navigation-android with MIT License 6 votes vote down vote up
/**
 * Creates an initial animation with the given {@link RouteInformation#route()}.
 * <p>
 * This is the first animation that fires prior to receiving progress updates.
 * <p>
 * If a user interacts with the {@link MapboxMap} while the animation is in progress,
 * the animation will be cancelled.  So it's important to add the {@link ProgressChangeListener}
 * in both onCancel() and onFinish() scenarios.
 *
 * @param routeInformation with route data
 */
private void animateCameraFromRoute(RouteInformation routeInformation) {

  Camera cameraEngine = navigation.getCameraEngine();

  Point targetPoint = cameraEngine.target(routeInformation);
  LatLng targetLatLng = new LatLng(targetPoint.latitude(), targetPoint.longitude());
  double bearing = cameraEngine.bearing(routeInformation);
  double zoom = cameraEngine.zoom(routeInformation);

  CameraPosition position = new CameraPosition.Builder()
    .target(targetLatLng)
    .bearing(bearing)
    .zoom(zoom)
    .build();

  updateMapCameraPosition(position, new AddProgressListenerCancelableCallback(navigation, progressChangeListener));
}
 
Example #3
Source File: MainActivity.java    From WhereAreTheEyes with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void recenterMap() {
    final Location l = gps.getLocation();

    // This shouldn't normally happen, but does on the Android emulator.
    // It only occurs if recenterMap is called before we receive our first
    // gps ping. We normally receive a ping within a second or two of app
    // launch, but *not* if the emulator only pings on demand.
    if( l == null )
        return;

    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {
            // We'll maintain zoom level and tilt, just want to change position
            CameraPosition old = mapboxMap.getCameraPosition();
            CameraPosition pos = new CameraPosition.Builder()
                    .target(new LatLng(l.getLatitude(), l.getLongitude()))
                    .zoom(old.zoom)
                    .tilt(old.tilt)
                    .build();
            mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));
        }
    });
}
 
Example #4
Source File: FillChangeActivity.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // configure initial map state
  MapboxMapOptions options = new MapboxMapOptions()
    .attributionTintColor(RED_COLOR)
    .compassFadesWhenFacingNorth(false)
    .camera(new CameraPosition.Builder()
      .target(new LatLng(45.520486, -122.673541))
      .zoom(12)
      .tilt(40)
      .build());

  // create map
  mapView = new MapView(this, options);
  mapView.setId(R.id.mapView);
  mapView.onCreate(savedInstanceState);
  mapView.getMapAsync(this);

  setContentView(mapView);
}
 
Example #5
Source File: DynamicCamera.java    From graphhopper-navigation-android with MIT License 6 votes vote down vote up
/**
 * Creates a camera position with the current location and upcoming maneuver location.
 * <p>
 * Using {@link MapboxMap#getCameraForLatLngBounds(LatLngBounds, int[])} with a {@link LatLngBounds}
 * that includes the current location and upcoming maneuver location.
 *
 * @param location      for current location
 * @param routeProgress for upcoming maneuver location
 * @return camera position that encompasses both locations
 */
private CameraPosition createCameraPosition(Location location, RouteProgress routeProgress) {
  LegStep upComingStep = routeProgress.currentLegProgress().upComingStep();
  if (upComingStep != null) {
    Point stepManeuverPoint = upComingStep.maneuver().location();

    List<LatLng> latLngs = new ArrayList<>();
    LatLng currentLatLng = new LatLng(location);
    LatLng maneuverLatLng = new LatLng(stepManeuverPoint.latitude(), stepManeuverPoint.longitude());
    latLngs.add(currentLatLng);
    latLngs.add(maneuverLatLng);

    if (latLngs.size() < 1 || currentLatLng.equals(maneuverLatLng)) {
      return mapboxMap.getCameraPosition();
    }

    LatLngBounds cameraBounds = new LatLngBounds.Builder()
      .includes(latLngs)
      .build();

    int[] padding = {0, 0, 0, 0};
    return mapboxMap.getCameraForLatLngBounds(cameraBounds, padding);
  }
  return mapboxMap.getCameraPosition();
}
 
Example #6
Source File: PlacePickerActivity.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Bind the device location Floating Action Button to this activity's UI and move the
 * map camera if the button's clicked.
 */
private void addUserLocationButton() {
  userLocationButton.show();
  userLocationButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      if (mapboxMap.getLocationComponent().getLastKnownLocation() != null) {
        Location lastKnownLocation = mapboxMap.getLocationComponent().getLastKnownLocation();
        mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(
          new CameraPosition.Builder()
            .target(new LatLng(lastKnownLocation.getLatitude(),
                lastKnownLocation.getLongitude()))
            .zoom(17.5)
            .build()
        ),1400);
      } else {
        Toast.makeText(PlacePickerActivity.this,
          getString(R.string.mapbox_plugins_place_picker_user_location_not_found), Toast.LENGTH_SHORT).show();
      }
    }
  });
}
 
Example #7
Source File: PressForSymbolActivity.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_annotation);
  AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
  mapView = findViewById(R.id.mapView);
  mapView.onCreate(savedInstanceState);
  mapView.getMapAsync(map -> {
    mapboxMap = map;
    mapboxMap.setCameraPosition(new CameraPosition.Builder()
      .target(new LatLng(60.169091, 24.939876))
      .zoom(12)
      .tilt(20)
      .bearing(90)
      .build()
    );
    mapboxMap.addOnMapLongClickListener(this::addSymbol);
    mapboxMap.addOnMapClickListener(this::addSymbol);
    mapboxMap.setStyle(getStyleBuilder(Style.MAPBOX_STREETS), style -> {
      findViewById(R.id.fabStyles).setOnClickListener(v ->
        mapboxMap.setStyle(getStyleBuilder(Utils.INSTANCE.getNextStyle())));

      symbolManager = new SymbolManager(mapView, mapboxMap, style);
      symbolManager.setIconAllowOverlap(true);
      symbolManager.setTextAllowOverlap(true);
    });
  });
}
 
Example #8
Source File: ComponentNavigationActivity.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@NonNull
private CameraPosition buildCameraPositionFrom(Location location, double bearing) {
  return new CameraPosition.Builder()
    .zoom(DEFAULT_ZOOM)
    .target(new LatLng(location.getLatitude(), location.getLongitude()))
    .bearing(bearing)
    .tilt(DEFAULT_TILT)
    .build();
}
 
Example #9
Source File: ComponentNavigationActivity.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private void moveCameraOverhead() {
  if (lastLocation == null) {
    return;
  }
  CameraPosition cameraPosition = buildCameraPositionFrom(lastLocation, DEFAULT_BEARING);
  navigationMap.retrieveMap().animateCamera(
    CameraUpdateFactory.newCameraPosition(cameraPosition), TWO_SECONDS_IN_MILLISECONDS
  );
}
 
Example #10
Source File: ComponentNavigationActivity.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private void moveCameraToInclude(Point destination) {
  LatLng origin = new LatLng(lastLocation);
  LatLngBounds bounds = new LatLngBounds.Builder()
    .include(origin)
    .include(new LatLng(destination.latitude(), destination.longitude()))
    .build();
  Resources resources = getResources();
  int routeCameraPadding = (int) resources.getDimension(R.dimen.component_navigation_route_camera_padding);
  int[] padding = {routeCameraPadding, routeCameraPadding, routeCameraPadding, routeCameraPadding};
  CameraPosition cameraPosition = navigationMap.retrieveMap().getCameraForLatLngBounds(bounds, padding);
  navigationMap.retrieveMap().animateCamera(
    CameraUpdateFactory.newCameraPosition(cameraPosition), TWO_SECONDS_IN_MILLISECONDS
  );
}
 
Example #11
Source File: PlacePickerTest.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
@Ignore
public void getLastCameraPosition() throws Exception {
  CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(new LatLng(2.0, 3.0))
    .bearing(30)
    .zoom(20)
    .build();

  Parcel parcel = mock(Parcel.class);
  cameraPosition.writeToParcel(parcel, 0);
  parcel.setDataPosition(0);

  Intent data = mock(Intent.class);
  data.putExtra(PlaceConstants.MAP_CAMERA_POSITION, cameraPosition);
  CameraPosition position = PlacePicker.getLastCameraPosition(data);
  assertNotNull(position);
}
 
Example #12
Source File: RerouteActivity.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
  if (tracking) {
    locationLayerPlugin.forceLocationUpdate(location);
    CameraPosition cameraPosition = new CameraPosition.Builder()
      .zoom(15)
      .target(new LatLng(location.getLatitude(), location.getLongitude()))
      .bearing(location.getBearing())
      .build();
    mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 2000);
  }
  instructionView.update(routeProgress);
}
 
Example #13
Source File: DynamicCamera.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
/**
 * Creates a zoom value based on the result of {@link MapboxMap#getCameraForLatLngBounds(LatLngBounds, int[])}.
 * <p>
 * 0 zoom is the world view, while 22 (default max threshold) is the closest you can position
 * the camera to the map.
 *
 * @param routeInformation for current location and progress
 * @return zoom within set min / max bounds
 */
private double createZoom(RouteInformation routeInformation) {
  CameraPosition position = createCameraPosition(routeInformation.location(), routeInformation.routeProgress());
  if (position.zoom > MAX_CAMERA_ZOOM) {
    return MAX_CAMERA_ZOOM;
  } else if (position.zoom < MIN_CAMERA_ZOOM) {
    return MIN_CAMERA_ZOOM;
  }
  return position.zoom;
}
 
Example #14
Source File: PlacePicker.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static CameraPosition getLastCameraPosition(Intent data) {
  return data.getParcelableExtra(PlaceConstants.MAP_CAMERA_POSITION);
}
 
Example #15
Source File: PlacePickerOptions.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Nullable
public abstract CameraPosition statingCameraPosition();
 
Example #16
Source File: DynamicSymbolChangeActivity.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_annotation);

  mapView = findViewById(R.id.mapView);
  mapView.setTag(false);
  mapView.onCreate(savedInstanceState);
  mapView.getMapAsync(mapboxMap -> {
    DynamicSymbolChangeActivity.this.mapboxMap = mapboxMap;

    LatLng target = new LatLng(51.506675, -0.128699);

    mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(
      new CameraPosition.Builder()
        .bearing(90)
        .tilt(40)
        .zoom(10)
        .target(target)
        .build()
    ));

    mapboxMap.setStyle(new Style.Builder()
        .fromUri(Style.MAPBOX_STREETS)
        .withImage(ID_ICON_1, generateBitmap(R.drawable.mapbox_ic_place), true)
        .withImage(ID_ICON_2, generateBitmap(R.drawable.mapbox_ic_offline), true)
      , style -> {
        symbolManager = new SymbolManager(mapView, mapboxMap, style);
        symbolManager.setIconAllowOverlap(true);
        symbolManager.setTextAllowOverlap(true);

        // Create Symbol
        SymbolOptions SymbolOptions = new SymbolOptions()
          .withLatLng(LAT_LNG_CHELSEA)
          .withIconImage(ID_ICON_1);

        symbol = symbolManager.create(SymbolOptions);
      });


  });

  FloatingActionButton fab = findViewById(R.id.fabStyles);
  fab.setVisibility(MapView.VISIBLE);
  fab.setOnClickListener(view -> {
    if (mapboxMap != null) {
      updateSymbol();
    }
  });
}
 
Example #17
Source File: OfflineUtils.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static CameraPosition getCameraPosition(OfflineRegionDefinition definition) {
  return new CameraPosition.Builder()
    .target(definition.getBounds().getCenter())
    .zoom(definition.getMinZoom())
    .build();
}
 
Example #18
Source File: ScaleBarPlugin.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void invalidateScaleBar() {
  CameraPosition cameraPosition = mapboxMap.getCameraPosition();
  scaleBarWidget.setDistancePerPixel((projection.getMetersPerPixelAtLatitude(cameraPosition.target.getLatitude()))
    / mapView.getPixelRatio());
}
 
Example #19
Source File: NavigationLauncherActivity.java    From graphhopper-navigation-example with Apache License 2.0 4 votes vote down vote up
private void animateCameraBbox(LatLngBounds bounds, int animationTime, int[] padding) {
    CameraPosition position = mapboxMap.getCameraForLatLngBounds(bounds, padding);
    mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), animationTime);
}
 
Example #20
Source File: NavigationLauncherActivity.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
private void animateCameraBbox(LatLngBounds bounds, int animationTime, int[] padding) {
  CameraPosition position = mapboxMap.getCameraForLatLngBounds(bounds, padding);
  mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), animationTime);
}
 
Example #21
Source File: ComponentNavigationActivity.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
private void moveCameraTo(Location location) {
  CameraPosition cameraPosition = buildCameraPositionFrom(location, location.getBearing());
  navigationMap.retrieveMap().animateCamera(
    CameraUpdateFactory.newCameraPosition(cameraPosition), TWO_SECONDS_IN_MILLISECONDS
  );
}
 
Example #22
Source File: NavigationCamera.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
@NonNull
private CameraUpdate buildResetCameraUpdate() {
  CameraPosition resetPosition = new CameraPosition.Builder().tilt(0).bearing(0).build();
  return CameraUpdateFactory.newCameraPosition(resetPosition);
}
 
Example #23
Source File: RegionSelectionOptions.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Define the map's starting camera position by providing a camera position.
 *
 * @param cameraPosition the camera position which is where the {@link OfflineActivity} initial
 *                       map's camera position will be placed
 * @return this builder for chaining options together
 * @since 0.2.0
 */
public abstract Builder statingCameraPosition(@NonNull CameraPosition cameraPosition);
 
Example #24
Source File: RegionSelectionOptions.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Define the map's starting camera position by providing a camera position.
 *
 * @return the camera position which is where the {@link OfflineActivity} initial map's camera
 * position will be placed
 * @since 0.2.0
 */
@Nullable
public abstract CameraPosition statingCameraPosition();
 
Example #25
Source File: NavigationCamera.java    From graphhopper-navigation-android with MIT License 2 votes vote down vote up
/**
 * Will animate the {@link MapboxMap} to the given {@link CameraPosition} with the given duration.
 *
 * @param position to which the camera should animate
 * @param callback that will fire if the animation is cancelled or finished
 */
private void updateMapCameraPosition(CameraPosition position, MapboxMap.CancelableCallback callback) {
  mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1000, callback);
}
 
Example #26
Source File: NavigationCamera.java    From graphhopper-navigation-android with MIT License 2 votes vote down vote up
/**
 * Will ease the {@link MapboxMap} to the given {@link CameraPosition} with the given duration.
 *
 * @param position to which the camera should animate
 */
private void easeMapCameraPosition(CameraPosition position) {
  mapboxMap.easeCamera(CameraUpdateFactory.newCameraPosition(position),
    obtainLocationUpdateDuration(), false, null);
}
 
Example #27
Source File: PlacePickerOptions.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License votes vote down vote up
public abstract Builder statingCameraPosition(@NonNull CameraPosition cameraPosition);