Exemplo n.º 1
0
 public GeoCoderStatusCode GetPlacemarksByKeywords(string keywords, string region, string rectangle,
                                                   QueryProgressDelegate queryProgressEvent, out List <Placemark> placemarkList, ref int count)
 {
     this.succeedCount = 0;
     placemarkList     = this.GetPlacemarksByKeywords(keywords, region, rectangle, 1, queryProgressEvent, ref count);
     return(GeoCoderStatusCode.G_GEO_SUCCESS);
 }
Exemplo n.º 2
0
        internal Task SubmitQueryAsync(Query query,
                                       QueryProgressDelegate queryProgressDelegate,
                                       CancellationToken cancellationToken = default(CancellationToken))
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            return(Task.Run(() =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var componentFilter = query.ComponentFilter.Count > 0
                    ? (IReadOnlyComponentFilter) new ReadOnlyComponentFilter(query.ComponentFilter)
                    : FullAccessComponentFilter.Instance;

                using (var e = volatileDatabaseItems.GetEnumerator(cancellationToken))
                {
                    foreach (var entity in e.AsEnumerable().Where(i => i.HasComponents(query.ComponentFilter)))
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return;
                        }
                        var readOnlyEntity = entity.AsReadOnly(componentFilter);
                        if (!query.Filter(readOnlyEntity, QueryHandler))
                        {
                            continue;
                        }
                        queryProgressDelegate?.Invoke(new QueryResult(readOnlyEntity));
                    }
                }
            }, cancellationToken));
        }
Exemplo n.º 3
0
        private List <Placemark> GetPlacemarksByKeywords(string keywords, string region, string rectangle,
                                                         int pageIndex, QueryProgressDelegate queryProgressEvent, ref int totalCount)
        {
            List <Placemark> list   = new List <Placemark>();
            int    pageSize         = 50;
            string keyWordUrlEncode = HttpUtility.UrlEncode(keywords);
            string format           = "http://restapi.amap.com/v3/place/text?&keywords={0}&city={1}&key={2}&output=json&offset={3}&page={4}";

            //http://restapi.amap.com/v3/place/text?&keywords=%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6&city=beijing&output=json&offset=5&page=1&key=26144cb5dbe74ea6c1410777feb646de&extensions=base
            if (!string.IsNullOrEmpty(region))
            {
                format = string.Format(format, new object[] { keyWordUrlEncode, HttpUtility.UrlEncode(region), KEY, pageSize, pageIndex });
            }
            //Get Cache Json Result if exist
            //string cacheUrl = string.Format("http://restapi.amap.com/v3/place/text/{0}/{1}/{2}/{3}", new object[] { keyWordUrlEncode, HttpUtility.UrlEncode(region), pageIndex, pageSize });
            //string cacheResult = Singleton<Cache>.Instance.GetContent(cacheUrl, CacheType.UrlCache, TimeSpan.FromHours(360.0));
            //if (string.IsNullOrEmpty(cacheResult))
            //{
            //    cacheResult = HttpUtil.RequestByJSON(format, "get", "");
            //    if (!string.IsNullOrEmpty(cacheResult))
            //    {
            //        Singleton<Cache>.Instance.SaveContent(cacheUrl, CacheType.UrlCache, cacheResult);
            //    }
            //}
            //if (string.IsNullOrEmpty(cacheResult))
            //{
            //    return list;
            //}
            try
            {
                string  cacheResult = HttpUtil.GetData(format);
                JObject jsonResult  = JObject.Parse(cacheResult);
                string  info        = (string)jsonResult["info"];
                if (info == "OK")
                {
                    if (pageIndex == 1)
                    {
                        string countStr = (string)jsonResult["count"];
                        totalCount = int.Parse(countStr);
                    }
                    if (totalCount <= 0)
                    {
                        return(list);
                    }

                    JArray results = (JArray)jsonResult["pois"];
                    if (results != null && results.Count > 0)
                    {
                        for (int i = 0; i < results.Count; ++i)
                        {
                            JObject  obj      = results[i] as JObject;
                            string   name     = obj["name"].ToString();
                            string   address  = obj["address"].ToString();
                            string   location = obj["location"].ToString();
                            string[] points   = location.Split(',');
                            if (points != null && points.Length == 2)
                            {
                                double    lat  = double.Parse(points[1]);
                                double    lng  = double.Parse(points[0]);
                                Placemark item = new Placemark(address);
                                item.Point = new PointLatLng(lat, lng);
                                item.Name  = name;
                                //item.ProvinceName = obj["pname"].ToString();
                                //item.CityName = obj["cityname"].ToString();
                                //item.Category = obj["type"].ToString();
                                list.Add(item);
                                ++this.succeedCount;
                                if (queryProgressEvent != null)
                                {
                                    queryProgressEvent((long)this.succeedCount, (long)totalCount);
                                }
                            }
                        }
                    }
                    int allPageNum = (int)Math.Ceiling((double)(((double)totalCount) / ((double)pageSize)));
                    if (pageIndex < allPageNum)
                    {
                        list.AddRange(this.GetPlacemarksByKeywords(keywords, region, rectangle, pageIndex + 1, queryProgressEvent, ref totalCount));
                    }
                }
            }
            catch (Exception ex)
            {
                log.Warn(ex.Message);
            }
            return(list);
        }
