(function (app) {
    app.factory("map", ["$q", "$http", function ($q, $http) {
        var key = '9f832e163baac0a04d437a52dd3e5d29';
        return {
            //获取高德地图图片
            getMapPic: function (input) {
                var deferred = $q.defer();
                var defaultPath = "img/demo/defaultmap.png";
                if (!input) return defaultPath;
                var arr = input.split(',');
               
                var keywords = arr[arr.length - 1];
                var url = 'http://restapi.amap.com/v3/place/text?key=' + key + '&keywords=' + keywords + "&callback=JSON_CALLBACK";
                $http.jsonp(url).then(function (result) {
                    var p = result.data.pois[0];
                    if (p) {
                        var l = p.location;
                        var m = "http://restapi.amap.com/v3/staticmap?location=" + l + "&zoom=9&size=360*160&markers=mid,,A:" + l + "&key=" + key;
                        deferred.resolve(m);
                    } else {
                        deferred.resolve(defaultPath);
                    }
                }, function (erro) {
                    deferred.reject(erro);
                });
                return deferred.promise;
            },
            //通过ip获取地址
            getLocation: function () {
                
                //http://restapi.amap.com/v3/ip?key=9f832e163baac0a04d437a52dd3e5d29
                var deferred = $q.defer();
                var url = "http://restapi.amap.com/v3/ip?key=" + key + "&callback=JSON_CALLBACK";
                $http.jsonp(url).then(function (result) {
                    deferred.resolve(result.data);
                }, function (erro) {
                    deferred.reject(erro);
                });
                ////http://lbs.amap.com/api/javascript-api/reference/plugin/#m_AMap.Geolocation
                //var map, geolocation;
                ////加载地图，调用浏览器定位服务
                //map = new AMap.Map('container', {
                //    resizeEnable: true
                //});
                //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: 'RB'
                //    });
                //    map.addControl(geolocation);
                //    geolocation.getCurrentPosition();
                //    AMap.event.addListener(geolocation, 'complete', function (data) {
                //        debugger;
                //        //返回定位信息
                //        var str = ['定位成功'];
                //        str.push('经度：' + data.position.getLng());
                //        str.push('纬度：' + data.position.getLat());
                //        str.push('精度：' + data.accuracy + ' 米');
                //        str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
                //        deferred.resolve(str);
                //    });
                //    AMap.event.addListener(geolocation, 'error', function (erro) {  //返回定位出错信息
                //        deferred.reject(erro);
                //    });
                //});
                return deferred.promise;
            }
        }
    }]);
})(angular.module('app.services'));