mobilemode.api.lbs4amap_wev8.js 2.93 KB
define("mApi/lbs4amap", ["mUtil", "amap"], function(mUtil) {
    var callbackStack = [];
	
	return {
		getCurrPosition: function(callback, type){
			callbackStack.push(callback);
			function transformLL(longitude, latitude){
			    var parseResult = function(addr, addrComp){
			        callbackStack.forEach(function(cb) {
			            cb && cb({
	                        status: "1",
	                        errMsg: "",
	                        lng: longitude,//经度
	                        lat: latitude,//纬度
	                        addr: addr,//中文地址
	                        province: addrComp.province,//省份
	                        city: addrComp.city,//城市
	                        district: addrComp.district,//区
	                        street: addrComp.street,//街道
	                        streetNumber: addrComp.streetNumber//街道号码
	                    });
			        });
			        callbackStack = [];
			    };
			    AMap.plugin('AMap.Geocoder', function() {
                    var geocoder = new AMap.Geocoder();
                    geocoder.getAddress([longitude, latitude], function(status, rs) {
                        rs = rs.regeocode || {};
                        parseResult(rs.formattedAddress, rs.addressComponent || {});
                    })
                });
			}
			
			if(mUtil.checkEmpJsApi("getLocation")){//e9移动平台
				mUtil.invokeEmApi("getLocation", {
					//高德地图使用的是火星坐标
                    type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
                    success: function (res) {
                        transformLL(res.longitude, res.latitude);
					}
                });
            }else if(mUtil.runtime.isEmobile6()){//em6
        		window.getLBSResult = function(result){
        			var arr = result.split(",");
    				var longitude = arr[2];
    				var latitude = arr[1];
    				transformLL(longitude, latitude);
        		};
        		location = "emobile:gps:getLBSResult";
        	}else if(typeof(eb_GetLocation) != "undefined" && mUtil.isFunction(eb_GetLocation)){//云桥
        		window.getLBSResult = function(longitude, latitude){
        			transformLL(longitude, latitude);
        		};
        		eb_GetLocation("getLBSResult");
        	}else{//浏览器
        	    AMap.plugin('AMap.Geolocation', function() {
                    var geolocation = new AMap.Geolocation({
                        enableHighAccuracy: true//是否使用高精度定位,默认:true
                    });
                    geolocation.getCurrentPosition(function(status,result){
                        if(status === 'complete'){
                            transformLL(result.position.P, result.position.O);
                        }else{
                            mUtil.console.error(result.message);
                        }
                    });
                });
        	}
        }
	};
});