大地坐标转换成百度坐标

<!DOCTYPE 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"/>
    <title>大地坐标转换成百度坐标</title>

    <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=你密钥"></script>
</head>
<body>
<div id="allmap"></div>

<script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 15);
    var convertor = new BMap.Convertor();

    //注意:百度和谷歌的经纬度坐标顺序是相反的。
    var carPoints = [new BMap.Point(116.3786889372559, 39.90762965106183),
        new BMap.Point(116.38632786853032, 39.90795884517671),
        new BMap.Point(116.39534009082035, 39.907432133833574),
        new BMap.Point(116.40624058825688, 39.90789300648029),
        new BMap.Point(116.41413701159672, 39.90795884517671),
        new BMap.Point(116.41413701159672, 39.908),
        new BMap.Point(116.41413701159672, 39.909),
        new BMap.Point(116.41413701159672, 39.910),
        new BMap.Point(116.41413701159672, 39.911),
        new BMap.Point(116.41413701159672, 39.912),
        new BMap.Point(116.41413701159672, 39.913),
        new BMap.Point(116.41413701159672, 39.914),
        new BMap.Point(116.41413701159672, 39.915),
        new BMap.Point(116.41413701159672, 39.916),
        new BMap.Point(116.41413701159672, 39.917),
        new BMap.Point(116.41413701159672, 39.918),
        new BMap.Point(116.41413701159672, 39.919),
        new BMap.Point(116.41413701159672, 39.920),
        new BMap.Point(116.41413701159672, 39.921),
        new BMap.Point(116.41413701159672, 39.922),
        new BMap.Point(116.41413701159672, 39.923),
        new BMap.Point(116.41413701159672, 39.924),
        new BMap.Point(116.41413701159672, 39.925),
        new BMap.Point(116.41413701159672, 39.926),
        new BMap.Point(116.41413701159672, 39.927),
        new BMap.Point(116.41413701159672, 39.928),
        new BMap.Point(116.41413701159672, 39.929),
        new BMap.Point(116.41413701159672, 39.930),
        new BMap.Point(116.41413701159672, 39.931),
        new BMap.Point(116.41413701159672, 39.932),
        new BMap.Point(116.41413701159672, 39.933),
        new BMap.Point(116.41413701159672, 39.934),
        new BMap.Point(116.41413701159672, 39.935),
        new BMap.Point(116.41413701159672, 39.936),
        new BMap.Point(116.41413701159672, 39.937),
        new BMap.Point(116.41413701159672, 39.938),
        new BMap.Point(116.41413701159672, 39.939),
        new BMap.Point(116.41413701159672, 39.940),
        new BMap.Point(116.41413701159672, 39.941),
        new BMap.Point(116.41413701159672, 39.942),
        new BMap.Point(116.41413701159672, 39.943),
        new BMap.Point(116.41413701159672, 39.944),
        new BMap.Point(116.41413701159672, 39.945),
        new BMap.Point(116.41413701159672, 39.946),
        new BMap.Point(116.41413701159672, 39.947),
        new BMap.Point(116.41413701159672, 39.948),
        new BMap.Point(116.41413701159672, 39.949),
        new BMap.Point(116.41413701159672, 39.950),
    ];
    var arr = [], i = 0, newCarPoint = [];

 // 百度API接口一次只能转换10个
    for (var a = 0; a < carPoints.length; a = a + 10) {
        arr.push(carPoints.slice(a, a + 10));
    }
    console.log(arr);

   // 声明一个递归方法
    function translateCarPoints(i) {
        convertor.translate(arr[i], 1, 5, function (res) {
            console.table(res.points);
            newCarPoint = newCarPoint.concat(res.points);
            if (arr[i + 1] && arr[i + 1].length > 0) {
                i++;
                translateCarPoints(i);
            }
            if (newCarPoint.length === carPoints.length) {
                console.log(newCarPoint);
                for (var j = 0; j < newCarPoint.length; j++) {
                    var point = new BMap.Point(newCarPoint[j].lng, newCarPoint[j].lat);
                    var marker = new BMap.Marker(point);
                    map.addOverlay(marker);
                    map.setCenter(point);// 由于写了这句,每一个被设置的点都是中心点的过程
                }
            }
        });

    }
    translateCarPoints(i);
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/web_cgh/article/details/82632336