百度地图使用

it2022-05-05  109

百度地图使用指南

申请密钥 地址创建一个web项目copy百度的demo进行修改 <%-- Created by IntelliJ IDEA. User: Jamin Date: 2019/7/12 Time: 10:26 Desc:${desc} To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> <style type="text/css"> body, html, #allmap { width: 100%; height: 100%; overflow: hidden; margin: 0; font-family: "微软雅黑"; } </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=TbQIpdknazbGPcCqe0D31eluZmIMwXyU"></script> <title>地图展示</title> </head> <body> <div id="allmap"></div> </body> </html> <script type="text/javascript"> // 百度地图API功能 // 创建Map实例 var map = new BMap.Map("allmap", {enableMapClick: false}); // 初始化地图,设置中心点坐标和地图级别 map.centerAndZoom(new BMap.Point(116.404, 39.915), 18); //右上角工具 map.addControl(new BMap.MapTypeControl({ mapTypes: [ // 普通地图 BMAP_NORMAL_MAP, // 实景地图 BMAP_HYBRID_MAP ] })); // 添加带有定位的导航控件 var navigationControl = new BMap.NavigationControl({ // 靠左上角位置 anchor: BMAP_ANCHOR_TOP_LEFT, // LARGE类型 type: BMAP_NAVIGATION_CONTROL_LARGE, // 启用显示定位 enableGeolocation: true }); map.addControl(navigationControl); // 添加定位控件(左下角) var geolocationControl = new BMap.GeolocationControl(); geolocationControl.addEventListener("locationSuccess", function (e) { // 定位成功事件 var address = ''; address += e.addressComponent.province; address += e.addressComponent.city; address += e.addressComponent.district; address += e.addressComponent.street; address += e.addressComponent.streetNumber; alert("当前定位地址为:" + address); }); geolocationControl.addEventListener("locationError", function (e) { // 定位失败事件 alert(e.message); }); map.addControl(geolocationControl); //开启鼠标滚轮缩放 map.enableScrollWheelZoom(true); // 开局定位 var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function (r) { if (this.getStatus() == BMAP_STATUS_SUCCESS) { //创建标点 var mk = new BMap.Marker(r.point); map.addOverlay(mk, 15); //用于跳转到定位点 map.panTo(r.point); alert('您的位置' + r.point.lng + "--------" + r.point.lat); } else { alert("失败"); } }) </script>

转载于:https://www.cnblogs.com/JaminYe/p/11175852.html


最新回复(0)