怎么打开获取用户自己现在的定位

来源:3-12 自由编程

bao_

2021-01-06 18:32:54

​怎么打开获取用户自己现在的定位

写回答

2回答

好帮手慕星星

2021-01-22

同学你好,高德地图中有浏览器精确定位,可以看看,是不是想要这种效果:

https://lbs.amap.com/api/javascript-api/example/location/browser-location

上面链接中是获取当前位置的经纬度,如果想要转化为地理位置,可以使用下面的方式转化一下(坐标转地址)

https://lbs.amap.com/api/javascript-api/example/geocoder/regeocoding

祝学习愉快!

0

好帮手慕久久

2021-01-06

同学你好,可以使用高德地图提供的使用ip的方式获取定位,如下:

http://img.mukewang.com/climg/5ff5985109b66be403600144.jpg

代码可参考如下:

<!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>根据ip定位</title>
<link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
<style type="text/css">
html,body,#container{
height:100%;
}
</style>
</head>
<body>
<div id="container"></div>
<div class="info">
<p id='info'></p>
</div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=dcee67f573cdde2da470cffd5a66b429&plugin=AMap.CitySearch"></script>
<script type="text/javascript">
var map = new AMap.Map("container", {
resizeEnable: true,
center: [116.397428, 39.90923],
zoom: 13
});
//获取用户所在城市信息
function showCityInfo() {
//实例化城市查询类
var citysearch = new AMap.CitySearch();
//自动获取用户IP,返回当前城市
citysearch.getLocalCity(function(status, result) {
if (status === 'complete' && result.info === 'OK') {
if (result && result.city && result.bounds) {
var cityinfo = result.city;
var citybounds = result.bounds;
document.getElementById('info').innerHTML = '您当前所在城市:'+cityinfo;
//地图显示当前城市
map.setBounds(citybounds);
}
} else {
document.getElementById('info').innerHTML = result.info;
}
});
}
showCityInfo();
</script>
</body>
</html>

参考文档如下:

https://lbs.amap.com/api/javascript-api/example/location/get-city-name-by-ip-location

如果同学想要更精确的定位,则可以参考该文档,自己尝试扩展下。

祝学习愉快!​

0
hao_
hp>不是定位你在哪所城市,是获取用户现在准确的定位,和手机定位似的那种

h021-01-22
共1条回复

0 学习 · 6815 问题

查看课程