Commit 658c5414 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

google maps api bug fix

fit bounds didn't work when wanted to fit bounds when the expected bounds were at the level that was below min level - custom method implemented
parent 68881148
......@@ -5,12 +5,14 @@ var Promise = require("bluebird");
var logger = require('../logger');
var functions = require('../Functions');
var Alias = require('./data/Alias');
var AliasInfoWindow = require('./window/AliasInfoWindow');
var AliasSurface = require('./surface/AliasSurface');
var GuiConnector = require('../GuiConnector');
var IdentifiedElement = require('./data/IdentifiedElement');
var ObjectWithListeners = require('../ObjectWithListeners');
var PointInfoWindow = require('./window/PointInfoWindow');
var Reaction = require('./data/Reaction');
var ReactionInfoWindow = require('./window/ReactionInfoWindow');
var ReactionSurface = require('./surface/ReactionSurface');
......@@ -1088,7 +1090,7 @@ AbstractCustomMap.prototype.getBounds = function () {
p2: this.fromLatLngToPoint(sw)
};
if (result.p2.x > result.p1.x) {
result.p2.x -= 360 * self.pixelsPerLonDegree_*self.zoomFactor;
result.p2.x -= 360 * self.pixelsPerLonDegree_ * self.zoomFactor;
}
return result;
};
......@@ -1097,14 +1099,13 @@ AbstractCustomMap.prototype.getBounds = function () {
/**
* Sets zoom level for google maps.
*
* @param mapIdentifier
* id of the model for which we change zoom level
* @param zoom
* new zoom level on map
*/
AbstractCustomMap.prototype.setZoom = function (zoom) {
if (this.initialized) {
return Promise.resolve(this.getGoogleMap().setZoom(zoom));
var self = this;
if (self.initialized) {
return Promise.resolve(self.getGoogleMap().setZoom(zoom));
} else {
logger.warn("cannot change zoom for map that is not opened yet");
return Promise.resolve();
......@@ -1118,21 +1119,66 @@ AbstractCustomMap.prototype.getZoom = function () {
AbstractCustomMap.prototype.fitBounds = function (markers) {
var self = this;
var map = self.getGoogleMap();
if (map !== undefined) {
var bounds = new google.maps.LatLngBounds();
if (map !== undefined && markers.length > 0) {
var minX = self.getModel().getWidth();
var minY = self.getModel().getHeight();
var maxX = 0;
var maxY = 0;
for (var i = 0; i < markers.length; i++) {
var marker = markers[i];
if (marker.getModelId() === self.getId()) {
var markerBounds = marker.getBounds();
bounds.extend(markerBounds.getNorthEast());
bounds.extend(markerBounds.getSouthWest());
if (marker instanceof Alias) {
minX = Math.min(minX, marker.getX());
minY = Math.min(minY, marker.getY());
maxX = Math.max(maxX, marker.getX() + marker.getWidth());
maxY = Math.max(maxY, marker.getY() + marker.getHeight());
} else if (marker instanceof Reaction) {
minX = Math.min(minX, marker.getCenter().x);
minY = Math.min(minY, marker.getCenter().y);
maxX = Math.max(maxX, marker.getCenter().x);
maxY = Math.max(maxY, marker.getCenter().y);
} else {
var markerBounds = marker.getBounds();
var p1 = self.fromLatLngToPoint(markerBounds.getNorthEast());
var p2 = self.fromLatLngToPoint(markerBounds.getSouthWest());
minX = Math.min(minX, p2.x);
minY = Math.min(minY, p1.y);
maxX = Math.max(maxX, p1.x);
maxY = Math.max(maxY, p2.y);
}
}
}
if (!bounds.isEmpty()) {
return map.fitBounds(bounds);
var currentBounds = self.getBounds();
var xScale = (maxX - minX) / (currentBounds.p1.x - currentBounds.p2.x);
var yScale = (maxY - minY) / (currentBounds.p2.y - currentBounds.p1.y);
var scale = Math.max(xScale, yScale);
var zoom = self.getZoom();
while (scale > 1) {
zoom--;
scale /= 2;
}
if (scale <= 1e-6) {
zoom = self.getModel().getMaxZoom();
} else {
while (scale < 0.5) {
zoom++;
scale *= 2;
}
}
if (zoom > self.getModel().getMaxZoom()) {
zoom = self.getModel().getMaxZoom();
}
var center = new google.maps.Point((minX + maxX) / 2, (minY + maxY) / 2);
var centerLatLng = self.fromPointToLatLng(center);
return Promise.all([map.setCenter(centerLatLng), map.setZoom(zoom)]);
}
return Promise.resolve();
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment