Java Code Examples for com.mapbox.geojson.Point#latitude()

The following examples show how to use com.mapbox.geojson.Point#latitude() . 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: 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 2
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 3
Source File: TurfJoins.java    From mapbox-java with MIT License 6 votes vote down vote up
private static boolean inRing(Point pt, List<Point> ring) {
  boolean isInside = false;

  for (int i = 0, j = ring.size() - 1; i < ring.size(); j = i++) {
    double xi = ring.get(i).longitude();
    double yi = ring.get(i).latitude();
    double xj = ring.get(j).longitude();
    double yj = ring.get(j).latitude();
    boolean intersect = (
      (yi > pt.latitude()) != (yj > pt.latitude()))
      && (pt.longitude() < (xj - xi) * (pt.latitude() - yi) / (yj - yi) + xi);
    if (intersect) {
      isInside = !isInside;
    }
  }
  return isInside;
}
 
Example 4
Source File: TurfMeasurement.java    From mapbox-java with MIT License 6 votes vote down vote up
private static double[] bboxCalculator(List<Point> resultCoords) {
  double[] bbox = new double[4];

  bbox[0] = Double.POSITIVE_INFINITY;
  bbox[1] = Double.POSITIVE_INFINITY;
  bbox[2] = Double.NEGATIVE_INFINITY;
  bbox[3] = Double.NEGATIVE_INFINITY;

  for (Point point : resultCoords) {
    if (bbox[0] > point.longitude()) {
      bbox[0] = point.longitude();
    }
    if (bbox[1] > point.latitude()) {
      bbox[1] = point.latitude();
    }
    if (bbox[2] < point.longitude()) {
      bbox[2] = point.longitude();
    }
    if (bbox[3] < point.latitude()) {
      bbox[3] = point.latitude();
    }
  }
  return bbox;
}
 
Example 5
Source File: PolylineUtils.java    From mapbox-java with MIT License 6 votes vote down vote up
/**
 * Square distance from a point to a segment.
 *
 * @param point {@link Point} whose distance from segment needs to be determined
 * @param p1,p2 points defining the segment
 * @return square of the distance between first input point and segment defined by
 *   other two input points
 */
private static double getSqSegDist(Point point, Point p1, Point p2) {
  double horizontal = p1.longitude();
  double vertical = p1.latitude();
  double diffHorizontal = p2.longitude() - horizontal;
  double diffVertical = p2.latitude() - vertical;

  if (diffHorizontal != 0 || diffVertical != 0) {
    double total = ((point.longitude() - horizontal) * diffHorizontal + (point.latitude()
      - vertical) * diffVertical) / (diffHorizontal * diffHorizontal + diffVertical
      * diffVertical);
    if (total > 1) {
      horizontal = p2.longitude();
      vertical = p2.latitude();

    } else if (total > 0) {
      horizontal += diffHorizontal * total;
      vertical += diffVertical * total;
    }
  }

  diffHorizontal = point.longitude() - horizontal;
  diffVertical = point.latitude() - vertical;

  return diffHorizontal * diffHorizontal + diffVertical * diffVertical;
}
 
Example 6
Source File: NavigationMapboxMap.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@NonNull
private Marker createMarkerFromIcon(Context context, Point position) {
  LatLng markerPosition = new LatLng(position.latitude(),
    position.longitude());
  Icon markerIcon = ThemeSwitcher.retrieveThemeMapMarker(context);
  return mapboxMap.addMarker(new MarkerOptions()
    .position(markerPosition)
    .icon(markerIcon));
}
 
Example 7
Source File: DynamicCameraTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@Test
public void onInformationFromRoute_engineCreatesCorrectTarget() throws Exception {
  DynamicCamera cameraEngine = buildDynamicCamera();
  RouteInformation routeInformation = RouteInformation.create(buildDirectionsRoute(), null, null);

  Point target = cameraEngine.target(routeInformation);

  double lng = target.longitude();
  assertEquals(-122.416686, lng);
  double lat = target.latitude();
  assertEquals(37.783425, lat);
}
 
Example 8
Source File: DynamicCameraTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@Test
public void onInformationFromLocationAndProgress_engineCreatesCorrectTarget() throws Exception {
  DynamicCamera cameraEngine = buildDynamicCamera();
  RouteInformation routeInformation = RouteInformation.create(null,
    buildDefaultLocationUpdate(-77.0339782574523, 38.89993519985637), buildDefaultRouteProgress(null));

  Point target = cameraEngine.target(routeInformation);

  double lng = target.longitude();
  assertEquals(-77.0339782574523, lng);
  double lat = target.latitude();
  assertEquals(38.89993519985637, lat);
}
 
Example 9
Source File: NavigationLauncherActivity.java    From graphhopper-navigation-example with Apache License 2.0 5 votes vote down vote up
private void updateRouteAfterWaypointChange() {
    if (this.waypoints.isEmpty()) {
        hideLoading();
    } else {
        Point lastPoint = this.waypoints.get(this.waypoints.size() - 1);
        LatLng latLng = new LatLng(lastPoint.latitude(), lastPoint.longitude());
        setCurrentMarkerPosition(latLng);
        if (this.waypoints.size() > 0) {
            fetchRoute();
        } else {
            hideLoading();
        }
    }
}
 
Example 10
Source File: AirMapMapView.java    From AirMapSDK-Android with Apache License 2.0 5 votes vote down vote up
private void zoomToFeatureIfNecessary(Feature featureClicked) {
    LatLngBounds cameraBounds = getMap().getProjection().getVisibleRegion().latLngBounds;
    LatLngBounds.Builder advisoryLatLngsBuilder = new LatLngBounds.Builder();
    boolean zoom = false;

    Geometry geometry = featureClicked.geometry();
    List<Point> points = new ArrayList<>();
    if (geometry instanceof Polygon) {
        points.addAll(((Polygon) geometry).outer().coordinates());
    } else if (geometry instanceof MultiPolygon) {
        List<Polygon> polygons = ((MultiPolygon) geometry).polygons();
        for (Polygon polygon : polygons) {
            points.addAll(polygon.outer().coordinates());
        }
    }

    for (Point position : points) {
        LatLng latLng = new LatLng(position.latitude(), position.longitude());
        advisoryLatLngsBuilder.include(latLng);
        if (!cameraBounds.contains(latLng)) {
            Timber.d("Camera position doesn't contain point");
            zoom = true;
        }
    }

    if (zoom) {
        int padding = Utils.dpToPixels(getContext(), 72).intValue();
        getMap().moveCamera(CameraUpdateFactory.newLatLngBounds(advisoryLatLngsBuilder.build(), padding));
    }
}
 
Example 11
Source File: PolylineUtils.java    From mapbox-java with MIT License 2 votes vote down vote up
/**
 * Square distance between 2 points.
 *
 * @param p1 first {@link Point}
 * @param p2 second Point
 * @return square of the distance between two input points
 */
private static double getSqDist(Point p1, Point p2) {
  double dx = p1.longitude() - p2.longitude();
  double dy = p1.latitude() - p2.latitude();
  return dx * dx + dy * dy;
}