amapHelper.js 5.32 KB
define(['mUtil', 'amap'], function (mUtil) {
    return {
        /**
         * 将非高德坐标转换为高德坐标
         * 
         * @param transPoint经纬度数组 如(泛微gps坐标): [121.5222556500,31.0824170200]
         * @param type类型可以为 1腾讯/高德 2baidu、3gps等
         * */
        convertPoint: function(transPoint, type, callback){
            var convertType = "", type = Number(type);
            switch(type){
            case 2:
                convertType = "baidu";
                break;
            case 3:
                convertType = "gps";
                break;
            }
            if(convertType != ""){
                AMap.convertFrom(transPoint, convertType, function (status, result) {
                    if (result.info === 'ok') {
                        var resLnglat = result.locations[0];
                        callback && callback([resLnglat.lng, resLnglat.lat]);
                    }else{
                        mUtil.console.error("convert to GD coordinate failed.");
                    }
                });
            }else{
                callback && callback(transPoint);
            }
        },
        /**
         * 根据经纬度(高德)获取中文地址
         * */
        getAddress: function(longitude, latitude, callback){
            var parseResult = function(addr, addrComp){
                callback && callback({
                    status: "1",
                    errMsg: "",
                    lng: longitude,//经度
                    lat: latitude,//纬度
                    addr: addr,//中文地址
                    province: addrComp.province,//省份
                    city: addrComp.city,//城市
                    district: addrComp.district,//区
                    street: addrComp.street,//街道
                    streetNumber: addrComp.streetNumber//街道号码
                });
            };
            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 || {});
                })
            });
        },
        /**
         * 根据中文地址查询坐标
         * */
        placeSearch: function(addr, callback){
            AMap.plugin('AMap.PlaceSearch', function () {
                var placeSearch = new AMap.PlaceSearch();
                placeSearch.search(addr, function(status, result){
                    if(result.info === "OK"){
                        var poi = result.poiList.pois[0].location;
                        callback && callback([poi.lng, poi.lat]);
                    }else{
                        mUtil.getLabel(5297, "地址:#ADDR# 没有解析到结果!",function(msg){
                            mUtil.console.error(msg.replace("#ADDR#",addr));
                        });
                    }
                });
            });
        },
        /**
         * 搜索附近的点
         * */
        searchNearBy: function(point, radius, num, callback){
            AMap.plugin('AMap.PlaceSearch', function () {
                var placeSearch = new AMap.PlaceSearch({
                    pageSize: Number(num)
                });
                placeSearch.searchNearBy('', point, Number(radius) || 100, function(status, result){
                    var pois = result.info === "OK" ? result.poiList.pois : [];
                    callback && callback(pois.map(function(item){
                        return {
                            point: {
                                lng: item.location.lng,
                                lat: item.location.lat
                            },
                            title: item.name,
                            address: item.address
                        };
                    }));
                });
            });
        },
        /**
         * 将详细地址描述信息转换成地理坐标
         */
        getPosition: function(addr, callback){
            var isMatchedAddress = function(addr, formmatedAddr){
                var j = 0;
                for(var i = 0; i < addr.length; i++){
                    if(~formmatedAddr.indexOf(addr.charAt(i))){
                        j++;
                        if(j >= formmatedAddr.length / 2)  return true;
                    }
                }
                return false;
            }
            AMap.plugin('AMap.Geocoder', function(){
                var geocoder = new AMap.Geocoder();
                geocoder.getLocation(addr, function(status, result){
                    if(status === 'complete' && result.geocodes.length){
                        var formattedAddress = result.geocodes[0].formattedAddress;
                        if(!isMatchedAddress(addr, formattedAddress)){
                            return;
                        }
                        var poi = result.geocodes[0].location;
                        callback && callback([poi.lng, poi.lat]);
                    }else{
                        mUtil.getLabel(5297, "地址:#ADDR# 没有解析到结果!",function(msg){
                            mUtil.console.error(msg.replace("#ADDR#",addr));
                        });
                    }
                });
            });
        }
    }
});