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

## createMapContext

> [VERSION] Base Library >= 2.21.8

> 💡 The returned MapContext is bound to a map component via the ID and operates that map component.

### Description

Create a map context (MapContext) object

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | ID of the map component |

### Return Value

Type: `MapContext`

MapContext instance

### Examples

#### Create map context

```ts
Page({
  onReady() {
    const mapCtx = ty.createMapContext('myMap');
    mapCtx.getCenterLocation({
      success(res) {
        console.log('Center longitude and latitude', res.longitude, res.latitude);
      },
    });
  },
});
```
