lat-lng-to-tile
Convert geographic coordinates (latitude/longitude) to map tile coordinates (x, y) for specified zoom levels using web mercator projection.
Instructions
Convert latitude/longitude coordinates to map tile coordinates (x, y) for a given zoom level. Uses the same web mercator projection as MapKit.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| lat | Yes | Latitude in decimal degrees (-90 to 90) | |
| lng | Yes | Longitude in decimal degrees (-180 to 180) | |
| zoom | Yes | Zoom level (0-18) |
Implementation Reference
- src/index.ts:1092-1120 (handler)The handler function that performs the core logic: converts latitude/longitude and zoom to tile coordinates (x, y, z) using Web Mercator projection formulas.async (request) => { const { lat, lng, zoom } = request; // Proper web mercator tile coordinate calculation (same as MapKit) const n = Math.pow(2, zoom); const x = Math.floor((lng + 180) / 360 * n); // Convert latitude to radians const lat_rad = lat * Math.PI / 180; // Web mercator y calculation const y = Math.floor((1 - Math.asinh(Math.tan(lat_rad)) / Math.PI) / 2 * n); const data = { x, y, z: zoom, lat, lng, zoom, note: "Use these x,y coordinates with the map-tiles tool" }; return { content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }] }; }
- src/index.ts:1083-1091 (schema)Zod input schema defining parameters: lat, lng, zoom with validation and descriptions.{ title: "Latitude/Longitude to Tile", description: "Convert latitude/longitude coordinates to map tile coordinates (x, y) for a given zoom level. Uses the same web mercator projection as MapKit.", inputSchema: { lat: z.number().min(-90).max(90).describe("Latitude in decimal degrees (-90 to 90)"), lng: z.number().min(-180).max(180).describe("Longitude in decimal degrees (-180 to 180)"), zoom: z.number().int().min(0).max(18).describe("Zoom level (0-18)"), } },
- src/index.ts:1081-1121 (registration)Registration of the 'lat-lng-to-tile' tool with MCP server, including schema and handler.server.registerTool( "lat-lng-to-tile", { title: "Latitude/Longitude to Tile", description: "Convert latitude/longitude coordinates to map tile coordinates (x, y) for a given zoom level. Uses the same web mercator projection as MapKit.", inputSchema: { lat: z.number().min(-90).max(90).describe("Latitude in decimal degrees (-90 to 90)"), lng: z.number().min(-180).max(180).describe("Longitude in decimal degrees (-180 to 180)"), zoom: z.number().int().min(0).max(18).describe("Zoom level (0-18)"), } }, async (request) => { const { lat, lng, zoom } = request; // Proper web mercator tile coordinate calculation (same as MapKit) const n = Math.pow(2, zoom); const x = Math.floor((lng + 180) / 360 * n); // Convert latitude to radians const lat_rad = lat * Math.PI / 180; // Web mercator y calculation const y = Math.floor((1 - Math.asinh(Math.tan(lat_rad)) / Math.PI) / 2 * n); const data = { x, y, z: zoom, lat, lng, zoom, note: "Use these x,y coordinates with the map-tiles tool" }; return { content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }] }; } );