拿来就能用的可拖拽的高德定位例子

2019-07-09  本文已影响0人  咩咩籽

把工作中用到的存储起来,备忘


2019-07-09_092243.png

<html >

<head>

    <!-- 原始地址://webapi.amap.com/ui/1.0/ui/misc/PositionPicker/examples/positionPicker.html -->

    <base href="https://webapi.amap.com/ui/1.0/ui/misc/PositionPicker/examples/" />

    <meta charset="utf-8">

    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">

    <title>拖拽选址</title>

    <script type="text/javascript" src='https://webapi.amap.com/maps?v=1.4.14&key=你自己的key'></script>

    <!-- UI组件库 1.0 -->

    <script src="https://webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>

    <script type="text/javascript">

        AMapUI.loadUI(['misc/PositionPicker'], function(PositionPicker) {

            var map = new AMap.Map('container', {

                zoom: 16,

                scrollWheel: false

            });

            map.plugin(['AMap.Geolocation','AMap.ToolBar'], function () {

                var toolbar = new AMap.ToolBar();

                map.addControl(toolbar);

                var geolocation = new AMap.Geolocation({

                    enableHighAccuracy: true, // 是否使用高精度定位,默认:true

                    timeout: 5000,          // 超过5秒后停止定位,默认:无穷大

                    maximumAge: 0,            // 定位结果缓存0毫秒,默认:0

                    convert: true,            // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true

                    showButton: true,        // 显示定位按钮,默认:true

                    buttonPosition: 'LB',    // 定位按钮停靠位置,默认:'LB',左下角

                    buttonOffset: new AMap.Pixel(10, 20), // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)

                    showMarker: true,        // 定位成功后在定位到的位置显示点标记,默认:true

                    showCircle: true,        // 定位成功后用圆圈表示定位精度范围,默认:true

                    panToLocation: true,      // 定位成功后将定位到的位置作为地图中心点,默认:true

                    zoomToAccuracy:true      // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false

                });

                map.addControl(geolocation);

                geolocation.getCurrentPosition();

                AMap.event.addListener(geolocation, 'complete', onComplete); // 返回定位信息

                AMap.event.addListener(geolocation, 'error', onError);      // 返回定位出错信息

            });

            var positionPicker = new PositionPicker({

                mode: 'dragMap',

                map: map

            });

            function onComplete(obj){

                console.log("成功");

            }

            function onError(obj) {

                console.log("失败");

            }

            positionPicker.on('success', function(positionResult) {

                console.log(positionResult);

                var sheng=positionResult.regeocode.addressComponent.province;

                var shi=positionResult.regeocode.addressComponent.city;

                var qx=positionResult.regeocode.addressComponent.district;

                var jd=positionResult.regeocode.addressComponent.township+

                    positionResult.regeocode.addressComponent.street+

                    positionResult.regeocode.addressComponent.streetNumber;

                console.log(sheng+shi+qx+jd);

            });

            positionPicker.on('fail', function(positionResult) {

                console.log(positionResult);

            });

            positionPicker.start();

                    map.panBy(0, 1);

                    map.addControl(new AMap.ToolBar({

                        liteStyle: true

                    }))

        });

    </script>

    <style>

    html,

    body {

        height: 100%;

        margin: 0;

        width: 100%;

        padding: 0;

        overflow: hidden;

        font-size: 13px;

    }

    .map {

        height: 100%;

        width: 100%;

        float: left;

    }

    </style>

</head>

<body>

    <div id="container" class="map" tabindex="0"></div>

</body>

</html>
上一篇下一篇

猜你喜欢

热点阅读