高德地图API的 Demo定位没显示出来

出现的问题

AMap.CitySearch方法返回不了城市信息,代码用的高德演示DEMO的

参考的https://lbs.amap.com/demo/jsapi-v2/example/location/get-city-name-by-ip-location

自己做出来的图没有定位

img

代码

<!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=2.0&key=e5884ecbf77744538749c105d17d5303&plugin=AMap.CitySearch"></script>
<script type="text/javascript">
    var map = new AMap.Map("container", {
        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>

img

题主可能没有添加安全密钥

你是在电脑运行,还是手机端测试呢?

应该是 定位 没开启吧

定位需要你填写在官网申请的key的

1、需要在高德地图上申请开发者key
2、手机与电脑都需要开启定位功能
3、手机浏览器也需要授权定位功能