获取时区

功能介绍

根据经纬度获取地区的时区信息。

使用约束

返回1条记录。

接口原型

承载协议

HTTPS POST

接口方向

开发者 -> 华为地图服务器

接口URL

https://siteapi.cloud.huawei.com/mapApi/v1/timezoneService/getTimezone?key=API KEY

说明
  1. 获取API KEY的方式请参见获取API密钥。
  2. API秘钥使用有2种方式:

    2.1 拼接URL中:使用API密钥时需要调用URLEncoder.encode("Your apiKey", "UTF-8")方法对API密钥进行encodeURI编码。例如,原始API密钥:ABC/DFG+ ,转换结果:ABC%2FDFG%2B。

    2.2 放在Header内:参考Request Header中Authorization字段,推荐使用该方式。

数据格式

请求消息:Content-Type: application/json

响应消息:Content-Type: application/json

请求参数

Request Header

展开

参数

是否必选

参数类型

描述

Authorization

是

String

业务签名,用于认证鉴权,格式为:Bearer+空格+API Key。

示例:Bearer ABC/DFG+。

Content-Type

是

application/json

请求消息的数据格式。

Request Body

展开

参数

是否必选

参数类型

描述

location

是

Coordinate

经纬度。

timestamp

是

Long

距离UTC 1970年1月1日0点0分0秒的秒数。

language

否

String(<=16)

搜索结果的语种,语种取值请参见支持的语言列表。如果不传,返回地点的当地语言。

请求示例

POST https://siteapi.cloud.huawei.com/mapApi/v1/timezoneService/getTimezone?key=API KEY   HTTP/1.1 
Content-Type: application/json 
Accept: application/json 
{
    "location": {
        "lng": 30.23235,
        "lat": 12.242585
    },
    "timestamp": 1577435043,
    "language": "en"
}

响应参数

状态码为200时:

Response Header

展开

参数

是否必选

参数类型

描述

Content-Type

是

application/json

响应消息的数据格式。

Response Body

展开

参数名称

参数类型

描述

returnCode

String

返回码,具体请参见错误码。

returnDesc

String

返回值描述。

dstOffset

int

夏令时的偏移量,单位:秒。

如果给定的timestamp不在夏令时里,返回0。

rawOffset

int

给定位置与UTC的偏移量(单位:秒)。

timeZoneId

String

时区ID。

例如“ America / Los_Angeles”或“ Australia / Sydney”

这些ID由Unicode 公共区域数据存储库(CLDR)项目定义。

可以参见GitHub上的链接。

timeZoneName

String

时区名称。比如太平洋夏令时。

响应示例

状态码为200时:

HTTP/1.1 200 OK
Content-type: application/json
{
    "returnCode": "0",
    "timeZoneName": "Central African Time",
    "rawOffset": 7200,
    "timeZoneId": "Africa/Khartoum",
    "dstOffset": 0,
    "returnDesc": "OK"
}

调用示例

public class TimeZoneService {
    public static final String ROOT_URL = "https://siteapi.cloud.huawei.com/mapApi/v1/timezoneService/getTimezone";

    public static final String connection = "?key=";

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static void timeZone(String apiKey) throws UnsupportedEncodingException {
        JSONObject json = new JSONObject();

        JSONObject location = new JSONObject();
        try {
            location.put("lng", 30.23235);
            location.put("lat", 12.242585);
            json.put("location", location);
            json.put("timestamp",1577435043);

        } catch (JSONException e) {
            Log.e("error", e.getMessage());
        }
        RequestBody body = RequestBody.create(JSON, String.valueOf(json));

        OkHttpClient client = new OkHttpClient();
        Request request =
            new Request.Builder().url(ROOT_URL + connection + URLEncoder.encode(apiKey, "UTF-8"))
                .post(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("TimeZone", e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("TimeZone", response.body().string());
            }
        });
    }
}