Skip to content

Instantly share code, notes, and snippets.

@ivanmrchk
Created September 23, 2019 14:51
Show Gist options
  • Save ivanmrchk/12eb8e7e2011f1c83d98874002dd3ece to your computer and use it in GitHub Desktop.
Save ivanmrchk/12eb8e7e2011f1c83d98874002dd3ece to your computer and use it in GitHub Desktop.
Delete and add places from google map with Golang
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type latlng struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
}
type newPlace struct {
Location latlng `json:"location"`
Accuracy int `json:"accuracy"`
Name string `json:"name"`
PhoneNumber string `json:"phone_number"`
Address string `json:"address"`
Types [1]string `json:"types"`
}
func check(err error) {
if err != nil {
log.Fatalf("fatal error: %s", err)
}
}
func main() {
// api := "<API KEY>"
requestUrl := "https://maps.googleapis.com/maps/api/place/add/json?key=<API KEY>"
types := [1]string{"storage"}
obj := newPlace{
Location: latlng{
Lat: 52.1502824,
Lng: 38.2643063,
},
Name: "01",
Types: types,
}
bodyBytes, err := json.Marshal(&obj)
check(err)
body := bytes.NewReader(bodyBytes)
client := &http.Client{}
req, err := http.NewRequest("POST", requestUrl, body)
req.Header.Add("Content-Type", "application/json")
if err != nil {
fmt.Println(err)
}
rsp, err := client.Do(req)
check(err)
defer rsp.Body.Close()
body_byte, err := ioutil.ReadAll(rsp.Body)
check(err)
fmt.Println(string(body_byte))
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type deletePlace struct {
PlaceId string `json:"place_id"`
}
func check(err error) {
if err != nil {
log.Fatalf("fatal error: %s", err)
}
}
func main() {
// api := "<API KEY>"
requestUrl := "https://maps.googleapis.com/maps/api/place/delete/json?key=<API KEY>"
obj := deletePlace{
PlaceId: "qgYvCi0wMDAwMDAzMGYyZjg2ZTU3OjQxM2FhNzEwMDAxOjQyODRhNzJkNTVkZDMwMmE",
}
bodyBytes, err := json.Marshal(&obj)
check(err)
body := bytes.NewReader(bodyBytes)
client := &http.Client{}
req, err := http.NewRequest("POST", requestUrl, body)
req.Header.Add("Content-Type", "application/json")
check(err)
rsp, err := client.Do(req)
check(err)
defer rsp.Body.Close()
body_byte, err := ioutil.ReadAll(rsp.Body)
check(err)
fmt.Println(string(body_byte))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment