Permalink
Browse files

reduce number of center-changed callbacks

avoid map.getZoom() from Java to JavaScript
  • Loading branch information...
cpesch committed Jun 25, 2012
1 parent 847b3bc commit f18a70f2c71122cc8928c33e5cf929255c616710
@@ -717,6 +717,10 @@ public void setCenter(BaseNavigationPosition center) {
this.center = center;
}
+ private int getZoom() {
+ return preferences.getInt(CENTER_ZOOM_PREFERENCE, 1);
+ }
+
protected abstract BaseNavigationPosition getNorthEastBounds();
protected abstract BaseNavigationPosition getSouthWestBounds();
protected abstract BaseNavigationPosition getCurrentMapCenter();
@@ -772,7 +776,7 @@ private BitSet calculateSignificantPositionsForZoomLevel(List<BaseNavigationPosi
private List<BaseNavigationPosition> filterSignificantPositions(List<BaseNavigationPosition> positions) {
long start = currentTimeMillis();
- int zoomLevel = getCurrentZoomLevel();
+ int zoomLevel = getZoom();
BitSet pointStatus = calculateSignificantPositionsForZoomLevel(positions, zoomLevel);
List<BaseNavigationPosition> result = new ArrayList<BaseNavigationPosition>();
for (int i = 0; i < positions.size(); i++)
@@ -833,8 +837,6 @@ private BitSet calculateSignificantPositionsForZoomLevel(List<BaseNavigationPosi
return positions;
}
- protected abstract int getCurrentZoomLevel();
-
private List<BaseNavigationPosition> filterVisiblePositions(List<BaseNavigationPosition> positions,
double factor, boolean includeFirstAndLastPosition) {
long start = currentTimeMillis();
@@ -1306,8 +1308,8 @@ void processLines(List<String> lines) {
private static final Pattern SELECT_POSITION_PATTERN = Pattern.compile("^select-position/(.*)/(.*)/(.*)/(.*)$");
private static final Pattern SELECT_POSITIONS_PATTERN = Pattern.compile("^select-positions/(.*)/(.*)/(.*)/(.*)/(.*)");
private static final Pattern MAP_TYPE_CHANGED_PATTERN = Pattern.compile("^map-type-changed/(.*)$");
- private static final Pattern ZOOM_CHANGED_PATTERN = Pattern.compile("^zoom-changed$");
- private static final Pattern CENTER_CHANGED_PATTERN = Pattern.compile("^center-changed/(.*)/(.*)/(.*)$");
+ private static final Pattern ZOOM_CHANGED_PATTERN = Pattern.compile("^zoom-changed/(.*)$");
+ private static final Pattern CENTER_CHANGED_PATTERN = Pattern.compile("^center-changed/(.*)/(.*)$");
private static final Pattern CALLBACK_PORT_PATTERN = Pattern.compile("^callback-port/(\\d+)$");
private static final Pattern INSERT_WAYPOINTS_PATTERN = Pattern.compile("^(Insert-All-Waypoints|Insert-Only-Turnpoints): (-?\\d+)/(.*)$");
@@ -1411,6 +1413,8 @@ public void run() {
Matcher zoomChangedMatcher = ZOOM_CHANGED_PATTERN.matcher(callback);
if (zoomChangedMatcher.matches()) {
+ Integer zoom = parseInt(zoomChangedMatcher.group(1));
+ preferences.putInt(CENTER_ZOOM_PREFERENCE, zoom);
synchronized (notificationMutex) {
// since setCenter() leads to a callback and thus paints the track twice
if (ignoreNextZoomCallback)
@@ -1421,10 +1425,8 @@ public void run() {
if (recenterAfterZooming)
haveToRecenterMap = true;
haveToRepaintSelectionImmediately = true;
- int currentZoomLevel = getCurrentZoomLevel();
- preferences.putInt(CENTER_ZOOM_PREFERENCE, currentZoomLevel);
- selectionUpdateReason = "zoomed from " + lastZoomLevel + " to " + currentZoomLevel;
- lastZoomLevel = currentZoomLevel;
+ selectionUpdateReason = "zoomed from " + lastZoomLevel + " to " + zoom;
+ lastZoomLevel = zoom;
notificationMutex.notifyAll();
}
}
@@ -1435,7 +1437,6 @@ public void run() {
if (centerChangedMatcher.matches()) {
preferences.putDouble(CENTER_LATITUDE_PREFERENCE, parseDouble(centerChangedMatcher.group(1)));
preferences.putDouble(CENTER_LONGITUDE_PREFERENCE, parseDouble(centerChangedMatcher.group(2)));
- preferences.putInt(CENTER_ZOOM_PREFERENCE, parseInt(centerChangedMatcher.group(3)));
if (visibleNorthEast != null && visibleSouthWest != null && visibleNorthWest != null && visibleSouthEast != null) {
BaseNavigationPosition mapNorthEast = getNorthEastBounds();
BaseNavigationPosition mapSouthWest = getSouthWestBounds();
@@ -626,11 +626,18 @@
}
google.maps.event.addListener(map, "zoom_changed", function() {
- callJava("zoom-changed");
+ var zoom = map.getZoom();
+ callJava("zoom-changed/" + zoom);
});
+ var centerLat = 0.0, centerLng = 0.0;
google.maps.event.addListener(map, "center_changed", function() {
- center = map.getCenter();
- callJava("center-changed/" + center.lat() + "/" + center.lng() + "/" + map.getZoom());
+ var center = map.getCenter();
+ var currentLat = Math.round(center.lat() * 100000) / 100000;
+ var currentLng = Math.round(center.lng() * 100000) / 100000;
+ if(currentLat != centerLat && currentLng != centerLng) {
+ centerLat = currentLat; centerLng = currentLng;
+ callJava("center-changed/" + centerLat + "/" + centerLng);
+ }
});
var shiftKey = false, altKey = false, ctrlKey = false;
@@ -28,8 +28,8 @@
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowOpeningEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowWillOpenEvent;
import slash.common.io.Externalization;
-import slash.common.system.Platform;
import slash.common.io.TokenResolver;
+import slash.common.system.Platform;
import slash.navigation.base.BaseNavigationPosition;
import javax.swing.*;
@@ -43,7 +43,6 @@
import static chrriis.dj.nativeswing.swtimpl.components.JWebBrowser.useWebkitRuntime;
import static chrriis.dj.nativeswing.swtimpl.components.JWebBrowser.useXULRunnerRuntime;
import static slash.common.system.Platform.isLinux;
-import static slash.common.io.Transfer.parseDouble;
/**
* Implementation for a component that displays the positions of a position list on a map.
@@ -283,12 +282,7 @@ private void resizeMap() {
}
}
- // zoom level and bounds
-
- protected int getCurrentZoomLevel() {
- String zoomLevel = executeScriptWithResult("return map.getZoom();");
- return zoomLevel != null ? parseDouble(zoomLevel).intValue() : 1;
- }
+ // bounds and center
protected BaseNavigationPosition getNorthEastBounds() {
return extractLatLng("return getNorthEastBounds();");

0 comments on commit f18a70f

Please sign in to comment.