百度地圖web api使用案例

效果以下:javascript

1.獲取位置的經緯度:css

如座標:114.110033,22.541098html

獲取經緯度:java

http://api.map.baidu.com/lbsapi/getpoint/index.htmljson

 

2.html代碼:api

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="百度地圖,百度地圖API,百度地圖自定義工具,百度地圖所見即所得工具" />
<meta name="description" content="百度地圖API自定義地圖,幫助用戶在可視化操做下生成百度地圖" />
<title>公司地圖</title>

<!--引用百度地圖API-->
<style type="text/css">
    html,body
    {
        margin:0;
        padding:0;
    }
    .iw_poi_title 
    {
        color:#CC5522;
        font-size:14px;
        font-weight:bold;
        overflow:hidden;
        padding-right:13px;
        white-space:nowrap
    }
    .iw_poi_content 
    {
        font:12px arial,sans-serif;
        overflow:visible;
        padding-top:4px;
        white-space:-moz-pre-wrap;
        word-wrap:break-word
    }
</style>

<script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script>

</head>

<body>
  <!--百度地圖容器-->
  
  <div style="width:697px;height:550px;border:#ccc solid 1px;" id="dituContent"></div>
  
</body>

    <script type="text/javascript">
        //建立和初始化地圖函數:
        function initMap(){
            createMap();//建立地圖
            setMapEvent();//設置地圖事件
            addMapControl();//向地圖添加控件
            addMarker();//向地圖中添加marker
        }
        
        //建立地圖函數:
        function createMap(){
            var map = new BMap.Map("dituContent");//在百度地圖容器中建立一個地圖
            var point = new BMap.Point(113.871998,22.657299);//定義一箇中心點座標
            map.centerAndZoom(point,18);//設定地圖的中心點和座標並將地圖顯示在地圖容器中
            window.map = map;//將map變量存儲在全局
        }
        
        //地圖事件設置函數:
        function setMapEvent(){
            map.enableDragging();//啓用地圖拖拽事件,默認啓用(可不寫)
            map.enableScrollWheelZoom();//啓用地圖滾輪放大縮小
            map.enableDoubleClickZoom();//啓用鼠標雙擊放大,默認啓用(可不寫)
            map.enableKeyboard();//啓用鍵盤上下左右鍵移動地圖
        }
        
        //地圖控件添加函數:
        function addMapControl(){
            //向地圖中添加縮放控件
        var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_SMALL});
        map.addControl(ctrl_nav);
                    }
        
        //標註點數組
        var markerArr = [{title:"定位位置名稱",content:"個人備註",point:"113.871566|22.657115",isOpen:0,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}}
             ];
        //建立marker
        function addMarker(){
            for(var i=0;i<markerArr.length;i++){
                var json = markerArr[i];
                var p0 = json.point.split("|")[0];
                var p1 = json.point.split("|")[1];
                var point = new BMap.Point(p0,p1);
                var iconImg = createIcon(json.icon);
                var marker = new BMap.Marker(point,{icon:iconImg});
                var iw = createInfoWindow(i);
                var label = new BMap.Label(json.title,{"offset":new BMap.Size(json.icon.lb-json.icon.x+10,-20)});
                marker.setLabel(label);
                map.addOverlay(marker);
                label.setStyle({
                            borderColor:"#808080",
                            color:"#333",
                            cursor:"pointer"
                });
                
                (function(){
                    var index = i;
                    var _iw = createInfoWindow(i);
                    var _marker = marker;
                    _marker.addEventListener("click",function(){
                        this.openInfoWindow(_iw);
                    });
                    _iw.addEventListener("open",function(){
                        _marker.getLabel().hide();
                    })
                    _iw.addEventListener("close",function(){
                        _marker.getLabel().show();
                    })
                    label.addEventListener("click",function(){
                        _marker.openInfoWindow(_iw);
                    })
                    if(!!json.isOpen){
                        label.hide();
                        _marker.openInfoWindow(_iw);
                    }
                })()
            }
        }
        //建立InfoWindow
        function createInfoWindow(i){
            var json = markerArr[i];
            var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>"+json.content+"</div>");
            return iw;
        }
        //建立一個Icon
        function createIcon(json){
            var icon = new BMap.Icon("http://app.baidu.com/map/images/us_mk_icon.png", new BMap.Size(json.w,json.h),{imageOffset: new BMap.Size(-json.l,-json.t),infoWindowOffset:new BMap.Size(json.lb+5,1),offset:new BMap.Size(json.x,json.h)})
            return icon;
        }
        
        initMap();//建立和初始化地圖
    </script>
</html>
相關文章
相關標籤/搜索