ionic 高德地图

定义一个高德地图的service,里面包含了ip定位到城市,另外一个地址搜索。
IP:http://restapi.amap.com/v3/ip
PLACE:http://restapi.amap.com/v3/place/text
这个key是从高德地图中注册的。

/**
 * Created by dzm on 2016/7/10.
 * 高德地图API
 */
services.service('GaoDeService',['$http','$q','ACCESS_URL','IonicStorageService','IonicNoticeService'
    ,function($http,$q,ACCESS_URL, IonicStorageService,IonicNoticeService){
    /**
     * 高德ip定位到城市
     */
    var location = function(){
        var curCity = IonicStorageService.getLocal('city');
        if (angular.isUndefined(curCity) || curCity) {
            $http({
                url:ACCESS_URL.GAODE.IP,
                method:'GET',
                params:{key:ACCESS_URL.GAODE.KEY,s:'rsv3',platform:"JS"}
            }).success(function(data){
                if(!angular.isUndefined(data.city)){
                    var city={id:data.adcode, name:data.city};
                    IonicStorageService.setLocal('city',city);
                }
            }).error(function(data){
                console.log("location error:"+data);
            });

        }
    }
    /**
     * 地址搜索
     * @param place
     */
    var placeSearch = function(place){
        var deferred = $q.defer();
        var city = IonicStorageService.getLocal(ACCESS_URL.SYSTEM.CITY);
        $http({
            url:ACCESS_URL.GAODE.PLACE,
            method:'GET',
            params:{key:ACCESS_URL.GAODE.KEY,s:'rsv3',platform:"JS",offset:5,page:1,keywords:place,city:city.id}
        }).success(function (result) {
            var addresses = [];
            if (result.info == 'OK'){
                result.pois.forEach(function (record, index) {
                    var locations = record.location.split(',');
                    addresses.push({name: record.name,longitude:locations[0], latitude:locations[1],adname:record.adname});
                });
                deferred.resolve(addresses);
            } else{
                IonicNoticeService.show(result.info);
                deferred.reject(addresses);
            }
        }).error(function (err) {
            // 提示异常信息
            if (err == null){
                IonicNoticeService.show("与服务器断开连接");
            } else{
                IonicNoticeService.show(err.info);
            }
            deferred.reject(err);
         })
        return deferred.promise;
    }
    return {
        placeSearch:placeSearch,
        location:location
    }
}])

你可能感兴趣的:(高德地图,ionic,angularjs)