dtz30833 2017-11-20 15:38
浏览 31
已采纳

通过Google Place API添加位置时不断收到空响应

I'm trying to add a place within app scope via google place API. For this I'm using golang. But I keep on getting no result, there is no error message.

Here is my code

```

type latlng struct {
   lat, lng float64
}
type newPlace struct {
   location     latlng
   accuracy     int
   name         string
   phone_number string
   address      string
   types        string
}

func main() {
   requestUrl := "https://maps.googleapis.com/maps/api/place/add/json?key=<MYAPIKEY>"

   obj := newPlace{
      location: latlng{
        lat: 52.1502824,
        lng: 38.2643063,
     },
      name:  "some field",
      types: "storage",
   }

   bodyBytes, err := json.Marshal(&obj)
   if err != nil {
      panic(err)
   }
   body := bytes.NewReader(bodyBytes)
   rsp, err := http.NewRequest("POST", requestUrl, body)
   if err != nil {
       log.Fatal(err)
   }
   defer rsp.Body.Close()

   body_byte, err := ioutil.ReadAll(rsp.Body)
   if err != nil {
       panic(err)
   }
   fmt.Println(string(body_byte))
}

```

Here is the documentation that I followed. https://developers.google.com/places/web-service/add-place

I'm a bit new to golang, any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • douzhu7507 2017-11-20 16:19
    关注

    FYI I wrote this article on this touchy topic (JSON data encoded into a POST body request in Go).

    You're missing 4 things here:

    • the http.Client creation. Then you need to execute the request you're preparing with http.NewRequest by using client.Do.
    • add json fields to your struct and export variables contained in struct by capitalizing variables's first letters
    • set Content-Type to application/json
    • Google is expecting an array instead of a string in types, so I replace with an array containing 1 string (but you should adapt this depending on how many types you want to pass to Google)

    Here is a working script:

    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 main() {
        requestUrl := "https://maps.googleapis.com/maps/api/place/add/json?key=<your key>"
    
        types := [1]string{"storage"}
        obj := newPlace{
            Location: latlng{
                Lat: 52.1502824,
                Lng: 38.2643063,
            },
            Name:  "some field",
            Types: types,
        }
    
        bodyBytes, err := json.Marshal(&obj)
        if err != nil {
            fmt.Println(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)
        defer rsp.Body.Close()
    
        body_byte, err := ioutil.ReadAll(rsp.Body)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(string(body_byte))
    }
    

    Hope it's working now !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?