diff options
author | Peng Liu <peng.liu@mapbox.com> | 2019-12-17 18:05:00 +0200 |
---|---|---|
committer | Peng Liu <peng.liu@mapbox.com> | 2020-01-02 22:56:21 +0200 |
commit | 1fc860117b8a5230668dae187e6c2ab3aac0d269 (patch) | |
tree | 9acdcd7baf209e652837b6731081ee8f38d596b6 | |
parent | 6d876f1d6bf95827598809b965ed6a6e06008675 (diff) | |
download | qtlocation-mapboxgl-1fc860117b8a5230668dae187e6c2ab3aac0d269.tar.gz |
[android] Add getVisibleCoordinateBounds method.
-rw-r--r-- | platform/android/src/native_map_view.cpp | 59 | ||||
-rwxr-xr-x | platform/android/src/native_map_view.hpp | 2 |
2 files changed, 61 insertions, 0 deletions
diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 554a59de76..db3fbddeeb 100644 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -529,6 +529,64 @@ void NativeMapView::setVisibleCoordinateBounds(JNIEnv& env, const jni::Array<jni map->easeTo(cameraOptions, animationOptions); } +void NativeMapView::getVisibleCoordinateBounds(JNIEnv& env, + jint screenWidth, + jint screenHeight, + jni::Array<jdouble>& output, + jfloat pixelRatio_) { + mbgl::LatLng southEast = map->latLngForPixel(mbgl::ScreenCoordinate(screenWidth / pixelRatio_, screenHeight / pixelRatio_)); + mbgl::LatLng northEast = map->latLngForPixel(mbgl::ScreenCoordinate(screenWidth / pixelRatio_, 0.0)); + mbgl::LatLng southWest = map->latLngForPixel(mbgl::ScreenCoordinate(0.0, screenHeight / pixelRatio_)); + mbgl::LatLng northWest = map->latLngForPixel(mbgl::ScreenCoordinate(0.0, 0.0)); + mbgl::LatLng center = map->latLngForPixel(mbgl::ScreenCoordinate(screenWidth / (2 * pixelRatio_), screenHeight / (2 * pixelRatio_))); + + double latitudes[] = {southEast.latitude(), northEast.latitude(), southWest.latitude(), northWest.latitude()}; + double longitudes[] = {southEast.longitude(), northEast.longitude(), southWest.longitude(), northWest.longitude()}; + + auto latitudeMinMax = std::minmax_element(std::begin(latitudes), std::end(latitudes)); + + double latNorth = *(latitudeMinMax.second); + double latSouth = *(latitudeMinMax.first); + + double lonEast = -180.0; + double lonWest = 180.0; + double centerLongitude = center.longitude(); + auto longitudeMinMax = std::minmax_element(std::begin(longitudes), std::end(longitudes)); + + // check if the bounds is over dateline + if (std::abs(centerLongitude - (*(longitudeMinMax.first) + *(longitudeMinMax.second)) / 2) > 0.1) { + for (int i = 0; i < 4; i++) { + double longitude = longitudes[i]; + if (longitude < 0) { + if (lonEast < longitude) { + lonEast = longitude; + } + } else { + if (lonWest > longitude) { + lonWest = longitude; + } + } + } + } else { + lonEast = *(longitudeMinMax.second); + lonWest = *(longitudeMinMax.first); + } + + // lonEast should be no less than lonWest + lonEast = lonEast > lonWest ? lonEast : lonEast + 360; + + std::vector<jdouble> buffer; + buffer.reserve(4); + + // Order of the LatLngBounds: double latNorth, double lonEast, double latSouth, double lonWest + buffer.push_back(latNorth); + buffer.push_back(lonEast); + buffer.push_back(latSouth); + buffer.push_back(lonWest); + + output.SetRegion<std::vector<jdouble>>(env, 0, buffer); +} + void NativeMapView::scheduleSnapshot(jni::JNIEnv&) { mapRenderer.requestSnapshot([&](PremultipliedImage image) { auto _env = android::AttachEnv(); @@ -1172,6 +1230,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) { METHOD(&NativeMapView::projectedMetersForLatLng, "nativeProjectedMetersForLatLng"), METHOD(&NativeMapView::pixelForLatLng, "nativePixelForLatLng"), METHOD(&NativeMapView::pixelsForLatLngs, "nativePixelsForLatLngs"), + METHOD(&NativeMapView::getVisibleCoordinateBounds, "nativeGetVisibleCoordinateBounds"), METHOD(&NativeMapView::latLngForProjectedMeters, "nativeLatLngForProjectedMeters"), METHOD(&NativeMapView::latLngForPixel, "nativeLatLngForPixel"), METHOD(&NativeMapView::latLngsForPixels, "nativeLatLngsForPixels"), diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 622c454b7d..0823e53509 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -139,6 +139,8 @@ public: void setVisibleCoordinateBounds(JNIEnv&, const jni::Array<jni::Object<LatLng>>&, const jni::Object<RectF>&, jni::jdouble, jni::jlong); + void getVisibleCoordinateBounds(JNIEnv& env, jint screenWidth, jint screenHeight, jni::Array<jdouble>& output, jfloat pixelRatio_); + void scheduleSnapshot(jni::JNIEnv&); jni::Local<jni::Object<CameraPosition>> getCameraPosition(jni::JNIEnv&); |