使用高德地图,点击位置定位,可搜索,带周边搜索点击可选,气泡事件

.container_main{position: relative;width: 800px ;height:511px ;margin-bottom: 200px;}
#result{margin: 0;padding: 0;z-index: 999;position: absolute;background-color: white;max-height: 100%;overflow-y: auto;top: 0;left: 0;width: 280px;}
.content-window-card{position: relative;box-shadow: none;bottom: 0;left: 0;width: auto;padding: 0;}
.content-window-card p{height: 2rem;}
.custom-info{border: solid 1px silver;}
div.info-top{position: relative;background: none repeat scroll 0 0 #F9F9F9;border-bottom: 1px solid #CCC;border-radius: 5px 5px 0 0;}
div.info-top div{display: inline-block;color: #333333;font-size: 14px;font-weight: bold;line-height: 31px;padding: 0 10px;}
div.info-top img{position: absolute;top: 10px;right: 10px;transition-duration: 0.25s;}
div.info-top img:hover{box-shadow: 0px 0px 5px #000;}
div.info-middle{font-size: 12px;padding: 10px 6px;line-height: 20px;}
div.info-bottom{height: 0px;width: 100%;clear: both;text-align: center;}
div.info-bottom img{position: relative;z-index: 104;}
div.info span{margin-left: 5px;font-size: 11px;}
.info-middle img{float: left;margin-right: 6px;}
<div class="container_main" >
    div id="container"></div>
    <div id="result"></div>
    <div id="myPageTop">
        <table>
          <tr>
            <td>
              <label>请输入关键字:</label>
            </td>
          </tr>
          <tr>
             <td>
              <input id="tipinput" name="resourceAddress" />
             </td>
          </tr>
        </table>
     </div>
     <div class="info">
       点击地图上的点标记,打开所添加的自定义信息窗体
     </div>
</div>
<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css" />
<script
    src="https://webapi.amap.com/maps?v=1.4.15&key=填入你自己的key&plugin=AMap.Autocomplete,AMap.PlaceSearch,AMap.Geocoder"></script>
<script>

    var lon = $('#longitude').val()
    var lat = $('#latitude').val();
    var map;
    if (lon > 0) {
        //初始化到已有地点
        map = new AMap.Map('container', {
            zoom: 20,
            resizeEnable: true,
            center: [lon, lat],
            viewMode: '3D'
        });
    } else {
        //初始化到默认地点
        map = new AMap.Map('container', {
            zoom: 12,
            resizeEnable: true,
            viewMode: '3D'
        });
    }

    //此插件用来显示当前位置,可在引入API时一同引入
    map.plugin('AMap.Geolocation', function () {
        var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,
            timeout: 10000,
            buttonOffset: new AMap.Pixel(10, 20),
            zoomToAccuracy: true,
            buttonPosition: 'RB'
        });
        map.center = geolocation;
        map.addControl(geolocation);//地图控件右下角显示当前位置
    });

    var geocoder, marker;
    if (!marker) {
        marker = new AMap.Marker();
        map.add(marker);

        //鼠标点击marker弹出自定义的信息窗体
        AMap.event.addListener(marker, 'click', function () {
            // console.log(marker.getPosition())
            // infoWindow.open(map, marker.getPosition());
        });
    }


    //实例化信息窗体
    var title = '方恒假日酒店<span style="font-size:11px;color:#F00;">价格:318</span>',
        content = [];
    content.push("<img src='http://tpc.googlesyndication.com/simgad/5843493769827749134'>地址:北京市朝阳区阜通东大街6号院3号楼东北8.3公里");
    content.push("电话:010-64733333");
    content.push("<a href='https://ditu.amap.com/detail/B000A8URXB?citycode=110105'>详细信息</a>");
    var infoWindow = new AMap.InfoWindow({
        isCustom: true,  //使用自定义窗体
        content: createInfoWindow(title, content.join("<br/>")),
        offset: new AMap.Pixel(16, -45)
    });

    //构建自定义信息窗体
    function createInfoWindow(title, content) {
        var info = document.createElement("div");
        info.className = "custom-info input-card content-window-card";

        //可以通过下面的方式修改自定义窗体的宽高
        //info.style.width = "400px";
        // 定义顶部标题
        var top = document.createElement("div");
        var titleD = document.createElement("div");
        var closeX = document.createElement("img");
        top.className = "info-top";
        titleD.innerHTML = title;
        closeX.src = "https://webapi.amap.com/images/close2.gif";
        closeX.onclick = closeInfoWindow;

        top.appendChild(titleD);
        top.appendChild(closeX);
        info.appendChild(top);

        // 定义中部内容
        var middle = document.createElement("div");
        middle.className = "info-middle";
        middle.style.backgroundColor = 'white';
        middle.innerHTML = content;
        info.appendChild(middle);

        // 定义底部内容
        var bottom = document.createElement("div");
        bottom.className = "info-bottom";
        bottom.style.position = 'relative';
        bottom.style.top = '0px';
        bottom.style.margin = '0 auto';
        var sharp = document.createElement("img");
        sharp.src = "https://webapi.amap.com/images/sharp.png";
        bottom.appendChild(sharp);
        info.appendChild(bottom);
        return info;
    }

    //关闭信息窗体
    function closeInfoWindow() {
        map.clearInfoWindow();
    }

    var lnglat = map.getCenter();
    var options = [];
    regeoCode(); //如果放在模态框中,弹窗显示后再调用,不然有时候找不到dom加载会报错
    //地图点击时,获取点击地经纬度
    map.on('click', function (e) {
        lnglat = e.lnglat;
        placeSearch.clear(); //清除左侧搜索目录在地图的重复标记,
        regeoCode(); //标记一个
    });

    $("#result").on("click", ".pageLink", function (params) {
        if (!$(this).hasClass('current')) {
            placeSearch.clear();
        }
    })

    function regeoCode() {
        if (!geocoder) {
            geocoder = new AMap.Geocoder();
        }
        if (!marker) {
            marker = new AMap.Marker();
            map.add(marker);
        }
        marker.setPosition(lnglat);//设置标记的位置
        geocoder.getAddress(lnglat, function (status, result) {
            if (status === 'complete' && result.regeocode) {
                var address = result.regeocode.formattedAddress;
                $("input[name=resourceAddress]").val(address);
                myMapViewLocation();
            }
        });
        marker.setMap(map);//在地图上显示一个标记
    }

    //输入提示
    var autoOptions = {
        input: "tipinput"
    };
    var auto = new AMap.Autocomplete(autoOptions);
    var placeSearch = new AMap.PlaceSearch({
        map: map
    });  //构造地点查询类
    AMap.event.addListener(auto, "select", select);//注册监听,当选中某条记录时会触发
    function select(e) {

        // placeSearch.setCity(e.poi.adcode);
        // placeSearch.search(e.poi.name);  //关键字查询查询

        myMapViewLocation();
    }

    //查找地图左侧可选定位目录
    function myMapViewLocation() {

        AMap.service(["AMap.PlaceSearch"], function () {
            console.log(9998888)
            placeSearch = new AMap.PlaceSearch({ //构造地点查询类
                pageSize: 10, // 单页显示结果条数
                pageIndex: 1, // 页码
                type: "汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施",
                // city: "010", // 兴趣点城市
                // citylimit: true,  //是否强制限制在设置的城市内搜索
                map: map, // 展现结果的地图实例
                renderStyle: "newpc",
                panel: "result", // 结果列表将在此容器中进行展示。
                autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
            });

            placeSearch.search($('input[name=resourceAddress]').val(), function (status, result) {
                console.log(status, result);

            });
        });
        AMap.event.addListener(placeSearch, "markerClick", function (e) {

            infoWindow.open(map, e.data.location);

        });
        AMap.event.addListener(placeSearch, "listElementClick", function (e) {

            // map.clearInfoWindow(); //自定义关闭弹窗
            infoWindow.open(map, e.data.location);
        });



    }


</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值