百度地图 搜索建议功能
由于suggestion返回的是地址数据,并没有point经纬度的信息。我们需要自己在回调函数,或者其他地方通过地址解析来打点。
// 百度地图API功能 var map = new BMap.Map("allmap"); map.enableScrollWheelZoom(true); var point = new BMap.Point(116.331398,39.897445); var marker = new BMap.Marker(point); map.addOverlay(marker); map.centerAndZoom(point, 15); // 添加带有定位的导航控件 var navigationControl = new BMap.NavigationControl({ // 靠左上角位置 anchor: BMAP_ANCHOR_TOP_LEFT, // LARGE类型 type: BMAP_NAVIGATION_CONTROL_LARGE, // 启用显示定位 enableGeolocation: true }); map.addControl(navigationControl); // 添加定位控件 var geolocationControl = new BMap.GeolocationControl(); geolocationControl.addEventListener("locationSuccess", function(e){ // 定位成功事件 point = map.getCenter(); geoCoder(); }); geolocationControl.addEventListener("locationError",function(e){ // 定位失败事件 alert(e.message); }); map.addControl(geolocationControl); //建立一个自动完成的对象 var ac = new BMap.Autocomplete({"input" : "mapSearch","location" : map}); //todo ..... function setPlace() { function myFun() { point = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果 $('#mapSearch').attr('data-x', point.lng); $('#mapSearch').attr('data-y', point.lat); map.centerAndZoom(point, 15); geoCoder(); } var local = new BMap.LocalSearch(map, { //智能搜索 onSearchComplete: myFun }); local.search(myValue); } function makeList(pois, key) { $searchAddressList.empty(); for (var i = 0; i < pois.length; i++) { var poi = pois[i]; var name = poi.name; var addr = poi.addr; if (!key || (key && key.length > 0 && poi.name.indexOf(key) > -1)) { $searchAddressList.append('<li data-x="' + poi.point.x + '" data-y="' + poi.point.y + '"><p class="place-name">' + poi.name + '</p><p class="details">' + addr + '</p></li>'); } } if ($searchAddressList.children().length == 2) { $searchAddressList.children().last().remove(); } var $lis = $searchAddressList.children(); if ($lis.length == 2) { var name1 = $lis.eq(0).find('.place-name'); var name2 = $lis.eq(1).find('.place-name'); if (name1 == name2) { $searchAddressList.children().last().remove(); } } } function geoCoder() { marker.setPosition(point); var send = { ak: "{:C('BAIDU_MAP_AK')}", location: point.lat + ',' + point.lng, output: 'json', pois: 1 }; $.ajax({ url: 'http://api.map.baidu.com/geocoder/v2/', dataType: 'jsonp', data: send, success: function (data) { if (data.status == 0 && data.result) { var pois = data.result.pois; addressList = pois; makeList(pois); } } }) }
相关推荐
zrtlin 2020-11-09
xuebingnan 2020-11-05
wikiwater 2020-10-27
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25
Aveiox 2020-06-23
dragonzht 2020-06-17