高德地图定位api使用

高德地图定位api使用

<!--<script src='https://webapi.amap.com/maps?v=2.0&key=你的key'></script>-->
var amp = AMap.plugin('AMap.Geolocation', function () {
    geolocation = new AMap.Geolocation({
        // 是否使用高精度定位,默认:true
        enableHighAccuracy: true,
        // 设置定位超时时间,默认:无穷大
        timeout: 500
    });
    geolocation.getCurrentPosition(function (status, result) {
        //成功获取当前位置
        if (status == 'complete') {
            //定位信息
            location = [result.position.lng, result.position.lat];
            //清除原有定位
            var layer = map.getLayer("locationLayer");
            layer.clear();
            layer.setVisibility(true);
            //添加符号
            var pointSymbol;
            if (location != "") {
                var height = "32";
                var width = "32";
                var symbolImage = {
                    "url": ctx + "assets/images/map/location.png",
                    "width": width,
                    "height": height,
                    "angle": 0
                }
                pointSymbol = new PictureMarkerSymbol(symbolImage);
                var point = new Point(location[0], location[1]);
                var gra = new Graphic(point, pointSymbol);
                layer.add(gra);
                map.setZoom(12);
                //聚焦到定位点
                map.centerAt(point);
            }
        } else {
            onError(mui.alert("定位失败请返回重试!!"))
        }
    });

});
posted @ 2020-07-27 22:05  JaminYe  阅读(593)  评论(0编辑  收藏  举报