lat-lng-to-tile
Convert latitude and longitude coordinates to map tile coordinates (x, y) for a specific zoom level using the web mercator projection, ideal for mapping and geospatial applications.
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 implements the tool logic: computes Web Mercator tile coordinates (x, y, z) from input lat, lng, and zoom, then returns formatted JSON response.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:1086-1090 (schema)Zod schema for input validation: defines parameters lat (-90..90), lng (-180..180), zoom (int 0..18).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)Registers the "lat-lng-to-tile" tool on the MCP server with title, description, input schema, and handler function.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) }] }; } );