起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5010|回复: 0

[分享] 关于用geolocation定位我有话说

[复制链接]

30

主题

78

帖子

346

积分

中级会员

Rank: 3Rank: 3

积分
346
发表于 2019-8-3 18:39:45 | 显示全部楼层 |阅读模式
本帖最后由 madmodo 于 2019-8-3 18:39 编辑

这次用geolocation.getCurrentPosition踩了几个坑

申请百度ak的时候,用的sha1码,不是有人说的在首选项->android->build里的SHA1
而是 native目录对应app目录中有一个android.keystore.info文中有sha1,从这里获取sha1进行百度ak申请

用这个插件geolocation.getCurrentPosition,必须要选上baiduMapBase这个插件
安卓app从这个方法获取到的经纬可以直接用于百度地图的网络定位,而ios获取的经纬度需要转换一下/* 定位当前位置 */
        Model.prototype.positionClick = function(event) {
                var _this = this;
                var currentTaksData = this.comp("currentTaksData");
                function onSuccess(position) {
                        var option = {
                                latitude : position.coords.latitude,
                                longitude : position.coords.longitude
                        };
                        _this.getAddress('address', option, function(data, flag, msg) {
                                if (flag) {
                                        var nodeData = _this.comp("nodeData");
                                        var recordData = _this.comp("recordData");
                                        nodeData.setValue("fSignAddress", data);
                                        nodeData.setValue("fSignCoordinate", position.coords.longitude + "," + position.coords.latitude);
                                        nodeData.saveData();
                                        var param = new biz.Request.ActionParam();
                                        param.setParam("taskID", taskid);
                                        param.setParam("nodeID", nodeData.getCurrentRowID());
                                        commonUtils.ajax(param, "nodeFinishAction", _this.getContext(), function(results) {
                                                if (results.state) {
                                                        justep.Util.hint("签到完成!");
                                                        recordData.refreshData({
                                                                "onSuccess" : function() {
//                                                                        var fNodeName = recordData.getValue("fNodeName");
//                                                                        var fPhoneContent = recordData.getValue("fPhoneContent");
//                                                                        var fExecutorTime = recordData.getValue("fExecutorTime");
//                                                                        var fExecutor = recordData.getValue("fExecutor");
//                                                                        currentTaksData.setValue("fNodeName", fNodeName);
//                                                                        currentTaksData.setValue("fPhoneContent", fPhoneContent);
//                                                                        currentTaksData.setValue("fExecutorTime", fExecutorTime);
//                                                                        currentTaksData.setValue("fExecutor", fExecutor);
//                                                                        $("#currentState").html("已处理");
//                                                                        $("#currentState").css("color", "#999999");
                                                                        $(_this.getElementByXid("list1")).css("display", "none");
                                                                        $(".confirm-div").css("display", "none");
                                                                }
                                                        });
                                                } else {
                                                        justep.Util.hint("签到失败!");
                                                }
                                        })
                                } else {
                                        justep.Util.confirm("定位失败,是否继续签到!", function() {
                                                var nodeData = _this.comp("nodeData");
                                                var recordData = _this.comp("recordData");
                                                nodeData.saveData();
                                                var param = new biz.Request.ActionParam();
                                                param.setParam("taskID", taskid);
                                                param.setParam("nodeID", nodeData.getCurrentRowID());
                                                commonUtils.ajax(param, "nodeFinishAction", _this.getContext(), function(results) {
                                                        if (results.state) {
                                                                justep.Util.hint("签到完成!");
                                                                recordData.refreshData({
                                                                        "onSuccess" : function() {
                                                                                var fNodeName = recordData.getValue("fNodeName");
                                                                                var fPhoneContent = recordData.getValue("fPhoneContent");
                                                                                var fExecutorTime = recordData.getValue("fExecutorTime");
                                                                                var fExecutor = recordData.getValue("fExecutor");
                                                                                currentTaksData.setValue("fNodeName", fNodeName);
                                                                                currentTaksData.setValue("fPhoneContent", fPhoneContent);
                                                                                currentTaksData.setValue("fExecutorTime", fExecutorTime);
                                                                                currentTaksData.setValue("fExecutor", fExecutor);
                                                                                $("#currentState").html("已处理");
                                                                                $("#currentState").css("color", "#999999");
                                                                                $(_this.getElementByXid("list1")).css("display", "none");
                                                                                $(".confirm-div").css("display", "none");
                                                                        }
                                                                });
                                                                // justep.Shell.closePage();
                                                        } else {
                                                                justep.Util.hint("签到失败!");
                                                        }
                                                })
                                        }, null)
                                }
                        });
                }
                function onError(data) {
                        justep.Util.confirm("定位失败,是否继续签到!", function() {
                                var nodeData = _this.comp("nodeData");
                                var recordData = _this.comp("recordData");
                                nodeData.saveData();
                                var param = new biz.Request.ActionParam();
                                param.setParam("taskID", taskid);
                                param.setParam("nodeID", nodeData.getCurrentRowID());
                                commonUtils.ajax(param, "nodeFinishAction", _this.getContext(), function(results) {
                                        if (results.state) {
                                                justep.Util.hint("签到完成!");
                                                recordData.refreshData({
                                                        "onSuccess" : function() {
                                                                var fNodeName = recordData.getValue("fNodeName");
                                                                var fPhoneContent = recordData.getValue("fPhoneContent");
                                                                var fExecutorTime = recordData.getValue("fExecutorTime");
                                                                var fExecutor = recordData.getValue("fExecutor");
                                                                currentTaksData.setValue("fNodeName", fNodeName);
                                                                currentTaksData.setValue("fPhoneContent", fPhoneContent);
                                                                currentTaksData.setValue("fExecutorTime", fExecutorTime);
                                                                currentTaksData.setValue("fExecutor", fExecutor);
                                                                $("#currentState").html("已处理");
                                                                $("#currentState").css("color", "#999999");
                                                                $(_this.getElementByXid("list1")).css("display", "none");
                                                                $(".confirm-div").css("display", "none");
                                                        }
                                                });
                                                // justep.Shell.closePage();
                                        } else {
                                                justep.Util.hint("签到失败!");
                                        }
                                })
                        }, null)
                }
                var geo_options = {
                        enableHighAccuracy : true,
                        maximumAge : 30000,
                        timeout : 27000
                };
                _this.getLocation(onSuccess, onError, geo_options);
        };


