Skip to content

Colota - Android GPS Location tracker

refactor: source map attribution from style.json

Max Dietricha92735f6af96
diff --git a/apps/mobile/src/components/features/map/ColotaMapView.tsx b/apps/mobile/src/components/features/map/ColotaMapView.tsx
index 06ce933f..f7821030 100644
--- a/apps/mobile/src/components/features/map/ColotaMapView.tsx
+++ b/apps/mobile/src/components/features/map/ColotaMapView.tsx
@@ -4,23 +4,49 @@
*/
import React, { useRef, useImperativeHandle, forwardRef, useState, useEffect, useCallback } from "react"
-import { StyleProp, ViewStyle, View, Text, StyleSheet, Linking, Pressable } from "react-native"
+import { StyleProp, ViewStyle, View, Text, StyleSheet, Linking, Pressable, Modal } from "react-native"
import { Map, Camera } from "@maplibre/maplibre-react-native"
import type { MapRef, CameraRef, ViewStateChangeEvent, LngLatBounds } from "@maplibre/maplibre-react-native"
import type { NativeSyntheticEvent } from "react-native"
-import { Compass } from "lucide-react-native"
+import { Compass, Info, X } from "lucide-react-native"
import { useIsFocused } from "@react-navigation/native"
import { useTheme } from "../../../hooks/useTheme"
import { DEFAULT_MAP_ZOOM, MAP_STYLE_URL_LIGHT, MAP_STYLE_URL_DARK } from "../../../constants"
import { fonts } from "../../../styles/typography"
import NativeLocationService from "../../../services/NativeLocationService"
-const DEFAULT_ATTRIBUTION_LINKS = [
- { url: "https://openmaptiles.org/", label: "© OpenMapTiles" },
+interface AttributionLink {
+ url: string
+ label: string
+}
+
+// Keep in sync with the attribution in maps.mxd.codes/styles/*/style.json.
+const FALLBACK_ATTRIBUTION_LINKS: AttributionLink[] = [
{ url: "https://www.openstreetmap.org/copyright", label: "© OpenStreetMap contributors" },
- { url: "https://maps.mxd.codes/", label: "© maps.mxd.codes" }
+ { url: "https://maps.mxd.codes", label: "© maps.mxd.codes" },
+ { url: "https://openmaptiles.org", label: "© OpenMapTiles" },
+ { url: "https://github.com/tilezen/joerd/blob/master/docs/attribution.md", label: "Mapzen Terrain Tiles" }
]
+function parseStyleAttribution(sources: unknown): AttributionLink[] {
+ if (!sources || typeof sources !== "object") return []
+ const seen = new Set<string>()
+ const links: AttributionLink[] = []
+ const anchorRe = /<a\s+[^>]*href=["']([^"']+)["'][^>]*>([^<]+)<\/a>/gi
+ for (const src of Object.values(sources as Record<string, unknown>)) {
+ const html = (src as { attribution?: unknown })?.attribution
+ if (typeof html !== "string") continue
+ for (const match of html.matchAll(anchorRe)) {
+ const url = match[1]
+ const label = match[2].trim()
+ if (!label || seen.has(url)) continue
+ seen.add(url)
+ links.push({ url, label })
+ }
+ }
+ return links
+}
+
export interface ColotaMapRef {
camera: CameraRef | null
mapView: MapRef | null
@@ -54,6 +80,8 @@ export const ColotaMapView = forwardRef<ColotaMapRef, Props>(function ColotaMapV
const [mapStyleLight, setMapStyleLight] = useState(MAP_STYLE_URL_LIGHT)
const [mapStyleDark, setMapStyleDark] = useState(MAP_STYLE_URL_DARK)
const [heading, setHeading] = useState(0)
+ const [attributionLinks, setAttributionLinks] = useState<AttributionLink[]>(FALLBACK_ATTRIBUTION_LINKS)
+ const [attributionOpen, setAttributionOpen] = useState(false)
const isFocused = useIsFocused()
@@ -84,7 +112,25 @@ export const ColotaMapView = forwardRef<ColotaMapRef, Props>(function ColotaMapV
)
const mapStyle = isDark ? mapStyleDark : mapStyleLight
- const isCustomStyle = mapStyleLight !== MAP_STYLE_URL_LIGHT || mapStyleDark !== MAP_STYLE_URL_DARK
+
+ useEffect(() => {
+ if (!/^https?:/i.test(mapStyle)) {
+ setAttributionLinks(FALLBACK_ATTRIBUTION_LINKS)
+ return
+ }
+ const controller = new AbortController()
+ fetch(mapStyle, { signal: controller.signal })
+ .then((r) => r.json())
+ .then((style: { sources?: unknown }) => {
+ const parsed = parseStyleAttribution(style?.sources)
+ setAttributionLinks(parsed.length > 0 ? parsed : FALLBACK_ATTRIBUTION_LINKS)
+ })
+ .catch((err) => {
+ if (err?.name === "AbortError") return
+ setAttributionLinks(FALLBACK_ATTRIBUTION_LINKS)
+ })
+ return () => controller.abort()
+ }, [mapStyle])
const handleRegionDidChange = useCallback(
(event: NativeSyntheticEvent<ViewStateChangeEvent>) => {
@@ -125,7 +171,7 @@ export const ColotaMapView = forwardRef<ColotaMapRef, Props>(function ColotaMapV
ref={mapViewRef}
style={styles.map}
mapStyle={mapStyle}
- attribution={isCustomStyle}
+ attribution={false}
logo={false}
compass={false}
onDidFinishLoadingMap={onMapReady}
@@ -154,26 +200,63 @@ export const ColotaMapView = forwardRef<ColotaMapRef, Props>(function ColotaMapV
onPress={handleCompassPress}
>
<View style={{ transform: [{ rotate: `${-heading}deg` }] }}>
- <Compass size={24} color={colors.text} />
+ <Compass size={20} color={colors.textLight} />
</View>
</Pressable>
)}
- {/* Attribution - custom overlay for default server, MapLibre built-in for custom styles */}
- {!isCustomStyle && (
- <View style={[styles.attribution, { backgroundColor: colors.card + "CC" }]}>
- {DEFAULT_ATTRIBUTION_LINKS.map((link, i) => (
- <React.Fragment key={link.url}>
- {i > 0 && <Text style={[styles.attributionText, { color: colors.textLight }, fonts.regular]}> · </Text>}
+ {attributionLinks.length > 0 && (
+ <>
+ <Pressable
+ onPress={() => setAttributionOpen(true)}
+ hitSlop={8}
+ style={({ pressed }) => [
+ styles.attributionButton,
+ { backgroundColor: colors.card, borderColor: colors.border },
+ pressed && { opacity: colors.pressedOpacity }
+ ]}
+ accessibilityRole="button"
+ accessibilityLabel="Show map attribution"
+ >
+ <Info size={20} color={colors.textLight} />
+ </Pressable>
+
+ <Modal
+ transparent
+ statusBarTranslucent
+ visible={attributionOpen}
+ animationType="fade"
+ onRequestClose={() => setAttributionOpen(false)}
+ >
+ <Pressable style={styles.attributionBackdrop} onPress={() => setAttributionOpen(false)}>
<Pressable
- onPress={() => Linking.openURL(link.url)}
- style={({ pressed }) => pressed && { opacity: colors.pressedOpacity }}
+ onPress={() => {}}
+ style={[styles.attributionPopup, { backgroundColor: colors.card, borderColor: colors.border }]}
>
- <Text style={[styles.attributionText, { color: colors.link }, fonts.regular]}>{link.label}</Text>
+ <Pressable
+ onPress={() => setAttributionOpen(false)}
+ hitSlop={8}
+ accessibilityRole="button"
+ accessibilityLabel="Close"
+ style={({ pressed }) => [styles.attributionClose, pressed && { opacity: colors.pressedOpacity }]}
+ >
+ <X size={20} color={colors.textLight} />
+ </Pressable>
+ {attributionLinks.map((link) => (
+ <Pressable
+ key={link.url}
+ onPress={() => Linking.openURL(link.url)}
+ style={({ pressed }) => pressed && { opacity: colors.pressedOpacity }}
+ >
+ <Text style={[styles.attributionPopupText, { color: colors.link }, fonts.regular]}>
+ {link.label}
+ </Text>
+ </Pressable>
+ ))}
</Pressable>
- </React.Fragment>
- ))}
- </View>
+ </Pressable>
+ </Modal>
+ </>
)}
</View>
)
@@ -184,7 +267,7 @@ const styles = StyleSheet.create({
map: { flex: 1 },
compassButton: {
position: "absolute",
- bottom: 78,
+ bottom: 126,
right: 16,
width: 40,
height: 40,
@@ -199,17 +282,52 @@ const styles = StyleSheet.create({
borderWidth: 1,
zIndex: 10
},
- attribution: {
+ attributionButton: {
position: "absolute",
- bottom: 4,
- right: 4,
- flexDirection: "row",
+ bottom: 30,
+ right: 16,
+ width: 40,
+ height: 40,
+ borderRadius: 20,
+ alignItems: "center",
+ justifyContent: "center",
+ borderWidth: 1,
+ elevation: 5,
+ shadowColor: "#000",
+ shadowOffset: { width: 0, height: 2 },
+ shadowOpacity: 0.25,
+ shadowRadius: 3.84,
+ zIndex: 10
+ },
+ attributionBackdrop: {
+ flex: 1,
+ backgroundColor: "rgba(0, 0, 0, 0.4)",
alignItems: "center",
- paddingHorizontal: 6,
- paddingVertical: 2,
- borderRadius: 4
+ justifyContent: "center",
+ paddingHorizontal: 32
+ },
+ attributionPopup: {
+ maxWidth: 320,
+ width: "100%",
+ paddingLeft: 16,
+ paddingRight: 36,
+ paddingVertical: 14,
+ borderRadius: 10,
+ borderWidth: 1,
+ gap: 8,
+ elevation: 8,
+ shadowColor: "#000",
+ shadowOffset: { width: 0, height: 2 },
+ shadowOpacity: 0.25,
+ shadowRadius: 6
},
- attributionText: {
- fontSize: 10
+ attributionPopupText: {
+ fontSize: 13
+ },
+ attributionClose: {
+ position: "absolute",
+ top: 6,
+ right: 6,
+ padding: 4
}
})
diff --git a/apps/mobile/src/components/features/map/MapCenterButton.tsx b/apps/mobile/src/components/features/map/MapCenterButton.tsx
index 11fd4e09..5e97d041 100644
--- a/apps/mobile/src/components/features/map/MapCenterButton.tsx
+++ b/apps/mobile/src/components/features/map/MapCenterButton.tsx
@@ -4,7 +4,7 @@
*/
import React from "react"
-import { ViewStyle, StyleProp } from "react-native"
+import { ViewStyle, StyleProp, StyleSheet } from "react-native"
import { LocateFixed } from "lucide-react-native"
import { useTheme } from "../../../hooks/useTheme"
import { MapActionButton, mapActionStyles } from "./MapActionButton"
@@ -21,8 +21,12 @@ export const MapCenterButton: React.FC<Props> = ({ onPress, visible, style }) =>
if (!visible) return null
return (
- <MapActionButton onPress={onPress} style={[mapActionStyles.right, style]}>
- <LocateFixed size={24} color={colors.text} />
+ <MapActionButton onPress={onPress} style={[mapActionStyles.right, styles.position, style]}>
+ <LocateFixed size={20} color={colors.textLight} />
</MapActionButton>
)
}
+
+const styles = StyleSheet.create({
+ position: { bottom: 78 }
+})
diff --git a/apps/mobile/src/components/features/map/TrackToggleButton.tsx b/apps/mobile/src/components/features/map/TrackToggleButton.tsx
index a7a1f8f6..91a6d2b1 100644
--- a/apps/mobile/src/components/features/map/TrackToggleButton.tsx
+++ b/apps/mobile/src/components/features/map/TrackToggleButton.tsx
@@ -18,7 +18,7 @@ export function TrackToggleButton({ onPress, active }: Props) {
return (
<MapActionButton onPress={onPress} style={mapActionStyles.left}>
- <Route size={24} color={active ? colors.primary : colors.text} />
+ <Route size={20} color={active ? colors.primary : colors.textLight} />
</MapActionButton>
)
}