Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offset tooltip popup so it fits inside the map bounds #9819

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/ui/public/agg_response/geo_json/_tooltip_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ export default function TileMapTooltipFormatter($compile, $rootScope, Private) {
value: metricAgg.fieldFormatter()(value)
},
{
label: 'Center',
value: geoFormat.convert({
lat: feature.geometry.coordinates[1],
lon: feature.geometry.coordinates[0]
})
label: 'Latitude',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is an improvement. Crisper and more legible.

value: feature.geometry.coordinates[1],
},
{
label: 'Longitude',
value: feature.geometry.coordinates[0],
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export default function MarkerFactory() {
if (!this.map) return;
const lat = _.get(feature, 'geometry.coordinates.1');
const lng = _.get(feature, 'geometry.coordinates.0');

latLng = latLng || L.latLng(lat, lng);

const content = this._tooltipFormatter(feature);
Expand All @@ -229,8 +230,51 @@ export default function MarkerFactory() {
this._createTooltip(content, latLng);
}

_getOffset(content, latLng) {
// Default maxWidth leaflet applies to popup is 300
const popupWidth = 300;

// We need to create the popup first to determine how tall it will be. Give
// it an out of map bounds offset as we don't want to see this one. It will
// get replaced on the next popup creation where we apply the correct offset
Copy link
Contributor

@thomasneirynck thomasneirynck Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clever approach!

const popup = L.popup({ autoPan: false, offset: new L.Point(this.map.getSize().x * 2, this.map.getSize().y * 2) })
.setLatLng(latLng)
.setContent(content)
.openOn(this.map);
const popupHeight = popup._contentNode.clientHeight;
Copy link
Contributor

@thomasneirynck thomasneirynck Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't introduce dependencies on internal implementation details of 3rd party libraries.


const east = this.map.getBounds().getEast();
const west = this.map.getBounds().getWest();
const north = this.map.getBounds().getNorth();
const south = this.map.getBounds().getSouth();
const width = Math.abs(east - west);
const height = Math.abs(north - south);
const xscale = this.map.getSize().x / width;
const yscale = this.map.getSize().y / height;

const popupDistanceToLeftEdge = Math.abs(latLng.lng - west) * xscale;
const popupDistanceToRightEdge = Math.abs(latLng.lng - east) * xscale;
let widthOffset = 0;
if (popupDistanceToLeftEdge < popupWidth / 2) {
widthOffset = popupWidth / 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the pop-up to attach to the point, we'd probably want:
widthOffset = popupWidth / 4 + 16;

image

Similar for right side:
widthOffset = -popupWidth / 4 - 16;

}
else if (popupDistanceToRightEdge < popupWidth / 2) {
widthOffset = -popupWidth / 2;
}

// Default height offset leaflet applies to popup is 6
let heightOffset = 6;
const popupDistanceToTopEdge = Math.abs(latLng.lat - north) * yscale;
if (popupDistanceToTopEdge < popupHeight + heightOffset) {
// 16 is the margin-bottom style that was added to the leaflet-popup in _tilemap.less
heightOffset += popupHeight + 16;
}

return new L.Point(widthOffset, heightOffset);
}

_createTooltip(content, latLng) {
L.popup({ autoPan: false })
L.popup({ autoPan: false, offset: this._getOffset(content, latLng) })
.setLatLng(latLng)
.setContent(content)
.openOn(this.map);
Expand Down