美文网首页
百度地图自定义inforWindows 移动端事件处理

百度地图自定义inforWindows 移动端事件处理

作者: loycoder | 来源:发表于2018-06-22 17:49 被阅读195次

在做百度地图开发时,有一个需求 需要自定义 热点弹窗, 当时使用的时 官方开源库
自定义信息窗口

PC端正常,不过在移动端出现问点击marker 出现弹窗后,弹窗内部的元素事件全部失效,不可触发, 会被地图默认的拖拽事件所拦截掉。

解决方案:
使用百度提供的开源方案事件包装器

example:


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, minimal-ui">
    <meta name="screen-orientation" content="portrait"/>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="full-screen" content="yes">
    <meta name="x5-fullscreen" content="true">

    <style type="text/css">
        .infoBoxContent{font-size: 12px;} .infoBoxContent .title{height: 42px; width: 272px;} .infoBoxContent .title strong{font-size: 14px; line-height: 42px; padding: 0 10px 0 5px;} .infoBoxContent .title .price{color: #FFFF00;} .infoBoxContent .list{width: 268px; border: solid 1px #4FA5FC; border-top: none; background: #fff; height: 260px;} .infoBoxContent .list ul{margin: 0; padding: 5px; list-style: none;} .infoBoxContent .list ul li{float: left; width: 255px; border-bottom: solid 1px #4FA5FC; padding: 2px 0;} .infoBoxContent .list ul .last{border: none;} .infoBoxContent .list ul img{width: 53px; height: 42px; margin-right: 5px;} .infoBoxContent .list ul p{padding: 0; margin: 0;} .infoBoxContent .left{float: left;} .infoBoxContent .rmb{float: right; color: #EB6100; font-size: 14px; font-weight: bold;} .infoBoxContent a{color: #0041D9; text-decoration: none;}
    </style>

    <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=Yil05mmmidi6TA4INkOwPr4LBxx9AH35"></script>

    <script type="text/javascript" src="./InfoBox_min.js"></script>

    <title>Creating and Using an InfoBox</title>
</head>
<body>

<script type="text/javascript" src="http://api.map.baidu.com/library/EventWrapper/1.2/src/EventWrapper.js"></script>

<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script>

<div id="map_canvas" style="width: 600px; height: 500px"></div>
<script type="text/javascript">
    var map = new BMap.Map('map_canvas', {enableMapClick: false});
    var poi = new BMap.Point(116.307852, 40.057031);
    map.centerAndZoom(poi, 16);
    var html = ["<div id='test' class='infoBoxContent'>",
        "<div class='list'>"
        , "<li class='last'><a class='go' href='baidu.com' class='left'>点击跳转</a> <a class='close'>关闭弹窗</a>" +
        "</li>"
        , "</ul></div>"
        , "</div>"];
    var infoBox = new BMapLib.InfoBox(map, html.join(""), {
        boxStyle: {
            width: "270px"
            , height: "300px"
        }
        , closeIconMargin: "1px 1px 0 0"
        , enableAutoPan: true
        , align: INFOBOX_AT_TOP
    });

    console.log(infoBox);

    var marker = new BMap.Marker(poi);
    map.addOverlay(marker);
    marker.enableDragging();

    marker.addEventListener('click', function (e) {
        infoBox.open(marker);

      //防止dom元素未获取到,直接去绑定事件,所以延迟绑定
        setTimeout(function () {
          var dom = infoBox._map.De[0].V

            //移动端需要用touch绑定
            BMapLib.EventWrapper.addDomListener($(dom).find('a.go')[0], "touchend", function (e) {
                console.log('链接点击');
            });

            BMapLib.EventWrapper.addDomListener($(dom).find('a.close')[0], "touchend", function (e) {
                //事件处理
                infoBox.close();
            });

        }, 1)

    })

</script>
</body>

</html>

相关文章

  • 百度地图自定义inforWindows 移动端事件处理

    在做百度地图开发时,有一个需求 需要自定义 热点弹窗, 当时使用的时 官方开源库自定义信息窗口 PC端正常,不过在...

  • 移动端百度地图无法触发自定义覆盖物事件的问题

    手上有一移动端项目 要使用百度地图,自定义覆盖物渲染正常,添加了自定义事件之后,PC端可以正常触发事件,然而在移动...

  • 移动端调用手机电话功能以及百度地图

    移动端调用手机电话功能 移动端调用百度地图 html js

  • iOS Apps

    信息 微信 //移动端第一 App; 手机百度 //移动端搜索入口,百度浏览器; 百度地图 //除 Google ...

  • 地图瓦片制作

    百度地图切图工具只能切<=18级别的瓦片,但是百度移动端地图已经到21级了,所以本文主要分析地图瓦片的制作流程,以...

  • 移动端事件

    移动端事件 触屏事件 移动端事件要比PC端要简单的多,移动端主要就有ontouchstart ontouchend...

  • input输入框键盘弹起延时问题

    一、移动端click点击事件300ms延迟介绍 在正常情况下,如果不进行特殊处理,移动端在触发点击事件时,会有30...

  • 移动端知识概括

    移动端地图 这两个文件是用来做移动端地图 移动端meta标签   meta标签常用于定义页面的说明,关键字,最后修...

  • 移动端

    移动端开发和 PC 端开发有哪些区别 移动端 考虑手机兼容性 使用触屏事件 布局自适应rem 动画处理CSS3 移...

  • 移动端 FastClick

    处理移动端 click 事件 300 毫秒延迟, 由 FT Labs 开发,Github 项目地址:https:/...

网友评论

      本文标题:百度地图自定义inforWindows 移动端事件处理

      本文链接:https://www.haomeiwen.com/subject/qkxlyftx.html