Files
baseapi/platformapi/autonavi/autonavi_page.go
suyl 706167fad2 aa
2021-05-11 16:09:10 +08:00

114 lines
4.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package autonavi
import (
"fmt"
"net/http"
"strings"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
func (a *API) AccessStorePage(fullURL string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
var request *http.Request
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", bizParams), nil)
request.Header.Set("Referer", "https://lbs.amap.com/")
// a.FillRequestCookies(request)
return request
},
a.config,
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
if jsonResult1 == nil {
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
}
if strings.Contains(bodyStr, "登录") || strings.Contains(bodyStr, "访问的内容") {
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("cookie可能过期了")
}
if err == nil {
if jsonResult1["info"].(string) != "OK" {
errLevel = platformapi.ErrLevelGeneralFail
err = utils.NewErrorCode(jsonResult1["info"].(string), jsonResult1["infocode"].(string))
baseapi.SugarLogger.Debugf("autonavi_page AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
}
retVal = jsonResult1
}
return errLevel, err
})
return retVal, err
}
func (a *API) GetCoordinateFromAddressByPage(address string, cityCode int) (lng, lat float64, err error) {
result, err := a.AccessStorePage("https://restapi.amap.com/v3/place/text", map[string]interface{}{
"s": "rsv3",
"key": "e07ffdf58c8e8672037bef0d6cae7d4a",
"page": 1,
"offset": 10,
"city": cityCode,
"language": "zh_cn",
"platform": "JS",
"logversion": 2.0,
"sdkversion": 1.3,
"appname": "https://lbs.amap.com/console/show/picker",
"csid": "7A90908C-BBA6-49FE-895E-DB70600E14F8",
"keywords": address,
"children": "",
})
if err == nil {
if len(result["pois"].([]interface{})) > 0 {
str := result["pois"].([]interface{})[0].(map[string]interface{})["location"].(string)
strs := strings.Split(str, ",")
if len(strs) > 0 {
return utils.Str2Float64(strs[0]), utils.Str2Float64(strs[1]), err
}
}
}
return lng, lat, err
}
type GetCoordinateFromAddressByPageAllResult struct {
ID string `json:"id"`
Parent []interface{} `json:"parent"`
Childtype []interface{} `json:"childtype"`
Name string `json:"name"`
Type string `json:"type"`
Typecode string `json:"typecode"`
BizType []interface{} `json:"biz_type"`
Address string `json:"address"`
Location string `json:"location"`
Tel []interface{} `json:"tel"`
Pname string `json:"pname"`
Cityname string `json:"cityname"`
Adname string `json:"adname"`
Importance []interface{} `json:"importance"`
Shopid []interface{} `json:"shopid"`
Shopinfo string `json:"shopinfo"`
Poiweight []interface{} `json:"poiweight"`
Distance string `json:"distance"`
Photos []interface{} `json:"photos"`
}
func (a *API) GetCoordinateFromAddressByPageAll(address string, cityCode int) (getCoordinateFromAddressByPageAllResult *GetCoordinateFromAddressByPageAllResult, err error) {
result, err := a.AccessStorePage("https://restapi.amap.com/v3/place/text", map[string]interface{}{
"s": "rsv3",
"key": "e07ffdf58c8e8672037bef0d6cae7d4a",
"page": 1,
"offset": 10,
"city": cityCode,
"language": "zh_cn",
"platform": "JS",
"logversion": 2.0,
"sdkversion": 1.3,
"appname": "https://lbs.amap.com/console/show/picker",
"csid": "7A90908C-BBA6-49FE-895E-DB70600E14F8",
"keywords": address,
"children": "",
})
if err == nil {
utils.Map2StructByJson(result["pois"].([]interface{})[0], &getCoordinateFromAddressByPageAllResult, false)
}
return getCoordinateFromAddressByPageAllResult, err
}