zoukankan      html  css  js  c++  java
  • 高德地图定位

    <div id="allmap" style="height:300px"></div>
    <input type="hidden" id="lng" name="lng" value="" />
    <input type="hidden" id="lat" name="lat" value="" />
    <script type="text/javascript">
    var map, geolocation;
    //加载地图,调用浏览器定位服务
    map = new AMap.Map('allmap', {
    resizeEnable: true,
    view: new AMap.View2D({
    resizeEnable: true,
    zoom:50//地图显示的缩放级别
    }),
    keyboardEnable:false
    });

    map.plugin(["AMap.ToolBar"],function(){
    toolBar = new AMap.ToolBar();
    map.addControl(toolBar);
    toolBar.show();
    toolBar.hideRuler();
    toolBar.hideDirection();
    });
    map.plugin(["AMap.MapType"],function(){
    //地图类型切换
    mapType= new AMap.MapType({defaultType:0,showRoad:true});
    map.addControl(mapType);
    });




    map.plugin('AMap.Geolocation', function() {
    geolocation = new AMap.Geolocation({
    enableHighAccuracy: true,//是否使用高精度定位,默认:true
    timeout: 10000, //超过10秒后停止定位,默认:无穷大
    buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
    zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
    buttonPosition:'LB'
    });
    map.addControl(geolocation);
    geolocation.getCurrentPosition();
    AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
    AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
    });
    //解析定位结果
    function onComplete(data) {
    var lon = data.position.getLng();
    var lat = data.position.getLat();
    new AMap.Marker({
    map: map,
    position: [lon,lat],
    icon: new AMap.Icon({
    size: new AMap.Size(19, 33), //图标大小
    image: "/image/mark_bs.png",

    })
    });
    map.setZoomAndCenter(20, [lon,lat]);
    var clickEventListener=AMap.event.addListener(map,'click',function(e){
    map.clearMap();
    new AMap.Marker({
    position: e.lnglat,
    map: map

    });

    document.getElementById("lng").value=e.lnglat.getLng();
    document.getElementById("lat").value=e.lnglat.getLat();
    });

    }
    //解析定位错误信息
    function onError(data) {
    document.getElementById('tip').innerHTML = '定位失败';
    }


    </script>
  • 相关阅读:
    Difference Between Arraylist And Vector : Core Java Interview Collection Question
    Man's Best Friend: The Science Behind the Dog and Human Relationship
    我在微软那些事--微软面试
    北美PM活着的攻略
    C#图解教程 第二十一章 命名空间和程序集
    C#图解教程 第二十章 异步编程
    C#图解教程 第十九章 LINQ
    C#图解教程 第十八章 枚举器和迭代器
    C#图解教程 第十七章 泛型
    C#图解教程 第十六章 转换
  • 原文地址:https://www.cnblogs.com/godLike7/p/5807677.html
Copyright © 2011-2022 走看看