Exemplo n.º 4
0
        private List <Placemark> GetPlacemarksByKeywords(string keywords, string region, string rectangle,
                                                         int pageIndex, QueryProgressDelegate queryProgressEvent, ref int totalCount)
        {
            List <Placemark> list   = new List <Placemark>();
            int    pageSize         = 20;
            string keyWordUrlEncode = HttpUtility.UrlEncode(keywords);
            string format           = "http://api.map.baidu.com/place/v2/search?q={0}&region={1}&output=json&ak={2}&page_size={3}&page_num={4}&scope=1";

            //"http://api.map.baidu.com/place/v2/search?ak=您的密钥&output=json&query=%E9%93%B6%E8%A1%8C&page_size=10&page_num=0&scope=1&region=%E5%8C%97%E4%BA%AC"
            if (!string.IsNullOrEmpty(region))
            {
                format = string.Format(format, new object[] { keyWordUrlEncode, HttpUtility.UrlEncode(region), KEY, pageSize, pageIndex });
            }
            //Get Cache Json Result if exist
            //string cacheUrl = string.Format("http://api.map.baidu.com/place/v2/search/{0}/{1}/{2}/{3}", new object[] { keyWordUrlEncode, HttpUtility.UrlEncode(region), pageIndex, pageSize });
            //string cacheResult = Singleton<Cache>.Instance.GetContent(cacheUrl, CacheType.UrlCache, TimeSpan.FromHours(360.0));
            //if (string.IsNullOrEmpty(cacheResult))
            //{
            //    cacheResult = HttpUtil.GetData(format);
            //    if (!string.IsNullOrEmpty(cacheResult))
            //    {
            //        Singleton<Cache>.Instance.SaveContent(cacheUrl, CacheType.UrlCache, cacheResult);
            //    }
            //}
            //if (string.IsNullOrEmpty(cacheResult))
            //{
            //    return list;
            //}
            try
            {
                string  cacheResult = HttpUtil.GetData(format);
                JObject jsonResult  = JObject.Parse(cacheResult);
                string  message     = (string)jsonResult["message"];
                if (message == "ok")
                {
                    if (pageIndex == 0)
                    {
                        totalCount = int.Parse((string)jsonResult["total"]);
                    }
                    if (totalCount <= 0)
                    {
                        return(list);
                    }

                    JArray results = (JArray)jsonResult["results"];
                    if (results != null && results.Count > 0)
                    {
                        for (int i = 0; i < results.Count; ++i)
                        {
                            JObject   obj     = results[i] as JObject;
                            string    name    = obj["name"].ToString();
                            string    address = obj["address"].ToString();
                            double    lat     = double.Parse(obj["location"]["lat"].ToString());
                            double    lng     = double.Parse(obj["location"]["lng"].ToString());
                            Placemark item    = new Placemark(address);
                            item.Point = new PointLatLng(lat, lng);
                            item.Name  = name;
                            list.Add(item);
                            ++this.succeedCount;
                            if (queryProgressEvent != null)
                            {
                                queryProgressEvent((long)this.succeedCount, (long)totalCount);
                            }
                        }
                    }
                    int allPageNum = (int)Math.Ceiling((double)(((double)totalCount) / ((double)pageSize)));
                    if (pageIndex < allPageNum)
                    {
                        list.AddRange(this.GetPlacemarksByKeywords(keywords, region, rectangle, pageIndex + 1, queryProgressEvent, ref totalCount));
                    }
                }
            }
            catch (Exception ex)
            {
                log.Warn(ex.Message);
            }
            return(list);
        }
