layer 弹个地图

这篇博客介绍如何利用JavaScript库layer弹出地图选择窗口,结合百度地图API实现地图上的点选操作。用户可以搜索地点,点击地图上的点来获取坐标,并将坐标值回填到父页面的输入框中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是父页面的内容!!!!

<input type="text" placeholder="活动地点" id="risenemEventplace" onClick="dz()" class="layui-input"/>

<input type="hidden" id="risenemLng" name="risenemLng"/>

 <input type="hidden" id="risenemLat" name="risenemLat" />


//弹出地图
   function dz(){   
      layer.open({
           type: 2,
           title: '活动地点',
           shadeClose: true,
           shade: 0.8,
           anim:0,
           area: ['80%', '80%'],
           content: '/risen/newdj/page/eventmanage/eventMap.html', //iframe的url
           scrollbar: false
        });

  }

以下是地图页面的内容

<script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=2lkDOKqrgrUodHwrwvG1Mv6BPbf0Id8Y"></script>     

       <script src="./assets/js/jquery-1.8.3.min.js"></script>


<span style="width: 50px; font-size:15px;display: inline-block;">搜索:</span><input type="text" id="selectText" style="height: 30px;width: 250px;"/>

<div id="searchResultPanel" style="position:absolute;z-index:999;background-color:#fff;border:1px solid #C0C0C0;height:auto;left: 50px;"></div>


 <!--地图容器-->

 <div id="allmap"></div> 


        var map;
$(function () {
init();
});

    function init() {
var geocoder= new BMap.Geocoder();
    map = new BMap.Map("allmap");            // 创建Map实例
var point = new BMap.Point(102.846837,24.95393); // 创建点坐标
map.addControl(new BMap.MapTypeControl());
map.centerAndZoom(point,11);
map.setCurrentCity("云南省");   
map.enableScrollWheelZoom();                 //启用滚轮放大缩小
var myIcon = new BMap.Icon("/risen/dj/home/index/images/dq.png", new BMap.Size(30,32),
{
  // offset: new BMap.Size(0,0), // 指定定位位置  
          // imageOffset: new BMap.Size(140,40) // 设置图片偏移  
             anchor: new BMap.Size(10, 30),
infoWindowAnchor: new BMap.Size(10, 0)
         });
   //没有设置党组织坐标
map.addEventListener("click",function(e){
//alert(e.point.lng + "," + e.point.lat);
map.clearOverlays();
var marker = new BMap.Marker(new BMap.Point(e.point.lng,e.point.lat),{icon:myIcon}); // 创建点
marker.disableDragging();
map.addOverlay(marker);
var _iframe = window.parent;//获取父页面中的窗口对象(index1.html
var risenemLng =_iframe.document.getElementById("risenemLng").value=e.point.lng; 
var risenemLat =_iframe.document.getElementById("risenemLat").value=e.point.lat;

$('input[name="risenpoXindex"]').val(e.point.lng);
$('input[name="risenpoYindex"]').val(e.point.lat);

   geocoder.getLocation(e.point,function(rs){                   
            var risenemEventplace =_iframe.document.getElementById("risenemEventplace").value=rs.address;
        }); 


});
}
//百度地图API功能  检索  str
      //************地图位置搜索 str


var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
{"input" : "selectText"
,"location" : map
});

ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
var str = "";
var _value = e.fromitem.value;
var value = "";
if (e.fromitem.index > -1) {
value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
}    
str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

value = "";
if (e.toitem.index > -1) {
_value = e.toitem.value;
value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
}    
str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
$("searchResultPanel").innerHTML = str;
});

var myValue;
ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
var _value = e.item.value;
myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
$("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

setPlace();
});

function setPlace(){
map.clearOverlays();    //清除地图上所有覆盖物
var geocoder= new BMap.Geocoder();
function myFun(){
var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
map.centerAndZoom(pp, 17);
map.addOverlay(new BMap.Marker(pp));    //添加标注
var _iframe = window.parent;//获取父页面中的窗口对象(index1.html
var risenemLng =_iframe.document.getElementById("risenemLng").value=pp.lng;
    var risenemLat =_iframe.document.getElementById("risenemLat").value=pp.lat;

$('input[name="risenpoXindex"]').val(pp.lng);
$('input[name="risenpoYindex"]').val(pp.lat);
var risenemEventplace = $("#selectText").val();
    var risenemEventplace =_iframe.document.getElementById("risenemEventplace").value=risenemEventplace;
}
var local = new BMap.LocalSearch(map, { //智能搜索
  onSearchComplete: myFun
});
local.search(myValue);
}


//************地图位置搜索 end
//百度地图API功能  检索  end
//============== 地图坐标 END




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值