Skip to content

Instantly share code, notes, and snippets.

@Fy50167
Created June 8, 2024 14:53
Show Gist options
  • Save Fy50167/02591f047e3a4cedf67af78597c953b3 to your computer and use it in GitHub Desktop.
Save Fy50167/02591f047e3a4cedf67af78597c953b3 to your computer and use it in GitHub Desktop.
Cypress test with mapbox
export const findStreetCoordinates = () => {
cy.window().then((win) => {
const map = win.mapRef
if (!map) return []
// Get features for road names
const features = map.queryRenderedFeatures(undefined, {
layers: ['road-label-small', 'road-label-medium', 'road-label-large']
})
// Obtain coordinate information from visible roads
const streetCoordinates: ScreenCoordinates[] = features.map(
(feature: any) => {
const geometry = feature.geometry
if (geometry.coordinates[0].length === 2) {
const coordinates = map.project(geometry.coordinates[0])
return coordinates
}
}
)
const filteredCoordinates: ScreenCoordinates[] = streetCoordinates.filter(
(coordinates: ScreenCoordinates) => {
// Ensuring that our clicks are constrained within the screen
return (
coordinates &&
coordinates.x >= 0 &&
coordinates.x < 1880 &&
coordinates.y >= 0 &&
coordinates.y < 1040
)
}
)
return filteredCoordinates
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment