kauron/estraba
Archived
1
0
Fork 0

Comments on map process and better zoom, centering and display

This commit is contained in:
Carlos Galindo 2016-05-19 23:25:40 +02:00
parent 3c3c1fca33
commit a8c6e79e2e
Signed by: kauron
GPG key ID: 83E68706DEE119A3

View file

@ -239,55 +239,84 @@ public class DashboardController implements Initializable, MapComponentInitializ
@Override @Override
public void mapInitialized() { public void mapInitialized() {
System.err.println("mapInitialized begin with " + track.getNumPoints()); // When the JS init is done
final int N = 0, S = 1, E = 2, W = 3;
final double[] coord = new double[4]; final double[] coord = new double[4];
coord[0] = Double.MIN_VALUE; coord[0] = Double.MIN_VALUE;
coord[1] = Double.MAX_VALUE; coord[1] = Double.MAX_VALUE;
coord[2] = Double.MIN_VALUE; coord[2] = Double.MIN_VALUE;
coord[3] = Double.MAX_VALUE; coord[3] = Double.MAX_VALUE;
MapOptions mapOptions = new MapOptions(); // Create a new map with the appropriate options
mapOptions.center(new LatLong( GoogleMap map = mapView.createMap(new MapOptions()
track.getChunks().get(track.getNumPoints() / 2).getFirstPoint().getLatitude(), .center(new LatLong(0.0, 0.0))
track.getChunks().get(track.getNumPoints() / 2).getFirstPoint().getLongitude()))
.mapType(MapTypeIdEnum.TERRAIN) .mapType(MapTypeIdEnum.TERRAIN)
.overviewMapControl(true) .overviewMapControl(true)
.panControl(false) .panControl(false)
.rotateControl(false) .rotateControl(false)
.scaleControl(true) .scaleControl(true)
.streetViewControl(false) .streetViewControl(false)
.zoomControl(false) .zoomControl(true)
.zoom(10); .zoom(0)
);
GoogleMap map = mapView.createMap(mapOptions); // Prepare an array with LatLong objects
//Add a marker to the map
MVCArray pathArray = new MVCArray(); MVCArray pathArray = new MVCArray();
System.err.println("mapInitialized pathArray created"); pathArray.push(new LatLong( // first step of the route
track.getChunks().get(0).getFirstPoint().getLatitude(),
track.getChunks().get(0).getFirstPoint().getLongitude()
));
track.getChunks().forEach(chunk -> { track.getChunks().forEach(chunk -> {
double lat = chunk.getFirstPoint().getLatitude(); double lat = chunk.getLastPoint().getLatitude();
double lon = chunk.getFirstPoint().getLongitude(); double lon = chunk.getLastPoint().getLongitude();
coord[0] = Math.max(lat, coord[0]); coord[N] = Math.max(lat, coord[N]);
coord[1] = Math.min(lat, coord[1]); coord[S] = Math.min(lat, coord[S]);
coord[2] = Math.max(lon, coord[2]); coord[E] = Math.max(lon, coord[E]);
coord[3] = Math.min(lon, coord[3]); coord[W] = Math.min(lon, coord[W]);
pathArray.push(new LatLong(lat, lon)); pathArray.push(new LatLong(lat, lon));
}); });
pathArray.push(new LatLong(track.getChunks().get(track.getNumPoints() - 1).getLastPoint().getLatitude(), // Create and add the polyline using the array
track.getChunks().get(track.getNumPoints() - 1).getLastPoint().getLongitude())); // This polyline displays instantly with no problem
System.err.println("mapInitialized chunks added"); // TODO: add color with PolylineOptions.strokeColor("#ffff00") to match the color schemes of the app
map.addMapShape(new Polyline( // When using that method, the line does not load properly, it needs an update to the zoom to show up.
new PolylineOptions() map.addMapShape(new Polyline(new PolylineOptions().path(pathArray)));
.path(pathArray) // Adjust the map to the correct center and zoom
.strokeColor("red") map.fitBounds(new LatLongBounds(
.strokeWeight(2) new LatLong(coord[S], coord[W]),
.visible(true)) new LatLong(coord[N], coord[E])
); ));
System.err.println("mapInitialized end"); map.setZoom(getBoundsZoomLevel(coord, mapView.getHeight(), mapView.getWidth()));
System.err.printf("Average coords: %.2fN, %.2S, %.2fE, %.2fW", coord[0], coord[1], coord[2], coord[3]); // Print some debug info
mapView.setCenter((coord[0] + coord[1]) / 2, (coord[2] + coord[3]) / 2); System.err.printf("Bound to coords: %.2fN, %.2S, %.2fE, %.2fW\n", coord[N], coord[S], coord[E], coord[W]);
// map.fitBounds(new LatLongBounds(new LatLong(coord[1], coord[3]), new LatLong(coord[0], coord[2]))); System.err.printf("Selected zoom: %d\n", map.getZoom());
}
/**
* Method to compute properly the amount of zoom in a GMap
* Many thanks to <a href=http://stackoverflow.com/a/13274361>http://stackoverflow.com/a/13274361</a>
* @param bound Array with coordinates bounds [N, S, E, W]
* @return Zoom from 0 to 21
*/
private int getBoundsZoomLevel(double[] bound, double mapHeight, double mapWidth) {
double latFraction = (latRad(bound[0]) - latRad(bound[1])) / Math.PI;
double lngDiff = bound[2] - bound[3];
double lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;
double latZoom = zoom(mapHeight, 256, latFraction);
double lngZoom = zoom(mapWidth, 256, lngFraction);
return (int) Math.min(Math.min(latZoom, lngZoom), 21);
}
private double zoom(double mapPx, int worldPx, double fraction) {
return Math.floor(Math.log(mapPx / worldPx / fraction) / Math.log(2));
}
private double latRad(double lat) {
double sin = Math.sin(lat * Math.PI / 180);
double radX2 = Math.log((1 + sin) / (1 - sin)) / 2;
return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;
} }
} }