html,body,#container{
height:100%;
width:100%;
}
.btn-box{
position: absolute;
right: 5rem;
top: 5rem;
}
.btn{
background: '#0277bd';
width:14em;
margin-left:3.2rem;
margin-top: 0.8rem;
}
坐标批量转换
var map = new AMap.Map("container", {
resizeEnable: true,
center: [116.388904,39.913423],
zoom: 14
});
var $ = function(elementId){
return document.getElementById(elementId);
};
var lnglats = [
'116.368904,39.913423',
'116.398258,39.904600'
];
// 创建包含4个节点的折线及文字标注
var path = [
new AMap.LngLat(116.368904,39.913423),
new AMap.LngLat(116.398258,39.904600)
];
// 创建折线实例
var polyline = new AMap.Polyline({
path: path,
borderWeight: 2, // 线条宽度,默认为 1
strokeColor: 'red', // 线条颜色
lineJoin: 'round' // 折线拐点连接处样式
});
// 将折线添加至地图实例
map.add(polyline);
var text1 = new AMap.Text({
position: new AMap.LngLat(116.368904,39.913423),
text: 'gps坐标',
offset: new AMap.Pixel(-20, -20)
})
map.add(text1);
var polyline2;
var text2;
// 坐标转换
AMap.convertFrom(path, 'gps', function (status, result) {
if (result.info === 'ok') {
var path2 = result.locations;
polyline2 = new AMap.Polyline({
path: path2,
borderWeight: 2, // 线条宽度,默认为 1
strokeColor: 'blue', // 线条颜色
lineJoin: 'round' // 折线拐点连接处样式
});
map.add(polyline2);
text2 = new AMap.Text({
position: result.locations[0],
text: '高德坐标',
offset: new AMap.Pixel(-20, -20)
})
map.add(text2);
}
});
function setCoordinate() {
var type = this.id;
var m1Text = "GPS 坐标";
if(type == 'mapbar'){
m1Text = '图吧坐标';
}else if(type == 'baidu'){
m1Text = '百度坐标';
}
map.remove(polyline2);
map.remove(text2);
text1.setText(m1Text);
// 坐标转换
AMap.convertFrom(path, type, function (status, result) {
if (result.info === 'ok') {
var path2 = result.locations;
polyline2 = new AMap.Polyline({
path: path2,
borderWeight: 2, // 线条宽度,默认为 1
strokeColor: 'blue', // 线条颜色
lineJoin: 'round' // 折线拐点连接处样式
});
map.add(polyline2);
text2 = new AMap.Text({
position: result.locations[0],
text: '高德坐标',
offset: new AMap.Pixel(-20, -20)
})
map.add(text2);
}
});
}
//绑定radio点击事件
var radios = document.querySelectorAll("#coordinate input");
radios.forEach(function(ratio) {
ratio.onclick = setCoordinate;
});