---
name: "createMapContext"
mode: "api"
versionRequirements:
  - { name: "基础库", version: "2.21.8" }
title: "ty.createMapContext - 创建 map 上下文 MapContext 对象"
---

## createMapContext

> [VERSION] 基础库 >= 2.21.8

> 💡 返回的 MapContext 通过 id 跟一个 map 组件绑定，操作对应的 map 组件。

### 描述

创建 map 上下文 MapContext 对象

### 参数

`Params`

| 参数 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `id` | `string` | 是 | map 组件的 id |

### 返回值

类型: `MapContext`

MapContext 实例

### 示例代码

#### 创建地图上下文

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