自己封装批量转换高德地图坐标点
/**
* @LngLatArray [Array] 带转换坐标的数组
* @success [Function]转换完成回调
*/
function convertFrom(LngLatArray, success) {
var LngLatArray2 = [];
// https://lbs.amap.com/api/javascript-api/reference/lnglat-to-address/#m_AMap.convertFrom
// 将其他地图服务商的坐标批量转换成高德地图经纬度坐标。最多支持40对坐标。
var size = 40;
var pageNum = parseInt((LngLatArray.length + size - 1) / size);
var convertNum = 0;
for (var i = 0; i < pageNum; i++) {
var LngLatArraySlice = LngLatArray.slice(i * size, (i + 1) * size);
convert(LngLatArraySlice, i);
}
function convert(LngLatArray, n) {
// 测试
/*setTimeout(function () {
LngLatArray2[n] = (LngLatArray);
convertNum++;
if (convertNum >= pageNum) {
typeof success == "function" ? success([].concat.apply([], LngLatArray2)) : null;
}
}, parseInt(Math.random() * 3) * 100);*/
AMap.convertFrom(LngLatArray, 'gps', function (status, result) {
if (status == 'complete') {
LngLatArray2[n] = (result.locations);
convertNum++;
if (convertNum >= pageNum) {
typeof success == "function" ? success([].concat.apply([], LngLatArray2)) : null;
}
}
});
};
}
调用示例:
convertFrom([[108.860649, 34.16061], [108.86071, 34.160591], [108.861061, 34.16061], [108.861122, 34.160595], [108.861153, 34.160561], [108.861191, 34.160297], [108.861191, 34.160217], [108.861221, 34.160053], [108.861221, 34.160053], [108.862083, 34.160122], [108.862419, 34.160122], [108.86306, 34.160053], [108.863213, 34.160065], [108.863953, 34.160908], [108.864403, 34.161388], [108.864883, 34.161888], [108.865295, 34.162285], [108.865852, 34.1628], [108.866112, 34.163044], [108.867012, 34.163792], [108.867577, 34.164234], [108.868668, 34.165054], [108.868927, 34.16523], [108.8703, 34.166111], [108.871078, 34.166565], [108.872711, 34.167477], [108.874062, 34.168213], [108.874504, 34.168461], [108.875137, 34.168812], [108.876259, 34.169437], [108.87632, 34.169472], [108.876816, 34.169777], [108.877731, 34.170395], [108.87822, 34.170792], [108.878754, 34.171246], [108.879013, 34.171463], [108.87925, 34.171707], [108.879601, 34.172043], [108.879997, 34.172482], [108.880424, 34.172997], [108.880852, 34.17355], [108.881393, 34.17437], [108.881691, 34.174835], [108.881989, 34.175369], [108.882072, 34.175507], [108.882332, 34.175919], [108.882561, 34.176323], [108.883209, 34.177437], [108.883812, 34.178463], [108.884018, 34.178822], [108.884079, 34.178928], [108.88459, 34.179806], [108.8853, 34.180996], [108.885399, 34.181168], [108.886154, 34.182453], [108.88649, 34.18301], [108.887001, 34.183895], [108.887444, 34.184635], [108.88784, 34.18531], [108.887878, 34.185375], [108.888016, 34.185616], [108.888847, 34.187004], [108.888931, 34.187168], [108.889168, 34.187557], [108.889206, 34.187645], [108.890015, 34.189037], [108.890266, 34.189453], [108.890648, 34.190094], [108.891121, 34.190899], [108.891251, 34.191116], [108.891304, 34.191212], [108.891304, 34.191212], [108.891441, 34.191334], [108.891632, 34.19157], [108.891953, 34.192032], [108.892059, 34.192181], [108.892212, 34.19231], [108.892296, 34.192375], [108.892403, 34.192425], [108.892494, 34.192459], [108.89267, 34.192486], [108.89283, 34.19249], [108.892944, 34.192482], [108.893082, 34.192444], [108.893349, 34.192356], [108.893456, 34.192356], [108.893456, 34.192356], [108.893532, 34.192253], [108.89357, 34.19215], [108.893661, 34.191936], [108.893715, 34.191822], [108.893768, 34.191715], [108.893967, 34.191433], [108.893967, 34.191433], [108.894173, 34.19117], [108.894234, 34.191105], [108.894402, 34.190941], [108.894516, 34.190804], [108.894516, 34.190804], [108.895393, 34.189453], [108.895439, 34.189388], [108.895935, 34.188602], [108.896103, 34.188366], [108.896278, 34.188129], [108.896408, 34.187988], [108.896477, 34.18792], [108.896652, 34.187801], [108.896736, 34.18774], [108.896835, 34.187695], [108.897034, 34.187614], [108.897232, 34.18755], [108.897339, 34.187531], [108.897568, 34.187504], [108.897781, 34.187504], [108.897896, 34.187508], [108.898109, 34.187546], [108.898323, 34.187614], [108.898415, 34.187645], [108.898613, 34.187737], [108.898788, 34.187847], [108.899269, 34.188229], [108.899452, 34.188347], [108.89962, 34.188461], [108.899803, 34.188553], [108.900002, 34.188629], [108.900108, 34.188667], [108.900322, 34.188717], [108.900528, 34.188751], [108.90123, 34.188786], [108.901535, 34.188808], [108.901749, 34.188835], [108.901886, 34.188854], [108.902222, 34.188953], [108.905861, 34.189125], [108.90847, 34.189186], [108.911049, 34.189163], [108.912781, 34.189125], [108.915184, 34.189007], [108.916275, 34.188938], [108.917603, 34.188828], [108.918457, 34.188751], [108.920685, 34.18853], [108.922592, 34.188278], [108.926277, 34.187737], [108.928886, 34.18745], [108.930183, 34.18734], [108.931763, 34.187237], [108.933411, 34.187168], [108.934921, 34.187141], [108.936638, 34.187145], [108.938347, 34.187187], [108.940109, 34.187267], [108.94043, 34.187286], [108.941063, 34.18734], [108.943054, 34.187511], [108.944229, 34.187645], [108.944916, 34.187717], [108.945312, 34.187759], [108.946533, 34.187893], [108.947128, 34.187946], [108.949394, 34.188137], [108.950188, 34.188187], [108.950821, 34.188217], [108.952782, 34.188278], [108.953445, 34.188278], [108.954605, 34.188278], [108.955757, 34.188255], [108.957779, 34.188183], [108.959015, 34.18811], [108.960243, 34.188007], [108.962006, 34.187832], [108.962402, 34.187782], [108.963364, 34.18766], [108.964539, 34.187508], [108.967644, 34.187122], [108.968552, 34.187035], [108.969994, 34.186935], [108.970833, 34.186893], [108.971718, 34.186863], [108.972771, 34.186848], [108.973976, 34.186863], [108.975166, 34.18689], [108.975708, 34.186913], [108.97744, 34.187035], [108.978806, 34.187172], [108.980888, 34.187462], [108.982536, 34.187756], [108.982857, 34.18782], [108.983727, 34.187992], [108.984375, 34.188137], [108.984489, 34.188164], [108.985588, 34.188435], [108.986732, 34.188744], [108.988411, 34.189255], [108.989006, 34.189453], [108.989601, 34.189648], [108.990364, 34.18993], [108.991661, 34.190426], [108.99234, 34.190697], [108.994438, 34.191628], [108.996254, 34.192513], [108.998596, 34.193638], [109.001648, 34.195084], [109.003143, 34.195824], [109.004051, 34.196232], [109.004555, 34.196449], [109.00489, 34.196583], [109.006348, 34.197208], [109.007248, 34.197578], [109.008759, 34.198177], [109.009895, 34.198608], [109.011467, 34.199188], [109.013641, 34.199921], [109.015274, 34.200439], [109.015717, 34.200573], [109.017494, 34.201099], [109.019814, 34.201721], [109.020988, 34.202015], [109.021858, 34.201992], [109.022415, 34.201965], [109.023033, 34.202015], [109.023857, 34.202175], [109.024567, 34.20237], [109.024857, 34.202461], [109.025436, 34.202713], [109.026268, 34.203156], [109.028107, 34.20348], [109.02832, 34.203514], [109.030296, 34.203835], [109.03418, 34.204414], [109.036667, 34.204815], [109.037575, 34.20499], [109.038765, 34.205261], [109.03965, 34.205502], [109.040329, 34.205704], [109.041519, 34.20612], [109.042473, 34.206493], [109.043243, 34.206825], [109.044426, 34.207397], [109.045647, 34.208076], [109.046646, 34.208702], [109.047218, 34.209103], [109.047874, 34.209579], [109.048531, 34.210091], [109.048965, 34.210464], [109.049553, 34.210983], [109.050018, 34.211426], [109.050293, 34.211689], [109.050674, 34.212067], [109.052788, 34.214256], [109.054199, 34.215691], [109.055359, 34.216908], [109.057388, 34.219017], [109.057884, 34.219563], [109.058578, 34.220356], [109.059212, 34.221157], [109.059746, 34.221909], [109.060051, 34.222408], [109.060265, 34.222759], [109.060776, 34.22374], [109.061081, 34.2244], [109.061211, 34.224705], [109.061493, 34.225464], [109.061699, 34.226089], [109.06189, 34.226841], [109.062065, 34.227631], [109.062172, 34.228271], [109.062309, 34.229435], [109.062599, 34.23243], [109.062653, 34.232979], [109.062683, 34.233398], [109.062706, 34.233479], [109.062714, 34.233627], [109.062729, 34.23381], [109.06282, 34.234627], [109.063019, 34.236244], [109.063141, 34.236893], [109.063316, 34.237755], [109.063629, 34.238888], [109.063858, 34.239632], [109.06427, 34.240749], [109.064545, 34.241432], [109.065376, 34.243416], [109.065704, 34.244152], [109.065804, 34.244385], [109.066376, 34.245747], [109.066772, 34.246666], [109.066994, 34.247192], [109.067215, 34.24773], [109.067215, 34.24773], [109.067375, 34.24791], [109.067749, 34.248657], [109.068176, 34.249466], [109.068398, 34.249866], [109.068832, 34.250572], [109.068932, 34.250774], [109.06897, 34.250916], [109.068977, 34.251003], [109.068977, 34.251076], [109.068962, 34.251156], [109.068909, 34.251282], [109.068825, 34.251396], [109.068756, 34.251461], [109.068687, 34.25153], [109.068596, 34.251579], [109.068512, 34.251621], [109.068436, 34.251648], [109.068329, 34.251678], [109.068222, 34.25169], [109.0681, 34.25169], [109.06797, 34.251678], [109.067833, 34.251644], [109.067215, 34.251476], [109.066978, 34.251415], [109.066734, 34.251366], [109.066483, 34.251339], [109.066154, 34.251308], [109.065819, 34.251297], [109.065475, 34.251339], [109.065308, 34.251366], [109.065086, 34.251392], [109.064819, 34.251411], [109.06443, 34.251408], [109.06443, 34.251408], [109.063332, 34.25132], [109.062935, 34.251301], [109.062775, 34.251301], [109.061363, 34.251308], [109.060936, 34.251297], [109.060249, 34.251308], [109.060143, 34.251308], [109.059669, 34.251308], [109.059563, 34.251308], [109.058952, 34.251316], [109.058029, 34.251316], [109.056717, 34.25132], [109.056229, 34.251324], [109.055725, 34.25132], [109.055199, 34.251316], [109.055199, 34.251316], [109.054878, 34.25145], [109.05471, 34.25153], [109.054634, 34.251568], [109.054512, 34.251659], [109.054382, 34.251785], [109.054237, 34.251945], [109.054062, 34.252213], [109.053856, 34.252472], [109.053688, 34.252697], [109.053185, 34.253593], [109.053123, 34.253731], [109.052666, 34.254475], [109.052338, 34.255348], [109.052322, 34.255367], [109.052071, 34.255978], [109.051811, 34.256592], [109.051025, 34.258236], [109.050865, 34.258591], [109.050613, 34.259098], [109.050339, 34.25975], [109.050293, 34.25983], [109.049568, 34.261284], [109.049248, 34.261936], [109.048241, 34.264072], [109.047829, 34.265079], [109.047485, 34.266174], [109.047432, 34.266354], [109.047264, 34.267094], [109.047119, 34.267906], [109.04705, 34.268459], [109.046997, 34.269104], [109.046967, 34.269779], [109.046967, 34.270679], [109.046997, 34.271172], [109.047066, 34.272034], [109.047127, 34.272434], [109.047127, 34.272434], [109.047218, 34.272514], [109.047256, 34.272583], [109.047318, 34.272758], [109.047432, 34.273064], [109.047638, 34.273533], [109.047707, 34.273735], [109.047707, 34.273735], [109.047783, 34.273998], [109.047829, 34.274231], [109.047852, 34.274414], [109.047859, 34.274677], [109.047852, 34.275448], [109.047859, 34.275867], [109.047882, 34.276001], [109.047935, 34.276112], [109.048027, 34.276215], [109.048149, 34.276302], [109.048294, 34.276348], [109.048454, 34.276352], [109.048607, 34.276329], [109.048744, 34.276268], [109.048859, 34.276173], [109.048897, 34.276096], [109.048927, 34.276054], [109.048958, 34.275925], [109.048943, 34.275799], [109.048912, 34.275745], [109.048882, 34.275673], [109.048775, 34.275574], [109.048637, 34.275505], [109.0485, 34.275448], [109.048309, 34.275406], [109.047073, 34.275314], [109.046379, 34.275223], [109.046051, 34.275166], [109.045288, 34.275013], [109.044518, 34.274868], [109.044174, 34.274799], [109.043831, 34.274731], [109.04319, 34.274597], [109.042862, 34.274529], [109.042854, 34.274601], [109.042679, 34.275276], [109.042366, 34.276535], [109.042145, 34.277344], [109.042046, 34.277718], [109.041672, 34.279194], [109.041519, 34.279598], [109.041214, 34.280258], [109.040802, 34.281044], [109.040375, 34.281792], [109.04007, 34.282215], [109.038971, 34.283466], [109.038719, 34.283772], [109.038239, 34.284451], [109.038193, 34.284504], [109.037758, 34.285248], [109.037483, 34.285801], [109.037186, 34.28651], [109.036972, 34.28709], [109.036919, 34.287277], [109.036819, 34.287632], [109.036713, 34.288128], [109.036674, 34.28833], [109.036621, 34.28867], [109.036507, 34.289463], [109.036324, 34.290886], [109.036301, 34.291359], [109.036308, 34.292542], [109.036331, 34.292667], [109.036301, 34.294243], [109.036285, 34.294846], [109.036217, 34.29533], [109.036087, 34.295933], [109.03582, 34.297081], [109.03582, 34.297081], [109.037994, 34.297352], [109.038399, 34.297405], [109.041977, 34.297832], [109.04361, 34.298023], [109.043671, 34.298038], [109.046616, 34.298389], [109.046616, 34.298389], [109.046692, 34.297955], [109.046883, 34.297272], [109.046883, 34.297272], [109.046677, 34.297245], [109.045876, 34.297134], [109.04512, 34.297039], [109.04464, 34.296982], [109.044044, 34.296902], [109.043571, 34.296841], [109.043571, 34.296841], [109.043594, 34.296715]], function (data) {
console.log('转换完成' + JSON.stringify(data));
});
来源:oschina
链接:https://my.oschina.net/u/4410118/blog/3344954