Files
baseapi/platformapi/autonavi/autonavi_page.go
suyl 5c6f604d7d aa
2021-05-11 15:42:06 +08:00

128 lines
4.4 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 {
Suggestion struct {
Keywords []interface{} `json:"keywords"`
Cities []interface{} `json:"cities"`
} `json:"suggestion"`
Count string `json:"count"`
Infocode string `json:"infocode"`
Pois []struct {
Parent []interface{} `json:"parent"`
Address string `json:"address"`
Distance []interface{} `json:"distance"`
Pname string `json:"pname"`
Importance []interface{} `json:"importance"`
BizExt struct {
Cost []interface{} `json:"cost"`
Rating []interface{} `json:"rating"`
} `json:"biz_ext"`
BizType []interface{} `json:"biz_type"`
Cityname string `json:"cityname"`
Type string `json:"type"`
Photos []interface{} `json:"photos"`
Typecode string `json:"typecode"`
Shopinfo string `json:"shopinfo"`
Poiweight []interface{} `json:"poiweight"`
Childtype []interface{} `json:"childtype"`
Adname string `json:"adname"`
Name string `json:"name"`
Location string `json:"location"`
Tel string `json:"tel"`
Shopid []interface{} `json:"shopid"`
ID string `json:"id"`
} `json:"pois"`
Status string `json:"status"`
Info string `json:"info"`
}
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, &getCoordinateFromAddressByPageAllResult, false)
}
return getCoordinateFromAddressByPageAllResult, err
}