百度地图开放平台
更新时间: 2026/10/22 14:15
JSAPI 2.0 已停止维护并废弃,不再提供功能更新和问题修复,禁止用于新项目开发。现有项目请尽快迁移到 JSAPI 4.0,迁移方法请参考升级指南。4.0 兼容 2.0 的 BMap 命名空间,多数项目只需修改脚本引用中的版本号。
JSAPI 2.0 is deprecated and no longer maintained. It receives no feature updates or bug fixes and must not be used for new projects. Migrate existing projects to JSAPI 4.0 as soon as possible; see the Migration Guide. JSAPI 4.0 is compatible with the 2.0 BMap namespace, so most projects only need to change the version number in the script URL.
地图展示

我们从展示一个以北京的天安门为中心的地图开始。以下为Demo和示例。

DEMO详情

1. 编写HTML页面的基础代码

在这一步我们完成头部文件和地图容器样式的编写,让地图容器充满网页。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Baidu Map </title>
<style type="text/css">
html{height:100%}
body{height:100%;margin:0px;padding:0px}
#container{height:100%}
</style>
</head>
<body></body>
</html>
2. 引入百度地图API文件
<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
3. 初始化地图逻辑

首先创建地图实例,之后用一个Point坐标点和缩放级别来初始化地图。

var map = new BMap.Map("container"); // 创建地图实例
var point = new BMap.Point(116.404, 39.915); // 创建点坐标
map.centerAndZoom(point, 15); // 初始化地图,设置中心点坐标和地图级别
4. 开启鼠标滚轮缩放

地图的鼠标滚轮缩放默认是关闭的,需要配置开启。

map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放

至此,我们完成了一个更加完整的地图展示的例子。

上一篇

坐标转换说明

下一篇

添加控件
本篇文章对您是否有帮助?