获取geojson文件
//获取geojson文件; $.getJSON('zj.json', function (data) { var d = []; for (var i = 0; i < data.features.length; i++) { //筛选条件; var str = data.features[i].properties.FULLNAMEPI; if (str.search("haishuqu") != -1) { d.push({ type: "Feature", geometry: {"type": "Polygon", coordinates: data.features[i].geometry.coordinates}, properties: { country: "海曙区", name: data.features[i].properties.FULLNAMEPI.replace("zhejiangshengningboshihaishuqu", "") } }) } } //转为字符串 var json = JSON.stringify(d); var begin = '{"type": "FeatureCollection", "features": '; var end = '}'; //拼接标准格式字符串; //console.log(begin + json + end); var txt = begin + json + end; //导出文件; exportRaw('text.json', txt) });
导出文件的封装函数
//导出txt文件; function fakeClick(obj) { var ev = document.createEvent("MouseEvents"); ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); obj.dispatchEvent(ev); } function exportRaw(name, data) { var urlObject = window.URL || window.webkitURL || window; var export_blob = new Blob([data]); var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a") save_link.href = urlObject.createObjectURL(export_blob); save_link.download = name; fakeClick(save_link); }
获取字符串长度
//获取字符串长度; function getLength(str) { console.log(str.length); }
Done!