go-staticmaps icon indicating copy to clipboard operation
go-staticmaps copied to clipboard

Transformer assumes tiles are centered, leads to lat/lon with offset

Open mschilli opened this issue 9 months ago • 0 comments

Hi there, go-staticmaps is great, and I was pleased to find a Transformer() function to let me translate taps/mouse clicks on the map into lat/lon values. Interestingly, though, this leads to discrepancies when the viewport has a tile that's not centered, i.e. a click into the center of the viewport does not coincide with the center of the corresponding tile.

Here's the output of the snippet below, which shows the discrepancy, but I wonder: Is this by design or should Transformer() return the actual lat/lon by factoring in the known offset?

Center: 37.874900, -122.419400 Tapped at center: 37.914727, -122.469868

package main

import (
        "fmt"
        sm "github.com/flopp/go-staticmaps"
        "github.com/golang/geo/s2"
)

func main() {
        width := 800
        height := 600
        zoom := 12
        centerLat := 37.8749
        centerLon := -122.4194

        ctx := sm.NewContext()
        ctx.SetSize(width, height)
        ctx.SetZoom(zoom)
        ctx.SetCenter(s2.LatLngFromDegrees(centerLat, centerLon))

        transformer, err := ctx.Transformer()
        if err != nil {
                panic(err)
        }

        // Tap at center
        tapX := float64(width) / 2
        tapY := float64(height) / 2

        latLng := transformer.XYToLatLng(tapX, tapY)
        fmt.Printf("Center:  %.6f, %.6f\n", centerLat, centerLon)
        fmt.Printf("Tapped at center: %.6f, %.6f\n", latLng.Lat.Degrees(), latLng.Lng.Degrees())
}


mschilli avatar May 02 '25 21:05 mschilli