首页 > 其他分享 >百度地图api接口poi检索示例----并在信息框显示经纬度

百度地图api接口poi检索示例----并在信息框显示经纬度

时间:2023-08-08 22:33:30浏览次数:42  
标签:示例 ---- push api BMap poi var new marker



api官网

http://developer.baidu.com/map/jshome.htm


例子代码(保存为静态页面html可见效果如下):


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=42b8ececa9cd6fe72ae4cddd77c0da5d"></script>
<title>根据关键字本地搜索</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">

// 百度地图API功能
var map = new BMap.Map("allmap");            // 创建Map实例
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
window.openInfoWinFuns = null;
var options = {
  onSearchComplete: function(results){
    // 判断状态是否正确
    if (local.getStatus() == BMAP_STATUS_SUCCESS){
        var s = [];
        s.push('<div style="font-family: arial,sans-serif; border: 1px solid rgb(153, 153, 153); font-size: 12px;">');
        s.push('<div style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">');
        s.push('<ol style="list-style: none outside none; padding: 0pt; margin: 0pt;">');
        openInfoWinFuns = [];
        for (var i = 0; i < results.getCurrentNumPois(); i ++){
            var marker = addMarker(results.getPoi(i).point,i);
            var openInfoWinFun = addInfoWindow(marker,results.getPoi(i),i);
            openInfoWinFuns.push(openInfoWinFun);
            // 默认打开第一标注的信息窗口
            var selected = "";
            if(i == 0){
                selected = "background-color:#f0f0f0;";
                openInfoWinFun();
            }
 
        }
     
    }
else
{
    	$("#allmap").empty();
       $("#allmap").append("检索没有结果,原因: " + result);
}

  }
};

// 添加标注
function addMarker(point, index){
  var myIcon = new BMap.Icon("http://api.map.baidu.com/img/markers.png", new BMap.Size(23, 25), {
    offset: new BMap.Size(10, 25),
    imageOffset: new BMap.Size(0, 0 - index * 25)
  });
  var marker = new BMap.Marker(point, {icon: myIcon});
  map.addOverlay(marker);
  return marker;
}
// 添加信息窗口
function addInfoWindow(marker,poi,index){
    var maxLen = 10;
    var name = null;
    if(poi.type == BMAP_POI_TYPE_NORMAL){
        name = "地址:  "
    }else if(poi.type == BMAP_POI_TYPE_BUSSTOP){
        name = "公交:  "
    }else if(poi.type == BMAP_POI_TYPE_SUBSTOP){
        name = "地铁:  "
    }
    // infowindow的标题
    var infoWindowTitle = '<div style="font-weight:bold;color:#CE5521;font-size:14px">'+poi.title+'</div>';
    // infowindow的显示信息
    var infoWindowHtml = [];
    infoWindowHtml.push('<table cellspacing="0" style="table-layout:fixed;width:100%;font:12px arial,simsun,sans-serif"><tbody>');
    infoWindowHtml.push('<tr>');
    infoWindowHtml.push('<td style="vertical-align:top;line-height:16px;width:38px;white-space:nowrap;word-break:keep-all">' + name + '</td>');
    infoWindowHtml.push('<td style="vertical-align:top;line-height:16px">' + poi.address +  poi.point.lng+','+poi.point.lat+' </td>');
    infoWindowHtml.push('</tr>');
    infoWindowHtml.push('</tbody></table>');
    var infoWindow = new BMap.InfoWindow(infoWindowHtml.join(""),{title:infoWindowTitle,width:200}); 
    var openInfoWinFun = function(){
        marker.openInfoWindow(infoWindow);
        for(var cnt = 0; cnt < maxLen; cnt++){
            if(!document.getElementById("list" + cnt)){continue;}
            if(cnt == index){
                document.getElementById("list" + cnt).style.backgroundColor = "#f0f0f0";
            }else{
                document.getElementById("list" + cnt).style.backgroundColor = "#fff";
            }
        }
    }
    marker.addEventListener("click", openInfoWinFun);
    return openInfoWinFun;
}
var local = new BMap.LocalSearch("成都", options);
local.search("宽窄巷子");
</script>




