高德地图 JS API示例-坐标系转换-其他坐标 -> 高德坐标 (批量)

高德地图 JS API示例-坐标系转换-其他坐标 -> 高德坐标 (批量)

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>其他坐标变换成高德坐标</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    <style>
        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; 
        }
    </style>
</head>
<body>
<div id="container"></div>
<div class="input-card" style="width:18rem">
    <h4>坐标批量转换</h4>
    <div id="coordinate">
      <div class="input-item"><input id="gps" name="language" type="radio" checked="checked"><span class="input-text">GPS 坐标转为高德坐标</span></div>
      <div class="input-item"><input id="mapbar" name="language" type="radio"><span class="input-text">图吧坐标转为高德坐标</span></div>
      <div class="input-item"><input id="baidu" name="language" type="radio"><span class="input-text">百度坐标转为高德坐标</span></div>
    </div>
</div>

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值&plugin=AMap.Geocoder"></script>
<script type="text/javascript">
    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;
    });


</script>
</body>
</html>
0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x