Skip to content

百度地图本地版。😃

  • 抓取百度地图相关静态文件,修改相关代码,绕过相关权限
  • 爬虫百度地图瓦片和POI

前端代码

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <style type="text/css">
    html,body,#container{
        width: 100%;
        height: 100%;
        margin:0;
        padding:0;
        overflow: hidden;
        font-family: "Microsoft YaHei", Aria, sans-serif;
    }
    </style>
    <link   rel="stylesheet" type="text/css" href="css/bmap.css"/>
    <title>baiduMapDemo</title>
</head>
<body>
    <div id="container"></div>
</body> 
<script src="v2.0.min.js"></script>
<script>

var log = console.info.bind(console)

var GLOBAL = {
    second: 1000,
    minute: 60000,
    hour: 3600000,
    day: 86400000,
    week: 604800000,
    comma: ",",
    centerLongitude: 123.38,
    centerLatitude: 41.8,
    defaultZoom: 10,
    minZoom: 8,
    maxZoom: 17,
    bondMinLongitude: 118.53,
    bondMinLatitude: 38.43, 
    bondMaxLongitude: 125.46,
    bondMaxLatitude: 43.26, 
    outputPath: CONTEXTPATH + "/map/tiles/",
    pictureFormat: ".png",
    map: '',
    points: [],
    markPoints: []
}

var TIMER = {
    second: function(s) {
        return GLOBAL.second * s
    },
    minute: function(m) {
        return GLOBAL.minute * m
    },
    hour: function(h) {
        return GLOBAL.hour * h
    },
    day: function(d) {
        return GLOBAL.day * d
    }
}

var utils = {
    isEmpty: function(str) {
        if (str == "" || str == null || str == undefined) return true
        return false
    }
}

var getPoints = function(callback) {
    $.ajax({
        url: CONTEXTPATH + "/",
        type: "POST",
        data: {},
        async: false,
        success: function(data) { callback(data) },
        error: function() {}
    })
}

var dataBindPoints = function() {
    getPoints(function(data) {
        if (data.status) {
            $.each(data.data.result, function(index, element) {
                if (!utils.isEmpty(element.jd) && !utils.isEmpty(element.wd)) {
                    var pointStr = element.jd + GLOBAL.comma + element.wd
                    GLOBAL.markPoints.push(pointStr)
                }
            })
        }
    })
}

var initTileLayer = function(outputPath, pictureFormat) {
    var tileLayer = new BMap.TileLayer();
    tileLayer.getTilesUrl = function(tileCoord, zoom) {
        var x = tileCoord.x,
            y = tileCoord.y,
            url = outputPath + zoom + '/' + x + '/' + y + pictureFormat
        return url
    }
    var tileMapType = new BMap.MapType('tileMapType', tileLayer)
    GLOBAL.map = new BMap.Map("container", {
        mapType: tileMapType
    });
}

var initMap = function() {
    var point = new BMap.Point(GLOBAL.centerLongitude, GLOBAL.centerLatitude)
    GLOBAL.map.centerAndZoom(point, GLOBAL.defaultZoom)      
    GLOBAL.map.setMinZoom(GLOBAL.minZoom)
    GLOBAL.map.setMaxZoom(GLOBAL.maxZoom)
    var controlArea = new BMap.Bounds(new BMap.Point(GLOBAL.bondMinLongitude, GLOBAL.bondMinLatitude), new BMap.Point(GLOBAL.bondMaxLongitude, GLOBAL.bondMaxLatitude))
    try {
        BMapLib.AreaRestriction.setBounds(GLOBAL.map, controlArea)
    } catch (e) {alert(e)}
}

var initPoints = function() {
    var point = []
    var marker = []
    for (var i = 0; i < GLOBAL.markPoints.length; i++) {
        var arr = GLOBAL.markPoints[i].split(CODE.comma)
        var p0 = arr[0]
        var p1 = arr[1]
        point[i] = new window.BMap.Point(p0, p1)
        marker[i] = new window.BMap.Marker(point[i])
        GLOBAL.map.addOverlay(marker[i])
        GLOBAL.map.setCenter(GLOBAL.markPoints[i])
        var content = CODE.pretext + GLOBAL.markPoints[i].split(CODE.comma)[2]
        GLOBAL.points.push(point[i])
        marker[i].setAnimation(BMAP_ANIMATION_BOUNCE)
    }
    if (document.createElement('canvas').getContext) {
        var options = {
            size: 8,
            shape: 1,
            color: "#FF0000"
        };
        var pointCollection = new BMap.PointCollection(GLOBAL.points, options);
        pointCollection.addEventListener('click', function(e) {
            var point = new BMap.Point(e.point.lng, e.point.lat)
            var opts = {
                width: 250, 
                height: 80,
                title: "message",
                enableMessage: true
            }
        });
        GLOBAL.map.addOverlay(pointCollection)
    }
}

var afterHandel = function() {
    setTimeout(function() {
        var convertor = new BMap.Convertor()
        convertor.translate(GLOBAL.points, 1, 5 , this.initPoints)
     }, TIMER.second(1))
    GLOBAL.map.enableScrollWheelZoom(true)
}

var __main = function() {
    initTileLayer(GLOBAL.outputPath, GLOBAL.pictureFormat)
    initMap()
    dataBindPoints()
    initPoints()
    afterHandel()
}

window.onload = function() {
    __main()
}
</script>
</html>

java瓦片爬虫代码

java
package com.baidu.map;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Spider extends Thread {

    private static int z = 0;
    private static int xmin = 0;
    private static int xmax = 0;
    private static int ymin = 0;
    private static int ymax = 0;
    private static String targetHost = "";

    public  Spider(final  int z , final  int  xmin , final int  xmax , final int  ymin , final  int  ymax , final String  targetHost ){
        super();
        Spider.targetHost = targetHost;
        Spider.z = z;
        Spider.xmax = xmax;
        Spider.xmin = xmin;
        Spider.ymax = ymax;
        Spider.ymin = ymin;
    }


    public void run() {
      for (int i = xmin; i <= xmax; i++) {
        for (int j = ymin; j <= ymax; j++) {
            try {
                URL url = new URL(targetHost.replace("{x}", i + "").replace("{y}", j + "").replace("{z}",z + ""));
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(100);
                conn.connect();
                InputStream in = conn.getInputStream();
                File dir = new File("F:/map/baiduMap/tiles/" + z + "/" + i);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File file = new File("F:/map/baiduMap/tiles/" + z + "/" + i + "/" + j + ".png");
                if (!file.exists()) {
                    file.createNewFile();
                }
                OutputStream out = new FileOutputStream(file);
                byte[] bytes = new byte[1024 * 20];
                int len = 0;
                while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
      }
    }
}

java爬虫主函数

java
package com.baidu.map;

public class runJob {
    
    private  final static  String targetHost = "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl&udt=20170712&scaler=1&p=1";
    private  final static  int  z = 15;
    private  final static  int  xmin = 6000;
    private  final static  int  xmax = 6300;
    private  final static  int  ymin = 1600;
    private  final static  int  ymax = 2400;
    private  final  static  int  SPIDER_COUNT = 5; 
	
    public static void main(String[] args) {
        for( int i= 0 ; i< SPIDER_COUNT ; i++ )  {
            Spider  spider=new Spider( z ,xmin , xmax , ymin ,  ymax , targetHost );
            spider.start();
        }
    }
}

Powered by VitePress.