Skip to content

Commit

Permalink
Resolve clusters shaking problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Артём Черныш authored and Артём Черныш committed Mar 15, 2024
1 parent b743646 commit e26cdc0
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions Sources/ClusterMap/Internal/Extensions/MKMapRect+Additions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,44 @@ extension MKMapRect {
}

init(region: MKCoordinateRegion) {
let center = region.center
let span = region.span
let topLeft = CLLocationCoordinate2D(
latitude: center.latitude + span.latitudeDelta * 0.5,
longitude: center.longitude - span.longitudeDelta * 0.5
var topLeft = CLLocationCoordinate2D(
latitude: min(region.center.latitude + (region.span.latitudeDelta / 2), 90),
longitude: region.center.longitude - (region.span.longitudeDelta / 2)
)
let bottomRight = CLLocationCoordinate2D(
latitude: center.latitude - span.latitudeDelta * 0.5,
longitude: center.longitude + span.longitudeDelta * 0.5
var bottomRight = CLLocationCoordinate2D(
latitude: max(region.center.latitude - (region.span.latitudeDelta / 2), -90),
longitude: region.center.longitude + (region.span.longitudeDelta / 2)
)
let topLeftPoint = MKMapPoint(topLeft)
let bottomRightPoint = MKMapPoint(bottomRight)

if topLeft.longitude < -180 || bottomRight.longitude > 180 {
let world = MKMapRect.world

if topLeft.longitude < -180 {
topLeft.longitude += 360
}
if bottomRight.longitude > 180 {
bottomRight.longitude -= 360
}
let topLeftPoint = MKMapPoint(topLeft)
let bottomRightPoint = MKMapPoint(bottomRight)

self.init(
x: min(topLeftPoint.x, bottomRightPoint.x),
y: min(topLeftPoint.y, bottomRightPoint.y),
width: abs(topLeftPoint.x - bottomRightPoint.x),
height: abs(topLeftPoint.y - bottomRightPoint.y)
)
self.init(
x: max(topLeftPoint.x, bottomRightPoint.x),
y: world.origin.y,
width: (world.maxX - max(topLeftPoint.x, bottomRightPoint.x)) + min(topLeftPoint.x, bottomRightPoint.x),
height: world.height
)

} else {
let topLeftPoint = MKMapPoint(topLeft)
let bottomRightPoint = MKMapPoint(bottomRight)

self.init(
x: min(topLeftPoint.x, bottomRightPoint.x),
y: min(topLeftPoint.y, bottomRightPoint.y),
width: max(topLeftPoint.x, bottomRightPoint.x) - min(topLeftPoint.x, bottomRightPoint.x),
height: max(topLeftPoint.y, bottomRightPoint.y) - min(topLeftPoint.y, bottomRightPoint.y)
)
}
}
}

0 comments on commit e26cdc0

Please sign in to comment.