百度地图API 批量坐标转换 回调问题

出处:https://blog.csdn.net/itmyhome1990/article/details/50426928

在将原始坐标批量转换的时候,回调的时候发现 data.status = 25

translateCallback = function (data){
      //回调时 data.status = 25
}
 
  • 1
  • 2
  • 3

原因是 个数非法,超过限制 百度API一次最多只支持10个点坐标转换

状态码说明

状态码含义
0正常
1内部错误
21from非法
22to非法
24coords格式非法
25coords个数非法,超过限制


解决方法可以每十个点为一组,然后依次转换

以下代码可全部复制到HTML中直接运行查看

<!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" />
    <style type="text/css">
        body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=rmtT1e2a4k770D9jK1ouGODt"></script>
    <title>GPS转百度批量转换</title>
</head>
<body>
    <div id="allmap"></div>
</body>
</html>
<script type="text/javascript">

    //所有批量转换的坐标
    var points = [new BMap.Point(116.3986889372559,39.91762965106183),
                  new BMap.Point(116.38632786853032,39.90795884517671),
                  new BMap.Point(116.39534009082035,39.917432133833574),
                  new BMap.Point(116.41624058825688,39.91789300648029),
                  new BMap.Point(116.41413701159672,39.90795884517671),

                  new BMap.Point(116.3886889372559,39.90962965106183),
                  new BMap.Point(116.39632786853032,39.91795884517671),
                  new BMap.Point(116.37534009082035,39.907432133833574),
                  new BMap.Point(116.41624058825688,39.92489300648029),
                  new BMap.Point(116.42413701159672,39.91795884517671),

                  new BMap.Point(116.41413701159672,39.92795884517671),
                  new BMap.Point(116.40624058825688,39.93489300648029),
                  new BMap.Point(116.42213701159672,39.90795884517671),
                  new BMap.Point(116.40413701159672,39.91795884517671)
    ];

    //地图初始化
    var bm = new BMap.Map("allmap");
    bm.centerAndZoom(new BMap.Point(116.378688937,39.9076296510), 15);

    var total = 0; //总记录数
    var groupCount = 0; //每次转十条
    if (points.length % 10 > 0) {
        groupCount = (points.length / 10) + 1;
    }
    else {
        groupCount = (points.length / 10);
    }

    for(var i=0;i<groupCount;i++){ //外层循环,有多少组十条
        var pos = new Array();
        for(var j=0;j<10;j++){ //内层循环,每组十条
            if(total<points.length){ //不超过总记录数结束
                var point = new BMap.Point(points[(i * 10) + j].lng,points[(i * 10) + j].lat);
                pos.push(point);
            }
            total++;
        }

        var convertor = new BMap.Convertor();
        convertor.translate(pos, 1, 5, function(data){
            if(data.status === 0) {
                for (var i = 0; i < data.points.length; i++) {
                    bm.addOverlay(new BMap.Marker(data.points[i]));
                    bm.setCenter(data.points[i]);
                }
              }
        });

    }


</script>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73


演示:baidu_map_batch_conversion/

作者:itmyhome


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页