效果图
右侧输入行程,
可添加多次行程,
生成足迹飞线图,
包含导出html代码功能
全部代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>足迹飞线图生成器</title>
<link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
padding-top: 0px
}
.footer {
padding-top: 40px;
padding-bottom: 40px;
margin-top: 40px;
border-top: 1px solid #eee
}
.jumbotron {
text-align: center;
background-color: transparent
}
.jumbotron .btn {
padding: 14px 24px;
font-size: 21px
}
.nav-justified {
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px
}
.nav-justified > li > a {
padding-top: 15px;
padding-bottom: 15px;
margin-bottom: 0;
font-weight: 700;
color: #777;
text-align: center;
background-color: #e5e5e5;
background-image: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#e5e5e5));
background-image: -webkit-linear-gradient(top, #f5f5f5 0%, #e5e5e5 100%);
background-image: linear-gradient(to bottom, #f5f5f5 0%, #e5e5e5 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f5f5f5', endColorstr='#e5e5e5', GradientType=0);
background-repeat: repeat-x;
border-bottom: 1px solid #d5d5d5
}
.nav-justified > .active > a, .nav-justified > .active > a:focus, .nav-justified > .active > a:hover {
background-color: #ddd;
background-image: none;
box-shadow: inset 0 3px 7px rgba(0, 0, 0, .15)
}
.nav-justified > li:first-child > a {
border-radius: 5px 5px 0 0
}
.nav-justified > li:last-child > a {
border-bottom: 0;
border-radius: 0 0 5px 5px
}
@media (min-width: 768px) {
.nav-justified {
max-height: 52px
}
.nav-justified > li > a {
border-right: 1px solid #d5d5d5;
border-left: 1px solid #fff
}
.nav-justified > li:first-child > a {
border-left: 0;
border-radius: 5px 0 0 5px
}
.nav-justified > li:last-child > a {
border-right: 0;
border-radius: 0 5px 5px 0
}
}
.form-horizontal input {
margin-top: 10px;
margin-bottom: 10px;
}
.modal-body {
font-size: 12px;
}
</style>
</head>
<body>
<div class="row" style='margin-top:20px;width: 98%'>
<div class="col-md-9" id='main'></div>
<div class="col-md-3" style='padding-left:20px;'>
<p class="alert alert-success">每次行程内部,两个目的地(城市 景点)以空格间隔</p>
<form class="form-horizontal">
<input id='title-input' type="text" class='form-control' value='' placeholder='请输入标题'>
<button type='button' id='add-btn' class="btn btn-success">添加行程</button>
<button type='button' id='remove-null-btn' class="btn btn-success">移除空行程</button>
<button type='button' id="fresh-btn" class='btn btn-success pull-right'>查看效果</button>
<input type="text" class='form-control router' value='北京 平遥 北京'>
<button id='output-btn' type="button" class="btn btn-success btn-lg btn-block">导出代码</button>
</form>
</div>
</div>
<div class="modal fade" id="codeModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel"></h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<script id='t1' type='text/template' charset="utf-8">
<!DOCTYPE html>
<lang="en">
<head>
<meta charset="utf-8">
<title><%=obj.title%></title>
</head>
<body >
<div id="main" style="margin:20px;"></div>
<script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script>
<script type="text/javascript" charset="utf-8">
if ((window.screen.height) > (window.screen.width)) {
document.getElementById('main').style.width=(window.screen.width)*2+'px' ;
document.getElementById('main').style.height=(window.screen.height)+'px' ;
} else {
document.getElementById('main').style.width=(window.screen.width-50)+'px' ;
document.getElementById('main').style.height=(window.screen.height-200)+'px' ;
}
var myChart = echarts.init(document.getElementById('main'));
var option = <%=obj.option%>;
myChart.setOption(option);
</script>
</body>
</html>
</script>
<script src='http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js'></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="http://cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js"></script>
<script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script>
<script type="text/javascript" charset="utf-8">
function FootPrint() {
this.key = 'fHrNQj6DHTjZtfTvfqbsuvTzKc5V9SBl'
this.url = 'http://api.map.baidu.com/geocoder/v2/?output=json&ak=' + this.key + '&address='
this.addbtn = $('#add-btn')
this.freshbtn = $('#fresh-btn')
this.removeNull = $('#remove-null-btn')
this.outbtn = $('#output-btn')
this.chart = echarts.init(document.getElementById('main'))
this.tmpl = $('#t1').html()
this.clearCache()
this.option = {
backgroundColor: '#FFFFFF',//背景颜色
color: ['gold', 'aqua', 'lime'],
title: {
text: '',
subtext: '',
x: 'center',
textStyle: {
color: '#1b1b1b'
}
},
tooltip: {
trigger: 'item',
formatter: '{b}'
},
dataRange: {
show: false,
min: 0,
max: 100,
calculable: true,
color: ['#ff3333', 'orange', 'yellow', 'lime', 'aqua'],
textStyle: {
color: '#fff'
}
},
series: [{
name: '全国',
type: 'map',
roam: true,//开启 缩放
scaleLimit: {
min: 1.1,//最小 缩放比例
max: 4 //最大 缩放比例
},
hoverable: false,
mapType: 'china',
itemStyle: {
//默认背景颜色
normal: {
"borderColor": "#6495ED",//默认边框颜色
borderWidth: 0.5,
areaStyle: {
color: '#1b1b1b'//默认背景颜色
}, label: {
show: true, //打开省份名展示
},
}
},
data: [],
geoCoord: []
}, {
name: '足迹',
type: 'map',
mapType: 'china',
data: [],
markLine: {
smooth: true,
effect: {
show: true,
scaleSize: 1,
period: 30,
color: '#fff',
shadowBlur: 10
},
itemStyle: {
normal: {
borderWidth: 1,
lineStyle: {
type: 'solid',
shadowBlur: 10
},
label: {
show: true
}
}
},
data: []
},
markPoint: {
symbol: 'emptyCircle',
symbolSize: function (v) {
return 10 + v / 10
},
effect: {
show: true,
shadowBlur: 0
},
itemStyle: {
normal: {
label: {
show: true
}
},
emphasis: {
label: {
show: true,
position: 'top'
}
}
},
data: []
}
}]
};
}
$.extend(FootPrint.prototype, {
bindEvent: function () {
var that = this
that.addbtn.on('click', function () {
//增加新的input标签
that.outbtn.before('<input type="text" placeholder="请输入行程,多个地点之间用空格间隔" class="form-control router">')
//触发[查看效果]按钮
that.freshbtn.click();
})
$.ajaxSettings.async = false;
that.freshbtn.on('click', function () {
that.clearCache()
that.getInputVal()
that.title = $('#title-input').val() || '默认标题'
var size = _.size(that.cache)
$.each(that.cache, function (key) {
$.getJSON(that.url + key + '&callback=?', function (res) {
if (res.result) {
var loc = res.result.location
that.alldata[key] = [loc.lng, loc.lat]
that.pointdata.push({
'name': key
})
if (_.size(that.alldata) === size) {
that.initPage()
}
}
})
})
}).trigger('click');
that.outbtn.on('click', function () {
//触发 [查看效果]按钮
that.freshbtn.click();
//获取配置
var obj = {
title: that.title,
option: JSON.stringify(that.option)
}
pageCode = _.template(that.tmpl)(obj)
//先触发 freshbtn效果事件,再导出
exportRaw(that.title + '.html', pageCode.replaceAll('<', '<').replaceAll('>', '>'))
})
that.removeNull.on('click', function () {
//触发 [查看效果]按钮
that.freshbtn.click();
//循环移除空input元素
$('input.router').each(function (i, v) {
var val = $(v).val().replace(' ', '');
if (!val) {
this.remove();
}
})
}).trigger('click');
},
initPage: function () {
var that = this, tmp;
$.each(that.foot, function (a, v) {
tmp = v.split(' ')
for (var i = 0; i < tmp.length - 1; i++) {
that.linedata.push([{
'name': tmp[i]
}, {
'name': tmp[i + 1]
}])
}
})
that.option.title.text = that.title
that.option.series[0].geoCoord = that.alldata
that.option.series[1].markLine.data = that.linedata
that.option.series[1].markPoint.data = that.pointdata
that.chart.setOption(that.option, true);
},
getInputVal: function () {
var that = this
that.foot = []
$('input.router').each(function (i, v) {
var val = $(v).val().replace(' ', ' ').replace(' ', ' ').replace(' ', ' ').replace(' ', ' ');
if (val) {
$.each(val.split(' '), function (j, s) {
that.cache[s] = true
})
that.foot.push(val)
}
})
},
clearCache: function () {
this.foot = []
// all data
this.cache = {}
this.alldata = {}
this.linedata = []
this.pointdata = []
},
init: function () {
this.bindEvent()
$('.masthead a').attr('target', '_blank')
}
})
//导出文本
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 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 () {
if ($(window).height() > $(window).width()) {
$('#main').height($(window).width() );
} else {
$('#main').height($(window).height()- 50);
}
var foot = new FootPrint()
foot.init();
})
</script>
</body>
</html>