Exemplo n.º 5
0
        private List <Placemark> GetPlacemarksByKeywords(string keywords, string region, string rectangle,
                                                         string nearby, int pageIndex, QueryProgressDelegate queryProgressEvent, ref int count)
        {
            List <Placemark> list   = new List <Placemark>();
            int    pageSize         = 20;
            string keyWordUrlEncode = HttpUtility.UrlEncode(keywords);
            string format           = "http://apis.map.qq.com/ws/place/v1/search?keyword={0}&boundary={5}({1})&page_size={2}&page_index={3}&key={4}";

            if (!string.IsNullOrEmpty(region))
            {
                format = string.Format(format, new object[] { keyWordUrlEncode, HttpUtility.UrlEncode(region) + ",0", pageSize, pageIndex, KEY, "region" });
            }
            else if (!string.IsNullOrEmpty(rectangle))
            {
                format = string.Format(format, new object[] { keyWordUrlEncode, rectangle, pageSize, pageIndex, KEY, "rectangle" });
            }
            else if (!string.IsNullOrEmpty(nearby))
            {
                format = string.Format(format, new object[] { keyWordUrlEncode, nearby, pageSize, pageIndex, KEY, "nearby" });
            }
            //string cacheUrl = string.Format("http://apis.map.qq.com/{0}/{1}/{2}/{3}/{4}", new object[] { keyWordUrlEncode, HttpUtility.UrlEncode(region), rectangle, nearby, pageIndex });
            //string cacheResult = Singleton<Cache>.Instance.GetContent(cacheUrl, CacheType.UrlCache, TimeSpan.FromHours(360.0));
            //if (string.IsNullOrEmpty(cacheResult))
            //{
            //    cacheResult = HttpUtil.GetData(format, "utf-8", "get", "", "text/htm");
            //    Thread.Sleep(200);
            //    if (!string.IsNullOrEmpty(cacheResult))
            //    {
            //        Singleton<Cache>.Instance.SaveContent(cacheUrl, CacheType.UrlCache, cacheResult);
            //    }
            //}
            //if (string.IsNullOrEmpty(cacheResult))
            //{
            //    return list;
            //}
            string  cacheResult = HttpUtil.GetData(format);
            JObject result      = JObject.Parse(cacheResult);
            string  status      = (string)result["status"];
            string  message     = (string)result["message"];

            if (message == "query ok")
            {
                if (pageIndex == 1)
                {
                    int.TryParse(result["count"].ToString(), out count);
                }
                if (count <= 0)
                {
                    return(list);
                }

                JArray data = (JArray)result["data"];
                if (data != null && data.Count >= 0)
                {
                    for (int i = 0; i < data.Count; ++i)
                    {
                        JObject   obj     = data[i] as JObject;
                        string    name    = obj["title"].ToString();
                        string    address = obj["address"].ToString();
                        double    lat     = double.Parse(obj["location"]["lat"].ToString());
                        double    lng     = double.Parse(obj["location"]["lng"].ToString());
                        Placemark item    = new Placemark(address);
                        item.Point = new PointLatLng(lat, lng);
                        item.Name  = name;
                        list.Add(item);
                        ++this.succeedCount;
                        if (queryProgressEvent != null)
                        {
                            queryProgressEvent((long)this.succeedCount, (long)count);
                        }
                    }
                }

                int allPageNum = (int)Math.Ceiling((double)(((double)count) / ((double)pageSize)));
                if (pageIndex < allPageNum)
                {
                    list.AddRange(this.GetPlacemarksByKeywords(keywords, region, rectangle, nearby, pageIndex + 1, queryProgressEvent, ref count));
                }
            }
            return(list);
        }