---
name: "MapContext.getCenterLocation"
mode: "api"
versionRequirements:
  - { name: "基础库", version: "2.21.8" }
title: "MapContext.getCenterLocation - 获取当前地图中心的经纬度"
---

## MapContext.getCenterLocation

> [VERSION] 基础库 >= 2.21.8

### 描述

获取当前地图中心的经纬度

### 参数

`Params`

| 参数 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `option` | `GetCenterLocationOption` | 是 | 回调配置 |

### 返回值

无


### 引用对象

##### `interface` GetCenterLocationOption

| 属性 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `iconPath` | `string` | 否 | 图标路径，支持网络路径、代码包路径 |
| `clear` | `boolean` | 否 | 是否先清空地图上所有 marker |
| `success` | `(result: GetCenterLocationResult) => void` | 否 | 接口调用成功的回调函数 |
| `fail` | `(result: MapFailResult) => void` | 否 | 接口调用失败的回调函数 |
| `complete` | `() => void` | 否 | 接口调用结束的回调函数（调用成功、失败都会执行） |

##### `interface` GetCenterLocationResult

| 属性 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `longitude` | `number` | 是 | 经度 |
| `latitude` | `number` | 是 | 纬度 |

##### `interface` MapFailResult

| 属性 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `errorCode` | `string` | 是 | 错误码 |
| `errorMsg` | `string` | 是 | 错误信息 |


### 示例代码

#### 获取地图中心经纬度

```ts
const mapCtx = ty.createMapContext('myMap');
mapCtx.getCenterLocation({
  success(res) {
    console.log('中心经度', res.longitude);
    console.log('中心纬度', res.latitude);
  },
});
```
