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", {
center: [116.473222,39.993214],
zoom: 15
});
var $ = function(elementId){
return document.getElementById(elementId);
};
// 创建gps坐标位置点标记
var lnglat = [116.46706996,39.99188446];
var m1 = new AMap.Marker({
position: lnglat,
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png"
});
map.add(m1);
m1.setLabel({
offset: new AMap.Pixel(20, 20),
content: "GPS 坐标系中首开广场"
});
var m2;
// 坐标转换
function convertFrom(lnglat, type){
AMap.convertFrom(lnglat, type, function (status, result) {
if (result.info === 'ok') {
var resLnglat = result.locations[0];
m2 = new AMap.Marker({
position: resLnglat,
});
map.add(m2);
// 设置标签
m2.setLabel({
offset: new AMap.Pixel(20, 20),
content: "高德坐标系中首开广场(正确)"
});
}
});
}
convertFrom(lnglat,'gps');
function setCoordinate() {
var type = this.id;
var m1Text = "GPS 坐标系中首开广场";
if(type == 'mapbar'){
m1Text = '图吧坐标系中首开广场';
lnglat = [116.46179996,39.99241446]; // 图吧坐标系下首开广场
}else if(type == 'baidu'){
m1Text = '百度坐标系中首开广场';
lnglat = [116.4798674287,39.9989020876]; // 百度坐标系下首开广场
}else if(type == 'gps'){
lnglat = [116.46706996,39.99188446]; // 百度坐标系下首开广场
}
// 设置label标签
m1.setLabel({
offset: new AMap.Pixel(20, 20),
content: m1Text
});
m1.setPosition(lnglat);
map.remove(m2);
// 坐标转换
convertFrom(lnglat,type);
}
//绑定radio点击事件
var radios = document.querySelectorAll("#coordinate input");
radios.forEach(function(ratio) {
ratio.onclick = setCoordinate;
});