ionic + 高德地图定位
2016-03-12 13:40
337 查看
代码一:
var AMapArea=document.getElementById('amap'); AMapArea.parentNode.style.height="100%"; //$scope.AMapId='container'; $scope.mapObj;//存放初始化的地图对象 //$scope.initAMap=function(){ var position=new AMap.LngLat(116.397428,39.90923); $scope.mapObj=new AMap.Map("container",{ resizeEnable: true, zoom: 14, center: [116.39,39.9] }); $scope.mapObj.plugin(['AMap.Geolocation'], function () { var geolocation = new AMap.Geolocation({ enableHighAccuracy: true,//是否使用高精度定位,默认:true timeout: 10000, //超过10秒后停止定位,默认:无穷大 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 }); $scope.mapObj.addControl(geolocation); AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息 AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息 }); function onComplete(dt) { var position=dt.position; var _latitude = position.getLat(); var _longitude = position.getLng(); $scope.mapObj.setCenter([_longitude,_latitude]); console.log(_latitude,_longitude); }代码二:
function getLocation() { var deferred = $q.defer(); if (!navigator.geolocation) { console.log("不支持"); return; } console.log("start");; navigator.geolocation.getCurrentPosition(function (position) { var _locationGaps = []; console.log("ok"); var _latitude = position.coords.latitude; var _longitude = position.coords.longitude; $scope.mapObj.setCenter([_longitude,_latitude]); console.log(_latitude,_longitude); }, function (error) { switch(error.code) { case error.PERMISSION_DENIED: console.log("User denied the request for Geolocation."); break; case error.POSITION_UNAVAILABLE: console.log("Location information is unavailable."); break; case error.TIMEOUT: console.log("The request to get user location timed out."); break; case error.UNKNOWN_ERROR: console.log("An unknown error occurred."); break; } // 如果获取经纬度时出错, 提示错误 //toaster.pop('error','无法获取您的地理位置','请检查您是否禁止本应用获取您的当前位置'); console.log('无法获取您的地理位置','请检查您是否禁止本应用获取您的当前位置'); deferred.reject(error); }); return deferred.promise; };代码三:
function getLocation1() { navigator.geolocation.getCurrentPosition(onSuccess, onError, { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }); function onSuccess(position) { var lat = position.coords.latitude; var long = position.coords.longitude; $scope.mapObj.setCenter([lat,long]); console.log(lat,long); //appvalues.showmessage("已经成功定位."); } function onError(err) { //appvalues.showmessage("无法获取地理位置."); console.log("无法获取地理位置."); console.log(JSON.stringify(err)); return; } //获取地理位置 /*document.addEventListener("deviceready", function (success) { var posOptions = { timeout: 5000, enableHighAccuracy: false }; $cordovaGeolocation .getCurrentPosition(posOptions) .then(function (position) { var lat = position.coords.latitude; var long = position.coords.longitude; $scope.mapObj.setCenter([lat,long]); console.log(lat,long); //appvalues.showmessage("已经成功定位."); }, function (err) { //appvalues.showmessage("无法获取地理位置."); console.log("无法获取地理位置."); console.log(JSON.stringify(err)); return; }); }, false);*/ };
相关文章推荐
- 第一次上机实验-2
- event 事件 clientX 和clientY 配合scrollTop使用, div跟着鼠标走
- 使用基于ThinkPHP3.2.3的ThinkAdmin创建手机电脑通用的表白墙(六)表白页面
- day13—HTML DOM编程
- CodeForces - 598A Tricky Sum (快速幂)
- 关于java开发技巧
- Backbone源码分析-Backbone架构+流程图
- SPRING循环依赖(circular reference)的解决方法
- 欢迎使用CSDN-markdown编辑器
- spring security执行流程图
- 面试题3 ----二维数组中的查找
- 重复登录Windows远程桌面-Autoit脚本
- 【红宝书笔记】一点关于JS的垃圾回收机制
- Python Multinomial Logistics 实现MNIST分类
- 浅谈ThreadLocal
- 一种虚拟机软件更新方法
- JAVA内置数据结构概述
- JAVA内置数据结构概述
- JAVA内置数据结构概述
- JAVA内置数据结构概述