Kortið tól
Map tools for GoogleMaps on Android.
Dependency
Add jitpack.io to your root build.gradle, eg:
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
}
then add the dependency to your project build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.github.fiskurgit:KortidTol:1.0.6'
}
You can find the latest version in the releases tab above: https://github.com/fiskurgit/KortidTol/releases
More options at jitpack.io: https://jitpack.io/#fiskurgit/KortidTol
Licence
Full licence here: https://github.com/fiskurgit/KortidTol/blob/master/LICENSE
In short:
The MIT License is a permissive license that is short and to the point. It lets people do anything they want with your code as long as they provide attribution back to you and don’t hold you liable.
Limit Bounds
** Note - LimitBounds is deprecated, use the native setLatLngBoundsForCameraTarget instead **
Create Bounds
Either provide four outlier coordinates or an array of LatLng pairs and return a bounding box LatLngBounds object:
LatLngBounds threePeaksBounds = MapTools.createBounds(MIN_LAT, MAX_LAT, MIN_LON, MAX_LON);
//or LatLng[] multipleCoords = ... LatLngBounds threePeaksBounds = MapTools.createBounds(multipleCoords);
Nearest Index
Find the nearest index in an array of coordinates to a point, for example when a user long-clicks on a map near a route, find the nearest point on the route:
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override public void onMapLongClick(LatLng latLng) {
int nearestIndex = MapTools.nearestIndex(routeCoords, latLng);
LatLng nearestRoutePoint = routeCoords.get(nearestIndex);
}
}
);
Long Click Helper and Draw Subsection
Manage long-click events with a map to help draw a route subsection.
LongClickHelper longClickHelper = new LongClickHelper();
... map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override public void onMapLongClick(LatLng latLng) {
vibrator.vibrate(175);
longClickHelper.addPoint(latLng);
if(longClickHelper.isReady()){
LatLng startClick = longClickHelper.getStart();
LatLng endClick = longClickHelper.getEnd();
MapTools.drawSubsection(map, routeCoords, startClick, endClick, Color.parseColor("#ff00cc"));
}
}
}
);
Subsection Distance
Calculate the distance of a subsection of a route
float meters = MapTools.subsectionDistance(routeCoords, startCoord, endCoord);
Quick (Convex) Hull
There's a Quick Hull implementation included (adapted from code by Jared Rummler) which can be used for displaying the bounding shape ( Concave Hull) of GeoJson polygons. Rather than expensive rendering of a shape consisting of thousands of points draw the bounding shape instead ( more detail here). The new single-pass method calculates the hull of an array of 580 coordinates in 4ms, versus 66ms for the older method:
New method
List<LatLng> polygonPoints = MapTools.getPoints(jsonString);
ArrayList<LatLng> hullLatLng = new QuickHullLatLng().quickHull(polygonPoints);
Polygon hullPolygon = map.addPolygon(new PolygonOptions()
.addAll(hullLatLng)
.strokeColor(Color.parseColor("#22000000"))
.strokeWidth(0)
.fillColor(Color.parseColor("#22000000")));
}
Old method (may be deprecated in future)
List<LatLng> polygonPoints = MapTools.getPoints(jsonString);
QuickHull quickHull = new QuickHull();
ArrayList<Point> points = quickHull.convertToPoint(polygonPoints, map.getProjection());
ArrayList<Point> hull = quickHull.quickHull(points);
ArrayList<LatLng> hullLatLng = quickHull.convertToLatLng(hull, map.getProjection());
Polygon hullPolygon = map.addPolygon(new PolygonOptions()
.addAll(hullLatLng)
.strokeColor(Color.parseColor("#22000000"))
.strokeWidth(0)
.fillColor(Color.parseColor("#22000000")));
}
Concave Hull
There's also a Concave Hull method which uses code by Eric Grosso. This is much more complex than the Quick Hull implementation, a native 'single-pass' conversion is beyond my capabilities (or at least available time). The method converts the LatLng array to points and back again so it will be much slower than the new native Quick Hull method above. A threshold of 30 reduces a ~5000 point polygon to ~800 points.
List<LatLng> polygonPoints = MapTools.getPoints(jsonString);
double threshold = 30; List<LatLng> concaveLatLng = new ConcaveHull(polygonPoints, threshold, map.getProjection()).getHull();
Polygon concavePolygon = map.addPolygon(new PolygonOptions()
.addAll(concaveLatLng)
.strokeColor(Color.parseColor("#66000000"))
.strokeWidth(0)
.fillColor(Color.parseColor("#66000000")));