openlayers怎么加载百度地图为底图?


请问openlayers怎么加载百度地图作为底图?
搜到了一些帖子但都已经打不开了.

openlayers

花生马里奥 10 years, 5 months ago

可以参考一个解决方案: https://github.com/shitao1988/baidu/

  1. 首先在页面上引入jquery、OpenLayers和百度云地图Js库;

    <script type="text/javascript" src="./js/base/jQuery.js"></script>
    <script type="text/javascript" src="./js/openlayer/OpenLayers.js"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
    
  2. 一张Html页面准备好map容器、准备好省、市两级下拉联动条;

  3. 创建地图:

    var mapOptions = {   
         displayProjection : WGS84,  
         units: "degrees",  
         numZoomLevels:15,  
         allOverlays: true,  
         controls: [],  
         projection: WGS84,  
     maxExtent: new OpenLayers.Bounds(-180.0,-90.20209041233414,180.4041808246683,90.0),  
         };  
    map = new OpenLayers.Map('map', mapOptions);
    
  4. 叠加

    var triangleCoords = [];    
    /** 
     * 根据城市名描画边界范围 
     * @author:wangyq 
     * @parameters:name,区划名,比如广东省或者东莞市 
    */  
    function makeBoundsByRegion(name){  
    //下面这句是调用百度API  
    var bdary = new BMap.Boundary();   
    var polygonArray = [];  
    triangleCoords.length=0;  
    bdary.get(name, function(rs) {  
         points = rs; //获取行政区域    
         var rslength = rs.boundaries.length;     
         var components = new Array();  
         var polygon = null;  
         for (i = 0; i < rslength; i++) {    
             var temp = rs.boundaries[i];    
             //一个temp代表一个polygon,部分区域譬如广东省边界范围中包含多个polygon,需要递归画出来  
             var latLngs = temp.split(";");    
             triangleCoords.length = 0;  
             for (j = 1; j < latLngs.length - 1; j++) {   
                 var postion = latLngs[j].indexOf(",");    
                 var lng = parseFloat(latLngs[j].substr(0, postion));//经度    
                 var lat = parseFloat(latLngs[j].substr(postion + 1));//纬度    
                 triangleCoords.push(new OpenLayers.Geometry.Point(lng,lat));    
             }  
             var k = triangleCoords.length;  
             var line = new OpenLayers.Geometry.LinearRing(triangleCoords);  
             polygon = new OpenLayers.Geometry.Polygon([line]);  
             polygonArray.push(polygon);  
         }  
         var MultiPolygon = new OpenLayers.Geometry.MultiPolygon(polygonArray);  
         var polygonBounds = new OpenLayers.Feature.Vector(MultiPolygon);  
         //设置layersname为行政边界的图层的样式  
         if(map.getLayersByName("行政边界").length==0){  
             var sketchStyle = new OpenLayers.Style(null, {  
                 rules: [  
                         new OpenLayers.Rule({  
                             symbolizer: {  
                                 "Polygon": {  
                                     strokeWidth: 3.5,  
                                     strokeOpacity: 1,  
                                     strokeColor: "#FF3366",  
                                     fillColor: "#ff0000",  
                                     fillOpacity: 0  
                                 }  
                             }  
                         })  
                         ]  
             });  
             var myStyle = new OpenLayers.StyleMap({   
                 "default":sketchStyle  
             });  
             var bouderLayer = new OpenLayers.Layer.Vector("行政边界",{  
                 styleMap: myStyle,  
                 projection:WGS84  
             });  
             bouderLayer.addFeatures(polygonBounds);  
             map.addLayer(bouderLayer);  
    
         }else{  
             map.getLayersByName("行政边界")[0].removeAllFeatures();  
             map.getLayersByName("行政边界")[0].addFeatures(polygonBounds);  
         }  
         var centroid = new OpenLayers.LonLat.fromString(MultiPolygon.getCentroid().x+","+MultiPolygon.getCentroid().y);  
         map.moveTo(centroid);  
    });  
    }
    
老夫石更啦 answered 10 years, 3 months ago

Your Answer