Created
June 8, 2024 14:53
-
-
Save Fy50167/02591f047e3a4cedf67af78597c953b3 to your computer and use it in GitHub Desktop.
Cypress test with mapbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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