-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtareaa.go
137 lines (110 loc) · 3.88 KB
/
tareaa.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
"golang.org/x/net/context"
"googlemaps.github.io/maps"
)
type Dirreq struct {
Origin string `json:"origen"`
Destination string `json:"destino"`
}
type Restaurantreq struct {
Location string `json:"origen"`
}
func getDirections(w http.ResponseWriter, p *http.Request) {
var orig Dirreq
//orig.Origen = "Hannover"
//orig.Destino = "Luxembourg"
_ = json.NewDecoder(p.Body).Decode(&orig)
c, erro := maps.NewClient(maps.WithAPIKey("AIzaSyAkMHjwcRtPz9trV2DUb6N6R3E_m7aH91s"))
if erro != nil {
log.Fatalf("fatal error: %s", erro)
}
r := &maps.DirectionsRequest{
Origin: orig.Origin,
Destination: orig.Destination,
}
listarutas, _, errr := c.Directions(context.Background(), r)
if errr != nil {
log.Fatalf("fatal error: %s", errr)
}
rutaStr := new(bytes.Buffer)
rutaStr.WriteString("{\n\"ruta\":[\n")
json.NewDecoder(p.Body).Decode(&listarutas)
for ind := 0; ind < len(listarutas[0].Legs[0].Steps); ind++ {
rutaStr.WriteString("{\n \"lat\": ")
rutaStr.WriteString(strconv.FormatFloat(listarutas[0].Legs[0].Steps[ind].StartLocation.Lat, 'f', 4, 64))
rutaStr.WriteString(",")
rutaStr.WriteString(" \n \"long\": ")
rutaStr.WriteString(strconv.FormatFloat(listarutas[0].Legs[0].Steps[ind].StartLocation.Lng, 'f', 4, 64))
rutaStr.WriteString("\n},\n")
if ind == (len(listarutas[0].Legs[0].Steps) - 1) {
rutaStr.WriteString("{\n \"lat\": ")
rutaStr.WriteString(strconv.FormatFloat(listarutas[0].Legs[0].Steps[ind].StartLocation.Lat, 'f', 4, 64))
rutaStr.WriteString(",")
rutaStr.WriteString(" \n \"long\": ")
rutaStr.WriteString(strconv.FormatFloat(listarutas[0].Legs[0].Steps[ind].StartLocation.Lng, 'f', 4, 64))
rutaStr.WriteString("\n}")
}
}
rutaStr.WriteString("\n ]\n}")
fmt.Fprintf(w, rutaStr.String())
//pretty.Println(buff.String())
}
func getRestaurants(w http.ResponseWriter, p *http.Request) {
var locat Restaurantreq
_ = json.NewDecoder(p.Body).Decode(&locat)
dClient, err := maps.NewClient(maps.WithAPIKey("AIzaSyAkMHjwcRtPz9trV2DUb6N6R3E_m7aH91s"))
if err != nil {
log.Fatalf("fatal error: %s", err)
}
r := &maps.DirectionsRequest{
Origin: locat.Location,
Destination: locat.Location,
}
route, _, errr := dClient.Directions(context.Background(), r)
if errr != nil {
log.Fatalf("fatal error: %s", errr)
}
json.NewDecoder(p.Body).Decode(&route)
cl, _ := maps.NewClient(maps.WithAPIKey("AIzaSyAkMHjwcRtPz9trV2DUb6N6R3E_m7aH91s"))
var latitud float64 = route[0].Legs[0].Steps[0].StartLocation.Lat
var longitud float64 = route[0].Legs[0].Steps[0].StartLocation.Lng
req := &maps.NearbySearchRequest{
Location: &maps.LatLng{latitud, longitud},
Radius: 10000,
Type: maps.PlaceTypeRestaurant,
Keyword: "restaurant",
//placed keyword for more accurate results
}
listarestau, _ := cl.NearbySearch(context.Background(), req)
json.NewDecoder(p.Body).Decode(&listarestau)
rstrnts := new(bytes.Buffer)
rstrnts.WriteString("{\n\"Restaurantes\":[\n")
for ind := 0; ind < len(listarestau.Results); ind++ {
rstrnts.WriteString("{\n\"nombre\": ")
rstrnts.WriteString("\"" + listarestau.Results[ind].Name + "\",\n")
rstrnts.WriteString("\"lat\": ")
rstrnts.WriteString(strconv.FormatFloat(listarestau.Results[ind].Geometry.Location.Lat, 'f', 4, 64))
rstrnts.WriteString(",\n")
rstrnts.WriteString("\"long\": ")
rstrnts.WriteString(strconv.FormatFloat(listarestau.Results[ind].Geometry.Location.Lng, 'f', 4, 64))
rstrnts.WriteString("\n },\n")
}
rstrnts.WriteString(" ]\n}")
fmt.Fprintf(w, rstrnts.String())
//pretty.Println(rstrnts.String())
}
func main() {
srvs := mux.NewRouter()
srvs.HandleFunc("/ejercicio1", getDirections).Methods("POST")
srvs.HandleFunc("/ejercicio2", getRestaurants).Methods("POST")
log.Println("Listening to http://localhost:8686...")
http.ListenAndServe(":8686", srvs)
}