-
-
Notifications
You must be signed in to change notification settings - Fork 6
Implement Mapbox route rendering via GeoJsonLayer #562
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
base: main
Are you sure you want to change the base?
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 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ interface McpResponseData { | |||||||||||||||||||||||||||
| address?: string; | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| mapUrl?: string; | ||||||||||||||||||||||||||||
| routeGeoJSON?: any; | ||||||||||||||||||||||||||||
|
Contributor
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. 🧹 Nitpick | 🔵 Trivial Consider using a more specific type for Using ♻️ Suggested type improvement+import type { Geometry } from 'geojson';
+
interface McpResponseData {
location: {
latitude?: number;
longitude?: number;
place_name?: string;
address?: string;
};
mapUrl?: string;
- routeGeoJSON?: any;
+ routeGeoJSON?: Geometry;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| interface GeospatialToolOutput { | ||||||||||||||||||||||||||||
|
|
@@ -42,8 +43,8 @@ export const MapQueryHandler: React.FC<MapQueryHandlerProps> = ({ toolOutput }) | |||||||||||||||||||||||||||
| // Optionally store more info from mcp_response if needed by MapboxMap component later | ||||||||||||||||||||||||||||
| mapFeature: { | ||||||||||||||||||||||||||||
| place_name, | ||||||||||||||||||||||||||||
| // Potentially add mapUrl or other details from toolOutput.mcp_response | ||||||||||||||||||||||||||||
| mapUrl: toolOutput.mcp_response?.mapUrl | ||||||||||||||||||||||||||||
| mapUrl: toolOutput.mcp_response?.mapUrl, | ||||||||||||||||||||||||||||
| routeGeoJSON: toolOutput.mcp_response?.routeGeoJSON | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,8 @@ import 'mapbox-gl/dist/mapbox-gl.css' | |
| import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css' | ||
| import { useMapToggle, MapToggleEnum } from '../map-toggle-context' | ||
| import { useMapData } from './map-data-context'; // Add this import | ||
| import { useMapLoading } from '../map-loading-context'; // Import useMapLoading | ||
| import { useMapLoading } from '../map-loading-context' | ||
| import { GeoJsonLayer } from './geojson-layer'; // Import useMapLoading | ||
|
Comment on lines
+14
to
+15
Contributor
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. Fix the misleading inline comment. The comment says "Import useMapLoading" but the line actually imports 📝 Proposed fix import { useMapLoading } from '../map-loading-context'
-import { GeoJsonLayer } from './geojson-layer'; // Import useMapLoading
+import { GeoJsonLayer } from './geojson-layer';
import { useMap } from './map-context'🤖 Prompt for AI Agents |
||
| import { useMap } from './map-context' | ||
|
|
||
| mapboxgl.accessToken = process.env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN as string; | ||
|
|
@@ -550,11 +551,8 @@ export const Mapbox: React.FC<{ position?: { latitude: number; longitude: number | |
| updateMapPosition(lat, lng); | ||
| } | ||
| } | ||
| // TODO: Handle mapData.mapFeature for drawing routes, polygons, etc. in a future step. | ||
| // For example: | ||
| // if (mapData.mapFeature && mapData.mapFeature.route_geometry && typeof drawRoute === 'function') { | ||
| // drawRoute(mapData.mapFeature.route_geometry); // Implement drawRoute function if needed | ||
| // } | ||
| // mapFeature route rendering is now handled declaratively by GeoJsonLayer | ||
| // mounted conditionally in the component return | ||
| }, [mapData.targetPosition, mapData.mapFeature, updateMapPosition]); | ||
|
|
||
| // Long-press handlers | ||
|
|
@@ -593,6 +591,19 @@ export const Mapbox: React.FC<{ position?: { latitude: number; longitude: number | |
| onMouseUp={handleMouseUp} | ||
| onMouseLeave={handleMouseUp} // Clear timer if mouse leaves container while pressed | ||
| /> | ||
| {mapData.mapFeature?.routeGeoJSON && ( | ||
| <GeoJsonLayer | ||
| id="map-feature-route" | ||
| data={{ | ||
| type: 'FeatureCollection', | ||
| features: [{ | ||
| type: 'Feature', | ||
| geometry: mapData.mapFeature.routeGeoJSON, | ||
| properties: {} | ||
| }] | ||
| }} | ||
| /> | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,7 @@ interface Location { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface McpResponse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| location: Location; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mapUrl?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| routeGeoJSON?: any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. 🧹 Nitpick | 🔵 Trivial Consider using a proper GeoJSON This is consistent with other files, but using 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface MapboxConfig { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -393,13 +394,33 @@ Uses the Mapbox Search Box Text Search API endpoint to power searching for and g | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Process results | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof content === 'object' && content !== null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parsedData = content as any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Extract route geometry if available (Directions tool) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let routeGeoJSON = undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (parsedData.geometry) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| routeGeoJSON = parsedData.geometry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (parsedData.routes && parsedData.routes.length > 0 && parsedData.routes[0].geometry) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| routeGeoJSON = parsedData.routes[0].geometry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (parsedData.results?.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const firstResult = parsedData.results[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mcpData = { location: { latitude: firstResult.coordinates?.latitude, longitude: firstResult.coordinates?.longitude, place_name: firstResult.name || firstResult.place_name, address: firstResult.full_address || firstResult.address }, mapUrl: parsedData.mapUrl }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mcpData = { location: { latitude: firstResult.coordinates?.latitude, longitude: firstResult.coordinates?.longitude, place_name: firstResult.name || firstResult.place_name, address: firstResult.full_address || firstResult.address }, mapUrl: parsedData.mapUrl, routeGeoJSON }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (parsedData.location) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mcpData = { location: { latitude: parsedData.location.latitude, longitude: parsedData.location.longitude, place_name: parsedData.location.place_name || parsedData.location.name, address: parsedData.location.address || parsedData.location.formatted_address }, mapUrl: parsedData.mapUrl || parsedData.map_url }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mcpData = { location: { latitude: parsedData.location.latitude, longitude: parsedData.location.longitude, place_name: parsedData.location.place_name || parsedData.location.name, address: parsedData.location.address || parsedData.location.formatted_address }, mapUrl: parsedData.mapUrl || parsedData.map_url, routeGeoJSON }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (parsedData.routes && parsedData.routes.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // It's a routing response, pick first route coordinates for map center | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const route = parsedData.routes[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let lat, lng; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (route.geometry && route.geometry.coordinates && route.geometry.coordinates.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Pick a point roughly in the middle, or the start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const coords = route.geometry.coordinates[Math.floor(route.geometry.coordinates.length / 2)]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lng = coords[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lat = coords[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mcpData = { location: { latitude: lat, longitude: lng, place_name: "Route", address: "Generated Route" }, routeGeoJSON }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+411
to
+421
Contributor
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. Route center coordinates may be undefined, causing downstream issues. When extracting the route center point, 🛠️ Proposed fix to handle empty coordinates } else if (parsedData.routes && parsedData.routes.length > 0) {
// It's a routing response, pick first route coordinates for map center
const route = parsedData.routes[0];
- let lat, lng;
+ let lat: number | undefined, lng: number | undefined;
if (route.geometry && route.geometry.coordinates && route.geometry.coordinates.length > 0) {
// Pick a point roughly in the middle, or the start
const coords = route.geometry.coordinates[Math.floor(route.geometry.coordinates.length / 2)];
- lng = coords[0];
- lat = coords[1];
+ if (Array.isArray(coords) && coords.length >= 2) {
+ lng = coords[0];
+ lat = coords[1];
+ }
+ }
+ // Fallback: if no valid center, use first coordinate pair
+ if (lat === undefined || lng === undefined) {
+ const firstCoord = route.geometry?.coordinates?.[0];
+ if (Array.isArray(firstCoord) && firstCoord.length >= 2) {
+ lng = firstCoord[0];
+ lat = firstCoord[1];
+ }
}
mcpData = { location: { latitude: lat, longitude: lng, place_name: "Route", address: "Generated Route" }, routeGeoJSON };📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error("Response missing required 'location' or 'results' field"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error("Response missing required 'location', 'results' or 'routes' field"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else throw new Error('Unexpected response format from mapping service'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🧹 Nitpick | 🔵 Trivial
Consider tightening the
mapFeaturetype.The index signature
[key: string]: anyis overly permissive and could mask type errors. If additional properties are needed, consider explicitly defining them or using a discriminated union. Also,routeGeoJSONcould benefit from a proper GeoJSONGeometrytype for better type safety.♻️ Suggested type improvement
📝 Committable suggestion
🤖 Prompt for AI Agents