拼接过程
var str = "116.387112,39.920977;116.385243,39.91306;116.394226,39.917988";//默认经纬度字符串; var PointArr = []; //将字符串分离成数组; var newArr = str.split(";"); //将新的数组二次分离为百度坐标系; for (var i = 0; i < newArr.length; i++) { var lng = newArr[i].split(",")[0]; var lat = newArr[i].split(",")[1]; PointArr.push(new BMap.Point(lng, lat)); } //输入坐标系数组; console.log(PointArr);
函数封装
/*封装函数 * str,需要转化的经纬度字符串,格式如"116.387112,39.920977;116.385243,39.91306" * PointArr,转化后的经纬度坐标数组; * */ function makePoint(str) { var PointArr = []; //将字符串分离成数组; var newArr = str.split(";"); //将新的数组二次分离为百度坐标系; for (var i = 0; i < newArr.length; i++) { var lng = newArr[i].split(",")[0]; var lat = newArr[i].split(",")[1]; PointArr.push(new BMap.Point(lng, lat)); } return PointArr; } var str = "116.387112,39.920977;116.385243,39.91306;116.394226,39.917988";//默认经纬度字符串; var PointArr = []; //输出坐标系数组; var points = makePoint(str); //调用经纬度坐标; console.log(points[0]);
lockdatav Done!