Example #1
0
        public virtual async Task <ReGeocodeLocation> ReGeocodeAsync(double lat, double lng, int radius = 1000)
        {
            var requestParamters = new Dictionary <string, string>
            {
                { "callback", Options.Callback },
                { "get_poi", Options.GetPoi },
                { "key", Options.AccessKey },
                { "location", $"{lat},{lng}" },
                { "output", Options.Output },
                { "poi_options", "radius=" + radius.ToString() }
            };
            var tencentMapUrl  = "https://apis.map.qq.com";
            var tencentMapPath = "/ws/geocoder/v1";

            if (!Options.SecretKey.IsNullOrWhiteSpace())
            {
                var sig = TencentSecretKeyCaculater.CalcSecretKey(tencentMapPath, Options.SecretKey, requestParamters);
                requestParamters.Add("sig", sig);
            }
            var tencentLocationResponse = await GetTencentMapResponseAsync <TencentReGeocodeResponse>(tencentMapUrl, tencentMapPath, requestParamters);

            var location = new ReGeocodeLocation
            {
                Street           = tencentLocationResponse.Result.AddressComponent.Street,
                AdCode           = tencentLocationResponse.Result.AddressInfo?.NationCode,
                Address          = tencentLocationResponse.Result.Address,
                FormattedAddress = tencentLocationResponse.Result.FormattedAddress?.ReCommend,
                City             = tencentLocationResponse.Result.AddressComponent.City,
                Country          = tencentLocationResponse.Result.AddressComponent.Nation,
                District         = tencentLocationResponse.Result.AddressComponent.District,
                Number           = tencentLocationResponse.Result.AddressComponent.StreetNumber,
                Province         = tencentLocationResponse.Result.AddressComponent.Province,
                Town             = tencentLocationResponse.Result.AddressReference.Town.Title,
                Pois             = tencentLocationResponse.Result.Pois.Select(p =>
                {
                    var poi = new Poi
                    {
                        Address  = p.Address,
                        Name     = p.Title,
                        Tag      = p.Id,
                        Type     = p.CateGory,
                        Distance = Convert.ToInt32(p.Distance)
                    };

                    return(poi);
                }).ToList()
            };

            if ((location.Address.IsNullOrWhiteSpace() ||
                 location.FormattedAddress.IsNullOrWhiteSpace()) &&
                location.Pois.Any())
            {
                var nearPoi = location.Pois.OrderBy(x => x.Distance).FirstOrDefault();
                location.Address          = nearPoi.Address;
                location.FormattedAddress = nearPoi.Name;
            }
            location.AddAdditional("TencentLocation", tencentLocationResponse.Result);

            return(location);
        }
Example #2
0
        public virtual async Task <IPGecodeLocation> IPGeocodeAsync(string ipAddress)
        {
            var requestParamters = new Dictionary <string, string>
            {
                { "callback", Options.Callback },
                { "ip", ipAddress },
                { "key", Options.AccessKey },
                { "output", Options.Output }
            };
            var tencentMapUrl  = "https://apis.map.qq.com";
            var tencentMapPath = "/ws/location/v1/ip";

            if (!Options.SecretKey.IsNullOrWhiteSpace())
            {
                var sig = TencentSecretKeyCaculater.CalcSecretKey(tencentMapPath, Options.SecretKey, requestParamters);
                requestParamters.Add("sig", sig);
            }
            var tencentLocationResponse = await GetTencentMapResponseAsync <TencentIPGeocodeResponse>(tencentMapUrl, tencentMapPath, requestParamters);

            var location = new IPGecodeLocation
            {
                IpAddress = tencentLocationResponse.Result.IpAddress,
                AdCode    = tencentLocationResponse.Result.AddressInfo.AdCode,
                City      = tencentLocationResponse.Result.AddressInfo.City,
                Country   = tencentLocationResponse.Result.AddressInfo.Nation,
                District  = tencentLocationResponse.Result.AddressInfo.District,
                Location  = new Location
                {
                    Latitude  = tencentLocationResponse.Result.Location.Lat,
                    Longitude = tencentLocationResponse.Result.Location.Lng
                },
                Province = tencentLocationResponse.Result.AddressInfo.Province
            };

            location.AddAdditional("TencentLocation", tencentLocationResponse.Result);

            return(location);
        }
Example #3
0
        public virtual async Task <GecodeLocation> GeocodeAsync(string address, string city = null)
        {
            var requestParamters = new Dictionary <string, string>
            {
                { "address", address },
                { "callback", Options.Callback },
                { "key", Options.AccessKey },
                { "output", Options.Output }
            };

            if (!city.IsNullOrWhiteSpace())
            {
                requestParamters.Add("region", city);
            }
            var tencentMapUrl  = "https://apis.map.qq.com";
            var tencentMapPath = "/ws/geocoder/v1";

            if (!Options.SecretKey.IsNullOrWhiteSpace())
            {
                var sig = TencentSecretKeyCaculater.CalcSecretKey(tencentMapPath, Options.SecretKey, requestParamters);
                requestParamters.Add("sig", sig);
            }
            var tencentLocationResponse = await GetTencentMapResponseAsync <TencentGeocodeResponse>(tencentMapUrl, tencentMapPath, requestParamters);

            var location = new GecodeLocation
            {
                Confidence = tencentLocationResponse.Result.Reliability,
                Latitude   = tencentLocationResponse.Result.Location.Lat,
                Longitude  = tencentLocationResponse.Result.Location.Lng,
                Level      = tencentLocationResponse.Result.Level.ToString()
            };

            location.AddAdditional("TencentLocation", tencentLocationResponse.Result);

            return(location);
        }