-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
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); | ||
|
There was a problem hiding this comment.
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.