百度地图覆盖物API 判断点击的是地图覆盖物还是覆盖物

后使用快捷导航没有帐号?
暂时没有人问过相似的问题,你可以做第一个提问题的人
查看: 1193|回复: 2
如何获取覆盖物类型
本帖最后由 wyh4680711 于
19:08 编辑
我现在的项目里面有Marker和Polygon这两种覆盖物,我要区分这两种覆盖物做处理代码是
var array=map.getOverlays();
for(var i=0;i&array.i++){
& && &&&var marker=array[i];
& && &&&//获取遮罩物是网格类型的
& && &&&if(marker.SG==&Polygon&){
& && &&&}else{
& && &&&}
}复制代码之前是使用的SG获取,后来发现程序都不能正常获取了,发现改成CH了,这才了解不能这样获取类型,有没有人知道有啥方法获取,谢谢
wyh4680711
亲,请参考:另外,一定不要自己去控制台里抓变量用啊,要用也得用类参考提供的方法、属性啊。
coralreef1217
谢谢,已经解决了
Powered by百度地图api小技巧:在自定义覆盖物上实现clearOverlays()不清除
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。13998人阅读
本文适合零基础百度地图API开发者
直接上代码;另注:code上面很多是钓鱼君自己写的动态的数据,所以不能拿过去马上可以用,本文只提供借鉴,不保证100%原创,但功能是修改之后已经实现。欢迎找茬,欢迎指教。
&input type=&hidden& value='${mapList }' id=&mapList&&
&div id=&container&&&/div&
&script type=&text/javascript&&
var basePath = '&%=basePath%&';
var map = new BMap.Map(&container&); // 创建地图实例
var point = new BMap.Point(121.5981); // 创建点坐标
map.centerAndZoom(point, 11); // 初始化地图,设置中心点坐标和地图级别
/* map.addControl(new BMap.NavigationControl({
// 靠左上角位置
anchor: BMAP_ANCHOR_TOP_LEFT,
// LARGE类型
type: BMAP_NAVIGATION_CONTROL_LARGE,
// 启用显示定位
enableGeolocation: true
}));// 添加带有定位的导航控件 */
//map.addControl(new BMap.ScaleControl());//比例尺控件
//map.addControl(new BMap.OverviewMapControl());//缩略地图控件
map.enableScrollWheelZoom(true);
//开启鼠标滚轮缩放
//map.addControl(new BMap.MapTypeControl()); //地图类型控件,默认位于地图右上方。
map.setCurrentCity(&上海&);
//var marker = new BMap.Marker(point);
// 创建标注
//var json=[{name:&a&,age:12},{name:&b&,age:11},{name:&c&,age:13},{name:&d&,age:14}];
// 添加定位控件
/* var geolocationControl = new BMap.GeolocationControl();
geolocationControl.addEventListener(&locationSuccess&, function(e){
// 定位成功事件
var address = '';
address += e.addressComponent.
address += e.addressComponent.
address += e.addressComponent.
address += e.addressComponent.
address += e.addressComponent.streetN
alert(&当前定位地址为:& + address);
geolocationControl.addEventListener(&locationError&,function(e){
// 定位失败事件
alert(e.message);
map.addControl(geolocationControl); */
&pre name=&code& class=&html&&&span style=&white-space:pre&&
&/span&// 复杂的自定义覆盖物
function ComplexCustomOverlay(point, text, mouseoverText){
this._point =
this._text =
this._overText = mouseoverT
ComplexCustomOverlay.prototype = new BMap.Overlay();
//初始化ComplexCustomOverlay
ComplexCustomOverlay.prototype.initialize = function(map){
this._map =
document.createElement(&div&);
this._div =
div.style.position = &absolute&;
div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
div.style.background = &url(&%=basePath %&images/blue.gif) repeat-x 0 -33px&;
div.style.color = &white&;
div.style.height = &21px&;
div.style.padding = &2px&;
div.style.lineHeight = &18px&;
div.style.whiteSpace = &nowrap&;
div.style.MozUserSelect = &none&;
div.style.fontSize = &12px&;
var span =
document.createElement(&span&);
this._span =
div.appendChild(span);
span.appendChild(document.createTextNode(this._text));
var that =
//var arrow = this._arrow = document.createElement(&div&);
var arrow = document.createElement(&div&);
this._arrow =
arrow.style.background = &url(&%=basePath %&images/blue.gif) no-repeat -20px -100px&;
arrow.style.position = &absolute&;
arrow.style.width = &30px&;
arrow.style.height = &12px&;
arrow.style.top = &19px&;
arrow.style.left = &10px&;
arrow.style.overflow = &hidden&;
div.appendChild(arrow);
var leftBar
= document.createElement(&div&);
this._leftBar = leftB
leftBar.style.background = &url(&%=basePath %&images/blue.gif) no-repeat -12px -2px&;
leftBar.style.position = &absolute&;
leftBar.style.width = &11px&;
leftBar.style.height = &24px&;
leftBar.style.top = &0px&;
leftBar.style.left = &-10px&;
leftBar.style.overflow = &hidden&;
div.appendChild(leftBar);
var rightBar= document.createElement(&div&);
this._rightBar = rightB
rightBar.style.background = &url(&%=basePath %&images/blue.gif) no-repeat -22px -2px&;
rightBar.style.position = &absolute&;
rightBar.style.width = &11px&;
rightBar.style.height = &24px&;
rightBar.style.top = &0px&;
rightBar.style.right = &-10px&;
rightBar.style.overflow = &hidden&;
div.appendChild(rightBar);
div.onmouseover = function(){
this.style.background = &url(&%=basePath %&images/blue2.gif) repeat-x 0 -33px&;
this.getElementsByTagName(&span&)[0].innerHTML = that._overT
arrow.style.background = &url(&%=basePath %&images/blue2.gif) no-repeat -20px -100px&;
leftBar.style.background = &url(&%=basePath %&images/blue2.gif) no-repeat -12px -2px&;
rightBar.style.background = &url(&%=basePath %&images/blue2.gif) no-repeat -22px -2px&;
div.onmouseout = function(){
this.style.background = &url(&%=basePath %&images/blue.gif) repeat-x 0 -33px&;
this.getElementsByTagName(&span&)[0].innerHTML = that._
arrow.style.background = &url(&%=basePath %&images/blue.gif) no-repeat -20px -100px&;
leftBar.style.background = &url(&%=basePath %&images/blue.gif) no-repeat -12px -2px&;
rightBar.style.background = &url(&%=basePath %&images/blue.gif) no-repeat -22px -2px&;
map.getPanes().labelPane.appendChild(div);
//画ComplexCustomOverlay
ComplexCustomOverlay.prototype.draw = function(){
var map = this._
var pixel = map.pointToOverlayPixel(this._point);
this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + &px&;
this._div.style.top = pixel.y - 30 + &px&;
//添加监听事件
ComplexCustomOverlay.prototype.addEventListener = function(event,fun){
this._div['on'+event] =
&span style=&white-space:pre&&
&/span&//事件处理
var jsonObj = $(&#mapList&).val();
var json = strToJson(jsonObj);
for(var o in json){
var txt = json[o].
var pointx = json[o].
var pointy = json[o].
//var mkr = new BMap.Marker(new BMap.Point(json[o].jd,json[o].wd));
var mkr = new ComplexCustomOverlay(new BMap.Point(pointx, pointy), txt, txt);
map.addOverlay(mkr);
(function(){
var index =
/* mkr.addEventListener('touchstart',function(){
this.openInfoWindow(Window('我是'+json[index].name));
mkr.addEventListener('touchstart',function(){
//把所有的驾校分离出来
var Arr = json[index].flag.split(&|&);
//存放单个驾校
//驾校名称
var content = '';
content +=&&div class='map'&&;
content +=&&div class='img' style='background-image: url(&%=basePath%&images/&+json[index].pic+&.png)'&&;
content +=&&/div&&div class='login'&&;
for(var i=0;i&Arr.i++){
jxArr = Arr[i].split('_');
jxname = jxArr[0];
jxid = jxArr[1];
content +=&&span&&a href='&%=basePath%&user/user_findSchoolByID.action?jxid=&+jxid+&'&&+jxname+&&/a&&/span&&;
content +=&&/div&&p&基地地址:&+json[index].add+&&/p&&;
content +=&&/div&&;
var infoWindow = Window(content,{
title: &&h3 class='lt'&&+json[index].name+&&/h3&&, //标题
enableAutoPan : true, //自动平移
width: 300, //宽度
height: 160, //高度
enableMessage:false});
// 创建信息窗口对象
map.openInfoWindow(infoWindow, new BMap.Point(json[index].jd, json[index].wd));
// 打开信息窗口 */
mkr.addEventListener('click',function(){
//把所有的驾校分离出来
var Arr = json[index].flag.split(&|&);
//存放单个驾校
//驾校名称
var content = '';
content +=&&div class='map'&&;
content +=&&div class='img' style='background-image: url(&%=basePath%&images/&+json[index].pic+&.png)'&&;
content +=&&/div&&div class='login'&&;
for(var i=0;i&Arr.i++){
jxArr = Arr[i].split('_');
jxname = jxArr[0];
jxid = jxArr[1];
content +=&&span&&a href='&%=basePath%&user/user_findSchoolByID.action?jxid=&+jxid+&'&&+jxname+&&/a&&/span&&;
content +=&&/div&&p&基地地址:&+json[index].add+&&/p&&;
content +=&&/div&&;
var infoWindow = Window(content,{
title: &&h3 class='lt'&&+json[index].name+&&/h3&&, //标题
enableAutoPan : true, //自动平移
width: 300, //宽度
height: 160, //高度
enableMessage:false});
// 创建信息窗口对象
map.openInfoWindow(infoWindow, new BMap.Point(json[index].jd, json[index].wd));
// 打开信息窗口 */
function strToJson(str){
return JSON.parse(str);
&/pre&&pre&
另附code引用的图片
blue2.gif& & &&blue.gif
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:25829次
排名:千里之外
原创:15篇
(2)(1)(2)(6)(6)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'在 SegmentFault,解决技术问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
一线的工程师、著名开源项目的作者们,都在这里:
获取验证码
已有账号?
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
如下链接为百度地图API实例。
覆盖物示例-&点聚合。
我目前有差不多1000个点经纬度坐标需要渲染在地图上,
但效率非常慢,基本上500个点都要等十几秒以上。
请问如何提高效率?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
之前做过arcgis的。如果百度地图的效率是在是很慢的话,那么可以考虑根据比例尺和显示的地图返回来控制同时显示的点。
比方你在全图的时候,5000个点,在中国地图上其实没有什么概念的。只是一个范围的。当比例尺小于一定的时候,再把该地图范围内的所有点绘制出来。
尽量控制显示的个数。另外参考百度的建议:常见问题7、标注过多时,地图在IE中速度会下降,尤其是IE6。如何解决?回复:标注数据量请控制在150个以内,以保持高性能。标注数量在260以内,可以使用自定义覆盖物实现。标注数量大于300个,建议尝试marker聚合,或者数据抽希。Marker聚合:数据抽希:比如有10个marker,选择其中6个做为显示点。我的建议是,不要一次在地图上添加过多的marker,而是先把point存储在数据库里,当需要显示某个marker是,再addOverlay。
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:原文:摘要:很多API爱好者问我,为什么我点击的是marker,而map也会响应该事件呢?怎样才能判断,我点击的是标注,还是地图呢?下面一起来看看。
------------------------------------------------------------
先看看地图有哪些事件,查看官网,翻到:
大家看到这里的参数了麽?
其实,在点击物体时,通过这些参数就能够判断所点击的物体是什么了。
用marker举例:
先创建一个marker
//覆盖物Marker
var pmk = new BMap.Point(116..946583);
var mk = new BMap.Marker(pmk);
map.addOverlay(mk);
然后对地图添加点击事件,如果判断出来是覆盖物,就弹出相应的对话框。
map.addEventListener("click",function(e){
if(e.overlay){
alert('你点击的是覆盖物:'+e.overlay.toString());
alert('你点击的是地图');
添加一堆覆盖物,然后给每个覆盖物写点击事件。
比如我点击了多边形,就会弹出这个。
全部源代码:
&!DOCTYPE html&
&meta http-equiv="Content-Type" content="text/ charset=utf-8" /&
&title&点击事件&/title&
&script type="text/javascript" src="http://api./api?key=v=1.3"&&/script&
&!--百度地图容器--&
&div style="width:697height:550border:#ccc solid 1" id="dituContent"&&/div&
&script type="text/javascript"&
var map = new BMap.Map("dituContent");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);
map.enableScrollWheelZoom();
map.addEventListener("click",function(e){
if(e.overlay){
alert('你点击的是覆盖物:'+e.overlay.toString());
alert('你点击的是地图');
//覆盖物Marker
var pmk = new BMap.Point(116.249472,39.946583);
var mk = new BMap.Marker(pmk);
map.addOverlay(mk);
//覆盖物Label
var plb = new BMap.Point(116.408149,39.958087);
var lb = new BMap.Label('我是覆盖物Label',{point:plb});
map.addOverlay(lb);
//覆盖物Polyline
var pl = new BMap.Polyline([new BMap.Point(116.250047,39.919583),new BMap.Point(116.441494,39.919583)],{strokeWeight:10})
map.addOverlay(pl);
//覆盖物Polygon
var pg = new BMap.Polygon([new BMap.Point(116.248323,39.893016),new BMap.Point(116.440344,39.893016),new BMap.Point(116.440344,39.811036),new BMap.Point(116.248323,39.811036)]);
map.addOverlay(pg);
Views(...) Comments()}

我要回帖

更多关于 百度地图清除覆盖物 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信