地址转换为坐标 geocoderGetLocation

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>地址至坐标转换</title>
</head>
<script charset="utf-8" src="https://map.qq.com/api/gljs?v=1.exp&libraries=service&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
<style type="text/css">
    html,
    body {
        height: 100%;
        margin: 0px;
        padding: 0px;
    }

    #container {
        width: 100%;
        height: 100%;
    }

    #panel {
        position: absolute;
        background: #FFF;
        width:300px;
        padding: 20px;
        z-index: 9999;
        top: 30px;
        left: 30px;
    }
</style>


<body>
    <div id="container"></div>
    <div id="panel">
        <p><label>地址</label><input id='address' type="text" value='北京市海淀区彩和坊路海淀西大街74号' ><input id="convert" type="button" class="btn" value="转换为坐标" onclick="convert()" /></p>
        <p><label>坐标</label><input id='location' type='text' disabled value='' /></p>
    </div>
</body>

<script type="text/javascript">
var map = new TMap.Map('container', {
  zoom: 14,
  center: new TMap.LatLng(39.986785, 116.301012),
});
var geocoder = new TMap.service.Geocoder(); // 新建一个正逆地址解析类
var markers = new TMap.MultiMarker({
  map: map,
  geometries: [],
});

function convert() {
  markers.setGeometries([]);
  // 将给定的地址转换为坐标位置
  geocoder
    .getLocation({ address: document.getElementById('address').value })
    .then((result) => {
      markers.updateGeometries([
        {
          id: 'main',
          position: result.result.location, // 将得到的坐标位置用点标记标注在地图上
        },
      ]);
      map.setCenter(result.result.location);
      document.getElementById(
        'location'
      ).value = result.result.location.toString();
      // 显示坐标数值
    });
}
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值