-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (51 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"image/color"
"io/ioutil"
"strconv"
_ "strconv"
"strings"
sm "github.com/flopp/go-staticmaps"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
"log"
"net/http"
)
func main() {
log.Println("0")
http.HandleFunc("/", handler)
log.Println("1")
log.Fatal(http.ListenAndServe(":8080", nil))
log.Println("2")
}
func createImage(lat float64, long float64) string {
ctx := sm.NewContext()
ctx.SetSize(400, 400)
ctx.SetZoom(18)
ctx.AddMarker(sm.NewMarker(s2.LatLngFromDegrees(lat, long), color.RGBA{0xff, 0, 0, 0xff}, 16.0))
img, err := ctx.Render()
if err != nil {
panic(err)
}
name := fmt.Sprintf("%v%f%v%f%v", "images/", lat, ",", long, ".png")
if err := gg.SavePNG(name, img); err != nil {
panic(err)
}
return name
}
func handler(w http.ResponseWriter, r *http.Request) {
request := r.URL.String()
if strings.Contains(request, "K3340") { //put some API security here if the service gets abused.
request = strings.TrimLeft(request, "/maps/")
parsed := strings.Split(request, ",")
lat, _ := strconv.ParseFloat(parsed[0], 64)
long, _ := strconv.ParseFloat(parsed[1], 64)
path := createImage(lat, long)
w.Header().Set("Content-Type", "image/png")
image, _ := ioutil.ReadFile(path)
w.Write(image)
} else {
fmt.Fprintf(w, "Error: no API key found in your request!")
}
}