百度地图api接口poi检索示例----并在信息框显示经纬度_实例



标签:示例,----,push,api,BMap,poi,var,new,marker
From: https://blog.51cto.com/u_16218512/7013104

相关文章

  • Sql server还原失败(数据库正在使用,无法获得对数据库的独占访问权)
    一.Sqlserver还原失败(数据库正在使用,无法获得对数据库的独占访问权)本次测试使用数据库实例SqlServer2008r2版错误详细:标题:MicrosoftSQLServerManagementStudio------------------------------还原数据库“Mvc_HNHZ”时失败。(Microsoft.SqlServer.Management.Rel......
  • soso地图api接口poi检索示例----并在信息框显示经纬度
    api官网:http://api.map.soso.com/doc_v2/example.html?sample-search-simple#7map示例代码如下(保存为html打开可见效果如下图):<!DOCTYPEhtml><html><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><title>......
  • ASP.NET----jQuery插件flot绘画图表
    flot是一个基于jQuery的图表绘制插件效果挺好看的:参考资料:http://code.google.com/p/flot/ 谷歌官网http://www.flotcharts.org/flot/examples/categories/index.html 例子(可右键查看源文件看看代码)要用到的js文件以及中文api文档可以在这里下载(没分的M我,给你发)基本语......
  • 中国联通“双门户”全栈信创割接成功
    【筑牢安全防线】中国联通“双门户”全栈信创割接成功联通软件研究院 联通软件研究院  2023-06-3019:26 发表于北京收录于合集#赋能41个【筑牢安全防线】中国联通“双门户”全栈信创割接成功为落实国务院“十四五”数字经济发展规划“推广使用安全可靠的信息产品、服务和解决......
  • 高德地图api接口poi检索示例----并在信息框显示经纬度
    api官网链接:http://api.amap.com/Javascript/example#http://api.amap.com/Javascript/plugin_detail/id/0代码示例如下(保存为html打开可见效果如下图):<!DOCTYPEhtml><html><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8&qu......
  • mysql中的空间地理字段
    第19章:MySQL中的空间扩展目录19.1.前言19.2.OpenGIS几何模型19.2.1.Geometry类的层次19.2.2.类Geometry19.2.3.类Point19.2.4.类Curve19.2.5.类LineString19.2.6.类Surface19.2.7.类Polygon19.2.8.类GeometryCollection19.2.9.类MultiPoint19......
  • mysql配置my.cnf包含innodb配置详解
    1.#以下选项会被MySQL客户端应用读取。2.#注意只有MySQL附带的客户端应用程序保证可以读取这段内容。3.#如果你想你自己的MySQL应用程序获取这些值。4.#需要在MySQL客户端库初始化的时候指定这些选项。5.6.#7.[client]8.#password=[your_passwo......
  • 扶植版做出拓展版950通栏轮播效果无缝连接
       一、左侧栏和右侧栏各添加一个自定义区   二、将如下代码分别复制到两个自定义区的源码编辑区内:   1、右侧自定义区代码<DIVclass="slider-promoJ_SliderJ_TWidgettb-slide"style="RIGHT:200px;WIDTH:950px;HEIGHT:450px"data-widget-type="Slide"da......
  • mysql索引
    1,索引什么是索引?索引------搜索---引导------引导着我去搜索到某个记录的定义:没有索引的情况下,我们查询一条记录,需要从第一条记录一直往下查询,知道查找到我们需要的记录,如果记录数特别大的话,相当于大海捞针,速度特别慢索引:建一个索引-----指向的是数据的位置-----反映到新华字......
  • XAML格式化工具:XAML Styler
    XAML格式化的意义在开发WPF应用过程中,编写XAML时需要手动去缩进或者换行,随着时间的推移或者参与开发的人增多,XAML文件内容的格式会越来越乱。要么属性全都写在一行,内容太宽一屏无法完整展现;要么属性单独占一行,难以直观的看清结构;另外xaml元素的属性无序,重要属性查找困难,手动维护......