Fixed a crash when native code was accessing the LatLngBounds class (#655)

This commit is contained in:
Artak Kalantarian
2023-01-04 14:56:20 -08:00
committed by GitHub
parent f2c1b4ab4b
commit 558c1c0ccd
7 changed files with 114 additions and 72 deletions

View File

@@ -16,6 +16,8 @@ import com.mapbox.mapboxsdk.utils.isNaN
* This class does not wrap values to the world bounds.
*
*/
@Keep
class LatLngBounds
/**
* Construct a new LatLngBounds based on its corners, given in NESW
@@ -29,35 +31,43 @@ class LatLngBounds
* the NE point as (10, -170) and the SW point as (-10, 170),
* use (10, -190) and (-10, -170), or (10, -170) and (-10, -150).
*
* @param latNorth Northern Latitude
* @param lonEast Eastern Longitude
* @param latSouth Southern Latitude
* @param lonWest Western Longitude
* @param latitudeNorth Northern Latitude
* @param longitudeEast Eastern Longitude
* @param latitudeSouth Southern Latitude
* @param longitudeWest Western Longitude
*/ @Keep internal constructor(
/**
* Get the north latitude value of this bounds.
*
* @return double latitude value for north
*/
@field:Keep val latNorth: Double,
@field:Keep
@JvmField
val latitudeNorth: Double,
/**
* Get the east longitude value of this bounds.
*
* @return double longitude value for east
*/
@field:Keep val lonEast: Double,
@field:Keep
@JvmField
val longitudeEast: Double,
/**
* Get the south latitude value of this bounds.
*
* @return double latitude value for south
*/
@field:Keep val latSouth: Double,
@field:Keep
@JvmField
val latitudeSouth: Double,
/**
* Get the west longitude value of this bounds.
*
* @return double longitude value for west
*/
@field:Keep val lonWest: Double
@field:Keep
@JvmField
val longitudeWest: Double
) : Parcelable {
/**
@@ -68,26 +78,62 @@ class LatLngBounds
*/
val center: LatLng
get() {
val latCenter = (latNorth + latSouth) / 2.0
val longCenter = (lonEast + lonWest) / 2.0
val latCenter = (latitudeNorth + latitudeSouth) / 2.0
val longCenter = (longitudeEast + longitudeWest) / 2.0
return LatLng(latCenter, longCenter)
}
/**
* Get the north latitude value of this bounds.
*
* @return double latitude value for north
*/
fun getLatNorth(): Double {
return latitudeNorth
}
/**
* Get the south latitude value of this bounds.
*
* @return double latitude value for south
*/
fun getLatSouth(): Double {
return latitudeSouth
}
/**
* Get the east longitude value of this bounds.
*
* @return double longitude value for east
*/
fun getLonEast(): Double {
return longitudeEast
}
/**
* Get the west longitude value of this bounds.
*
* @return double longitude value for west
*/
fun getLonWest(): Double {
return longitudeWest
}
/**
* Get the latitude-longitude pair of the south west corner of this bounds.
*
* @return LatLng of the south west corner
*/
val southWest: LatLng
get() = LatLng(latSouth, lonWest)
get() = LatLng(latitudeSouth, longitudeWest)
/**
* Get the latitude-longitude paur if the north east corner of this bounds.
* Get the latitude-longitude pair if the north east corner of this bounds.
*
* @return LatLng of the north east corner
*/
val northEast: LatLng
get() = LatLng(latNorth, lonEast)
get() = LatLng(latitudeNorth, longitudeEast)
/**
* Get the latitude-longitude pair of the south east corner of this bounds.
@@ -95,7 +141,7 @@ class LatLngBounds
* @return LatLng of the south east corner
*/
val southEast: LatLng
get() = LatLng(latSouth, lonEast)
get() = LatLng(latitudeSouth, longitudeEast)
/**
* Get the latitude-longitude pair of the north west corner of this bounds.
@@ -103,7 +149,7 @@ class LatLngBounds
* @return LatLng of the north west corner
*/
val northWest: LatLng
get() = LatLng(latNorth, lonWest)
get() = LatLng(latitudeNorth, longitudeWest)
/**
* Get the area spanned by this LatLngBounds
@@ -120,7 +166,7 @@ class LatLngBounds
* @return Span distance
*/
val latitudeSpan: Double
get() = Math.abs(latNorth - latSouth)
get() = Math.abs(latitudeNorth - latitudeSouth)
/**
* Get the absolute distance, in degrees, between the west and
@@ -129,7 +175,7 @@ class LatLngBounds
* @return Span distance
*/
val longitudeSpan: Double
get() = Math.abs(lonEast - lonWest)
get() = Math.abs(longitudeEast - longitudeWest)
/**
* Validate if LatLngBounds is empty, determined if absolute distance is
@@ -145,7 +191,7 @@ class LatLngBounds
* @return the string representation
*/
override fun toString(): String {
return ("N:" + latNorth + "; E:" + lonEast + "; S:" + latSouth + "; W:" + lonWest)
return ("N:" + latitudeNorth + "; E:" + longitudeEast + "; S:" + latitudeSouth + "; W:" + longitudeWest)
}
/**
@@ -178,17 +224,17 @@ class LatLngBounds
return true
}
if (other is LatLngBounds) {
return latNorth == other.latNorth && latSouth == other.latSouth && lonEast == other.lonEast && lonWest == other.lonWest
return latitudeNorth == other.latitudeNorth && latitudeSouth == other.latitudeSouth && longitudeEast == other.longitudeEast && longitudeWest == other.longitudeWest
}
return false
}
private fun containsLatitude(latitude: Double): Boolean {
return latitude <= latNorth && latitude >= latSouth
return latitude <= latitudeNorth && latitude >= latitudeSouth
}
private fun containsLongitude(longitude: Double): Boolean {
return longitude <= lonEast && longitude >= lonWest
return longitude <= longitudeEast && longitude >= longitudeWest
}
/**
@@ -218,7 +264,7 @@ class LatLngBounds
* @return LatLngBounds
*/
fun union(bounds: LatLngBounds): LatLngBounds {
return unionNoParamCheck(bounds.latNorth, bounds.lonEast, bounds.latSouth, bounds.lonWest)
return unionNoParamCheck(bounds.latitudeNorth, bounds.longitudeEast, bounds.latitudeSouth, bounds.longitudeWest)
}
/**
@@ -248,10 +294,10 @@ class LatLngBounds
private fun unionNoParamCheck(northLat: Double, eastLon: Double, southLat: Double, westLon: Double): LatLngBounds {
return LatLngBounds(
if (latNorth < northLat) northLat else latNorth,
if (lonEast < eastLon) eastLon else lonEast,
if (latSouth > southLat) southLat else latSouth,
if (lonWest > westLon) westLon else lonWest
if (latitudeNorth < northLat) northLat else latitudeNorth,
if (longitudeEast < eastLon) eastLon else longitudeEast,
if (latitudeSouth > southLat) southLat else latitudeSouth,
if (longitudeWest > westLon) westLon else longitudeWest
)
}
@@ -262,7 +308,7 @@ class LatLngBounds
* @return LatLngBounds
*/
fun intersect(box: LatLngBounds): LatLngBounds? {
return intersectNoParamCheck(box.latNorth, box.lonEast, box.latSouth, box.lonWest)
return intersectNoParamCheck(box.latitudeNorth, box.longitudeEast, box.latitudeSouth, box.longitudeWest)
}
/**
@@ -291,11 +337,11 @@ class LatLngBounds
}
private fun intersectNoParamCheck(northLat: Double, eastLon: Double, southLat: Double, westLon: Double): LatLngBounds? {
val minLonWest = Math.max(lonWest, westLon)
val maxLonEast = Math.min(lonEast, eastLon)
val minLonWest = Math.max(longitudeWest, westLon)
val maxLonEast = Math.min(longitudeEast, eastLon)
if (maxLonEast >= minLonWest) {
val minLatSouth = Math.max(latSouth, southLat)
val maxLatNorth = Math.min(latNorth, northLat)
val minLatSouth = Math.max(latitudeSouth, southLat)
val maxLatNorth = Math.min(latitudeNorth, northLat)
if (maxLatNorth >= minLatSouth) {
return LatLngBounds(maxLatNorth, maxLonEast, minLatSouth, minLonWest)
}
@@ -309,7 +355,7 @@ class LatLngBounds
* @return the hash code
*/
override fun hashCode(): Int {
return (latNorth + 90 + (latSouth + 90) * 1000 + (lonEast + 180) * 1000000 + (lonWest + 180) * 1000000000).toInt()
return (latitudeNorth + 90 + (latitudeSouth + 90) * 1000 + (longitudeEast + 180) * 1000000 + (longitudeWest + 180) * 1000000000).toInt()
}
/**
@@ -328,10 +374,10 @@ class LatLngBounds
* @param flags Additional flags about how the object should be written
*/
override fun writeToParcel(out: Parcel, flags: Int) {
out.writeDouble(latNorth)
out.writeDouble(lonEast)
out.writeDouble(latSouth)
out.writeDouble(lonWest)
out.writeDouble(latitudeNorth)
out.writeDouble(longitudeEast)
out.writeDouble(latitudeSouth)
out.writeDouble(longitudeWest)
}
/**

View File

@@ -273,7 +273,12 @@ final class NativeMapView implements NativeMap {
}
@Override
public CameraPosition getCameraForLatLngBounds(LatLngBounds bounds, int[] padding, double bearing, double tilt) {
public CameraPosition getCameraForLatLngBounds(
@NonNull LatLngBounds bounds,
int[] padding,
double bearing,
double tilt
) {
if (checkState("getCameraForLatLngBounds")) {
return null;
}
@@ -289,7 +294,7 @@ final class NativeMapView implements NativeMap {
}
@Override
public CameraPosition getCameraForGeometry(Geometry geometry, int[] padding, double bearing, double tilt) {
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry, int[] padding, double bearing, double tilt) {
if (checkState("getCameraForGeometry")) {
return null;
}
@@ -1521,14 +1526,7 @@ final class NativeMapView implements NativeMap {
mapRenderer.setOnFpsChangedListener(new MapboxMap.OnFpsChangedListener() {
@Override
public void onFpsChanged(final double fps) {
handler.post(new Runnable() {
@Override
public void run() {
listener.onFpsChanged(fps);
}
});
handler.post(() -> listener.onFpsChanged(fps));
}
});
} else {

View File

@@ -141,10 +141,10 @@ public class OfflineTilePyramidRegionDefinition implements OfflineRegionDefiniti
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeString(styleURL);
dest.writeDouble(bounds.getLatNorth());
dest.writeDouble(bounds.getLonEast());
dest.writeDouble(bounds.getLatSouth());
dest.writeDouble(bounds.getLonWest());
dest.writeDouble(bounds.latitudeNorth);
dest.writeDouble(bounds.longitudeEast);
dest.writeDouble(bounds.latitudeSouth);
dest.writeDouble(bounds.longitudeWest);
dest.writeDouble(minZoom);
dest.writeDouble(maxZoom);
dest.writeFloat(pixelRatio);

View File

@@ -41,8 +41,8 @@ object Utils {
*/
fun getRandomLocation(bounds: LatLngBounds): Location {
val random = Random()
val randomLat = bounds.latSouth + (bounds.latNorth - bounds.latSouth) * random.nextDouble()
val randomLon = bounds.lonWest + (bounds.lonEast - bounds.lonWest) * random.nextDouble()
val randomLat = bounds.latitudeSouth + (bounds.latitudeNorth - bounds.latitudeSouth) * random.nextDouble()
val randomLon = bounds.longitudeWest + (bounds.longitudeEast - bounds.longitudeWest) * random.nextDouble()
val location = Location("random-loc")
location.longitude = randomLon
location.latitude = randomLat

View File

@@ -25,7 +25,6 @@ import com.mapbox.mapboxsdk.style.layers.PropertyFactory
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import com.mapbox.mapboxsdk.testapp.R
import com.mapbox.mapboxsdk.testapp.activity.style.AnimatedSymbolLayerActivity.Car
import com.mapbox.turf.TurfMeasurement
import java.util.*
@@ -296,8 +295,8 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
get() {
val bounds = mapboxMap!!.projection.visibleRegion.latLngBounds
val generator = Random()
val randomLat = bounds.latSouth + generator.nextDouble() * bounds.latNorth - bounds.latSouth
val randomLon = bounds.lonWest + generator.nextDouble() * bounds.lonEast - bounds.lonWest
val randomLat = bounds.latitudeSouth + generator.nextDouble() * bounds.latitudeNorth - bounds.latitudeSouth
val randomLon = bounds.longitudeWest + generator.nextDouble() * bounds.longitudeEast - bounds.longitudeWest
return LatLng(randomLat, randomLon)
}

View File

@@ -130,8 +130,8 @@ class CollectionUpdateOnStyleChange : AppCompatActivity(), OnMapReadyCallback, S
private fun getLatLngInBounds(bounds: LatLngBounds): LatLng {
val generator = Random()
val randomLat = bounds.latSouth + generator.nextDouble() * (bounds.latNorth - bounds.latSouth)
val randomLon = bounds.lonWest + generator.nextDouble() * (bounds.lonEast - bounds.lonWest)
val randomLat = bounds.latitudeSouth + generator.nextDouble() * (bounds.latitudeNorth - bounds.latitudeSouth)
val randomLon = bounds.longitudeWest + generator.nextDouble() * (bounds.longitudeEast - bounds.longitudeWest)
return LatLng(randomLat, randomLon)
}
}

View File

@@ -16,7 +16,6 @@ import com.mapbox.mapboxsdk.style.layers.*
import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource
import com.mapbox.mapboxsdk.style.sources.GeometryTileProvider
import com.mapbox.mapboxsdk.testapp.R
import com.mapbox.mapboxsdk.testapp.activity.style.GridSourceActivity.GridProvider
import java.util.*
/**
@@ -59,24 +58,24 @@ class GridSourceActivity : AppCompatActivity(), OnMapReadyCallback {
20.0
}
var gridLines: MutableList<Any?> = ArrayList<Any?>()
var y = Math.ceil(bounds.latNorth / gridSpacing) * gridSpacing
while (y >= Math.floor(bounds.latSouth / gridSpacing) * gridSpacing) {
var y = Math.ceil(bounds.latitudeNorth / gridSpacing) * gridSpacing
while (y >= Math.floor(bounds.latitudeSouth / gridSpacing) * gridSpacing) {
gridLines.add(
Arrays.asList(
Point.fromLngLat(bounds.lonWest, y),
Point.fromLngLat(bounds.lonEast, y)
Point.fromLngLat(bounds.longitudeWest, y),
Point.fromLngLat(bounds.longitudeEast, y)
)
)
y -= gridSpacing
}
features.add(Feature.fromGeometry(MultiLineString.fromLngLats(gridLines as MutableList<MutableList<Point>>)))
gridLines = ArrayList<Any?>()
var x = Math.floor(bounds.lonWest / gridSpacing) * gridSpacing
while (x <= Math.ceil(bounds.lonEast / gridSpacing) * gridSpacing) {
var x = Math.floor(bounds.longitudeWest / gridSpacing) * gridSpacing
while (x <= Math.ceil(bounds.longitudeEast / gridSpacing) * gridSpacing) {
gridLines.add(
Arrays.asList(
Point.fromLngLat(x, bounds.latSouth),
Point.fromLngLat(x, bounds.latNorth)
Point.fromLngLat(x, bounds.latitudeSouth),
Point.fromLngLat(x, bounds.latitudeNorth)
)
)
x += gridSpacing
@@ -110,32 +109,32 @@ class GridSourceActivity : AppCompatActivity(), OnMapReadyCallback {
override fun onStart() {
super.onStart()
mapView!!.onStart()
mapView.onStart()
}
public override fun onResume() {
super.onResume()
mapView!!.onResume()
mapView.onResume()
}
public override fun onPause() {
super.onPause()
mapView!!.onPause()
mapView.onPause()
}
override fun onStop() {
super.onStop()
mapView!!.onStop()
mapView.onStop()
}
override fun onDestroy() {
super.onDestroy()
mapView!!.onDestroy()
mapView.onDestroy()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView!!.onSaveInstanceState(outState)
mapView.onSaveInstanceState(outState)
}
companion object {