Model.prototype.getLocation = function(onsuccess, onerror,geo_options) {
                if (window.plugins) {
                         navigator.geolocation.getCurrentPosition(function(r) {
                                var lon = r['coords']['longitude'];
                                var lat = r['coords']['latitude'];
                                if (justep.Browser.isIOS || justep.Browser.isIphone) {
                                        var gpsPoint = new baiduMap.Point(lon, lat);
                                        // 原始坐标转换为百度坐标(一定要等待地图加载完成之后才能进行)
                                        var convertor = new baiduMap.Convertor();
                                        var pointArr = [];
                                        pointArr.push(gpsPoint);
                                        // 开始转换坐标
                                        convertor.translate(pointArr, 1, 5, function translateCallback(data) {
                                                lon = data.points[0].lng;// 转换后的百度坐标系经度
                                                lat = data.points[0].lat;// 转换后的百度坐标系纬度
                                                var postion = {
                                                        coords : {
                                                                latitude : lat,
                                                                longitude : lon
                                                        }
                                                };
                                                onsuccess && onsuccess(postion);
                                        });
                                } else {
                                        var postion = {
                                                coords : {
                                                        latitude : lat,
                                                        longitude : lon
                                                }
                                        };
                                        onsuccess && onsuccess(postion);
                                }
                        }, onerror, geo_options);
                } else {
                        var geolocation = new baiduMap.Geolocation();
                        geolocation.getCurrentPosition(function(r) {
                                if (this.getStatus() == 0) {
                                        var postion = {
                                                point : r['point'],
                                                coords : {
                                                        latitude : r['point']['lat'],
                                                        longitude : r['point']['lng']
                                                }
                                        };
                                        onsuccess && onsuccess(postion);
                                } else {
                                        onerror && onerror(this.getStatus());
                                }
                        }, geo_options);
                }
        };
Model.prototype.getAddress = function(type, option, callback) {
                if (!option['latitude'] || !option['longitude']) {
                        justep.Util.hint("定位失败!");
                        return;
                }
                var url = 'https://api.map.baidu.com/geocoder/v2/?ak=YE5s4jumXrGHlnSh3qj85VrPNyCTtDkZ&location=' + option['latitude'] + ',' + option['longitude'] + '&output=json&pois=0';
                $.ajax({
                        url : url,
                        dataType : 'jsonp',
                        processData : false,
                        type : 'get',
                        success : function(data) {
                                var result = data['result'];
                                if (!result) {
                                        return callback && callback(null, false, "获取位置信息失败!");
                                }
                                var address = "";
                                try {
                                        address += result.formatted_address;
                                        address += result.sematic_description;
                                } catch (e) {
                                        address = "";
                                }
                                if (type === "address") {
                                        callback && callback(address, true, "success");
                                } else if (type === "region") {
                                        callback && callback({
                                                address : address,
                                                other : result['addressComponent']
                                        }, true, "success");
                                }
                        },
                        error : function(XMLHttpRequest, textStatus, errorThrown) {
                                callback && callback(null, false, "获取位置信息失败!");
                        }
                });
        };






您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-4-19 21:54 , Processed in 0.055228 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表