大地坐标转换成百度坐标

<!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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值