注册账号并申请key
高德开发平台
常见问题
温馨提示:Google浏览器调试过程中需要开启HTTPS服务,否则可能会出现一直定位失败的问题,详细解释参考官方详细说明
官方定位示例链接
官方常见问题链接
以下代码引入了jquery
<!--引入高德地图定位能力--><script type="text/javascript"src="/maps?v=1.3&key=这里用你自己申请的key&plugin=AMap.Autocomplete,AMap.PlaceSearch,AMap.Geocoder"></script><!--引入jquery--><script type="text/javascript" src="plugin/jquery-3.4.1.min.js"></script>
var map, geolocation;// 获取经纬度function data() {console.log("自动获取定位")map = new AMap.Map('container');map.plugin('AMap.Geolocation', function () {geolocation = new AMap.Geolocation({enableHighAccuracy: true,//是否使用高精度定位,默认:truetimeout: 10000,//超过10秒后停止定位,默认:无穷大maximumAge: 0, //定位结果缓存0毫秒,默认:0GeoLocationFirst: true//功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false});map.addControl(geolocation);geolocation.getCurrentPosition();AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息});}var myplace = ''; //我的位置var mycity = ''; //我所在的城市//解析定位结果function onComplete(data) {var str = ['定位成功'];// $(".zqxf01").text(data.position.getLng() + "aaa" + data.position.getLat())str.push('经度:' + data.position.getLng());str.push('纬度:' + data.position.getLat());getPositionByLonLats2(data.position.getLng(), data.position.getLat());}function getPositionByLonLats2(lat, lng) {var location = lat + "," + lng;$.get({url: "/v3/geocode/regeo?parameters",dataType: "json",data: {key: "这里用你自己申请的key", location: location},success: function (data) {console.log('aaa' + data)//alert(JSON.stringify(data))var message = JSON.stringify(data);var d = JSON.parse(message);if (d.status == 1) {//高德文档地址:/api/webservice/guide/api/georegeo//district:区//存储省级信息var address = d.regeocode.addressComponent.province//district;//存储当前选的地址(自动或者手动)localStorage.setItem("province", address);selectShop();//自动定位存储定位信息sessionStorage.setItem("locationAddressSession", address);$("#locationCity").html(address);} else {$("#locationCity").html("上海");//默认上海}$('.toast').css({display: 'none'})}})}