百度地图API示例之设置地图显示范围
代码

<!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 {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
#allmap{width:100%;height:500px;}
p{margin-left:5px; font-size:14px;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=sSelQoVi2L3KofLo1HOobonW"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/AreaRestriction/1.2/src/AreaRestriction_min.js"></script>
<title>设置地图显示范围</title>
</head>
<body>
<div id="allmap"></div>
<p>将地图显示范围设定在指定区域,地图拖出该区域后会重新弹回。</p>
</body>
</html>
<script type="text/javascript">
//百度地图API功能
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.404, 39.915), 13);
// map.centerAndZoom(new BMap.Point(116.027143, 39.772348),13); // 测试为左下角的位置
// map.centerAndZoom(new BMap.Point(116.832025, 40.126349),13); // 测试为右上角的位置
map.enableScrollWheelZoom(); // 允许滚动
var b = new BMap.Bounds(new BMap.Point(116.027143, 39.772348),new BMap.Point(116.832025, 40.126349)); // 范围 左下角,右上角的点位置
try { // js中尽然还有try catch方法,可以避免bug引起的错误
BMapLib.AreaRestriction.setBounds(map, b); // 已map为中心,已b为范围的地图
} catch (e) {
// 捕获错误异常
alert(e);
}
</script>

引入更多的类AreaRestriction_min
/**
* @fileoverview 百度地图浏览区域限制类,对外开放。
* 允许开发者输入限定浏览的地图区域的Bounds值,
* 则地图浏览者只能在限定区域内浏览地图。
* 基于Baidu Map API 1.2。
*
* @author Baidu Map Api Group
* @version 1.2
*/
/**
* @namespace BMap的所有library类均放在BMapLib命名空间下
*/
var BMapLib = window.BMapLib = BMapLib || {};
(function() {
/**
* @exports AreaRestriction as BMapLib.AreaRestriction
*/
var AreaRestriction =
/**
* AreaRestriction类,静态类,不用实例化
* @class AreaRestriction类提供的都是静态方法,勿需实例化即可使用。
*/
BMapLib.AreaRestriction = function(){
}
/**
* 是否已经对区域进行过限定的标识
* @private
* @type {Boolean}
*/
var _isRestricted = false;
/**
* map对象
* @private
* @type {BMap}
*/
var _map = null;
/**
* 开发者需要限定的区域
* @private
* @type {BMap.Bounds}
*/
var _bounds = null;
/**
* 对可浏览地图区域的限定方法
* @param {BMap} map map对象
* @param {BMap.Bounds} bounds 开发者需要限定的区域
*
* @return {Boolean} 完成了对区域的限制即返回true,否则为false
*/
AreaRestriction.setBounds = function(map, bounds){
// 验证输入值的合法性
if (!map ||
!bounds ||
!(bounds instanceof BMap.Bounds)) {
throw "请检查传入参数值的合法性";
return false;
}
if (_isRestricted) {
this.clearBounds();
}
_map = map;
_bounds = bounds;
// 添加地图的moving事件,用以对浏览区域的限制
_map.addEventListener("moveend", this._mapMoveendEvent);
_isRestricted = true;
return true;
};
/**
* 需要绑定在地图移动事件中的操作,主要控制出界时的地图重新定位
* @param {Event} e e对象
*
* @return 无返回值
*/
AreaRestriction._mapMoveendEvent = function(e) {
// 如果当前完全没有出界,则无操作
if (_bounds.containsBounds(_map.getBounds())) {
return;
}
// 两个需要对比的bound区域的边界值
var curBounds = _map.getBounds(),
curBoundsSW = curBounds.getSouthWest(),
curBoundsNE = curBounds.getNorthEast(),
_boundsSW = _bounds.getSouthWest(),
_boundsNE = _bounds.getNorthEast();
// 需要计算定位中心点的四个边界
var boundary = {n : 0, e : 0, s : 0, w : 0};
// 计算需要定位的中心点的上方边界
boundary.n = (curBoundsNE.lat < _boundsNE.lat) ?
curBoundsNE.lat :
_boundsNE.lat;
// 计算需要定位的中心点的右边边界
boundary.e = (curBoundsNE.lng < _boundsNE.lng) ?
curBoundsNE.lng :
_boundsNE.lng;
// 计算需要定位的中心点的下方边界
boundary.s = (curBoundsSW.lat < _boundsSW.lat) ?
_boundsSW.lat :
curBoundsSW.lat;
// 计算需要定位的中心点的左边边界
boundary.w = (curBoundsSW.lng < _boundsSW.lng) ?
_boundsSW.lng :
curBoundsSW.lng;
// 设置新的中心点
var center = new BMap.Point(boundary.w + (boundary.e - boundary.w) / 2,
boundary.s + (boundary.n - boundary.s) / 2);
setTimeout(function() {
_map.panTo(center, {noAnimation : "no"});
}, 1);
};
/**
* 清除对地图浏览区域限定的状态
* @return 无返回值
*/
AreaRestriction.clearBounds = function(){
if (!_isRestricted) {
return;
}
_map.removeEventListener("moveend", this._mapMoveendEvent);
_isRestricted = false;
};
})();
百度地图API示例之设置地图显示范围的更多相关文章
- 百度地图API示例之设置地图最大、最小级别
代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...
- 百度地图API示例之设置级别setZoom与禁止拖拽disableDragging
百度地图API示例之设置级别setZoom与禁止拖拽disableDragging 设置级别 <html> <head> <meta http-equiv="C ...
- 百度地图API示例之移动地图
级别为6 级别为8 级别为12 代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Conten ...
- 百度地图API示例之根据城市名设置地图中心点
代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" con ...
- 【百度地图API】如何利用地图API制作汽车沿道路行驶的动画?——如何获得道路层数据
原文:[百度地图API]如何利用地图API制作汽车沿道路行驶的动画?--如何获得道路层数据 有几个做汽车导航的朋友问我说,他们想在地图上制作一辆车沿着道路行驶的动画.可是,百度地图的道路数据并没有公开 ...
- 【百度地图API】如何自定义地图图层?实例:制作麻点图(自定义图层+热区)
原文:[百度地图API]如何自定义地图图层?实例:制作麻点图(自定义图层+热区) 摘要:自定义地图图层的用途十分广泛.常见的应用,比如制作魔兽地图和清华校园地图(使用切图工具即可轻松实现).今天我们来 ...
- 【百度地图API】如何在地图上添加标注?——另有:坐标拾取工具+打车费用接口介绍
原文:[百度地图API]如何在地图上添加标注?--另有:坐标拾取工具+打车费用接口介绍 摘要: 在这篇文章中,你将学会,如何利用百度地图API进行标注.如何使用API新增的打车费用接口. ------ ...
- 【百度地图API】多家地图API内存消耗对比测验(带源码)
原文:[百度地图API]多家地图API内存消耗对比测验(带源码) 任务描述: 啊,美妙的春节结束了.酸奶小妹和妈妈的山西平遥之旅也宣告成功!距离平遥古城7km,有一个同样身为“世界文化遗产”的寺庙,叫 ...
- 百度地图api通过地址显示地图,白名单
百度地图api通过地址显示地图,白名单 http://developer.baidu.com/map/jsdemo.htm#i7_1?qq-pf-to=pcqq.c2c---------------- ...
随机推荐
- 团队开发——冲刺1.b
冲刺阶段一(第二天) 1.昨天做了什么? 在了解C#的基础上,深入熟悉Windows窗体应用程序,熟练掌握基本功能 2.今天准备做什么? 在C#的Windows窗体应用程序中,设计简单的游戏开始主界面 ...
- NOI Linux JAVA
右键open with another application 里输javaws 按ctrl+shift+del,调出火狐内置的清除最近的历史记录工具(或者按alt键弹出菜单,工具->清空最近历 ...
- iOS Xcodebuild
简介 xcodebuild 是苹果发布自动构建的工具.它在一个Xcode项目下能构建一个或者多个targets ,也能在一个workspace或者Xcode项目上构建scheme,总的来说,用它没错就 ...
- JS运动基础(二) 摩擦运动、缓冲运动
摩擦运动: 逐渐变慢,最后停止 缓冲运动: 与摩擦力的区别:可以精确的停到指定目标点距离越远速度越大速度由距离决定速度=(目标值-当前值)/缩放系数Bug:速度取整值取整: iSpeed = iSpe ...
- 移动互联网实战--Web Restful API设计和基础架构
前言: 在移动互联网的大潮中, Web Restful API逐渐成为Web Server重要的一个分支. 移动端和服务端的交互, 主流的方式还是通过Http协议的形式来进行. 请求以Get/Post ...
- Monte Carlo Approximations
准备总结几篇关于 Markov Chain Monte Carlo 的笔记. 本系列笔记主要译自A Gentle Introduction to Markov Chain Monte Carlo (M ...
- xmind的第七天笔记
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
- JSBinding / About 2048 sample
2048 Source 2048 source code is here: https://github.com/gabrielecirulli/2048 Play here!http://gabri ...
- pt-online-schema-change 实例
pt-pmp (http://www.cnblogs.com/ivictor/p/6012183.html) pt-online-schema-change (http://blog.csdn.net ...