示例#1
0
        public async Task <GecodeLocation> GeocodeAsync(string address, string city)
        {
            var positiceRequestParamter = new AmapPositiveHttpRequestParamter
            {
                Address = address
            };

            return(await AmapHttpRequestClient.PositiveAsync(positiceRequestParamter));
        }
示例#2
0
        public virtual async Task <GecodeLocation> PositiveAsync(AmapPositiveHttpRequestParamter requestParamter)
        {
            var client            = HttpClientFactory.CreateClient(AmapHttpConsts.HttpClientName);
            var requestUrlBuilder = new StringBuilder(128);

            requestUrlBuilder.Append("http://restapi.amap.com/v3/geocode/geo");
            requestUrlBuilder.AppendFormat("?key={0}", Options.ApiKey);
            requestUrlBuilder.AppendFormat("&address={0}", requestParamter.Address);
            if (!requestParamter.City.IsNullOrWhiteSpace())
            {
                requestUrlBuilder.AppendFormat("&city={0}", requestParamter.City);
            }
            if (!requestParamter.Output.IsNullOrWhiteSpace())
            {
                requestUrlBuilder.AppendFormat("&output={0}", requestParamter.Output);
            }
            if (!requestParamter.Sig.IsNullOrWhiteSpace())
            {
                requestUrlBuilder.AppendFormat("&sig={0}", requestParamter.Sig);
            }
            requestUrlBuilder.AppendFormat("&batch={0}", requestParamter.Batch);

            var requestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrlBuilder.ToString());

            var response = await client.SendAsync(requestMessage, GetCancellationToken());

            if (!response.IsSuccessStatusCode)
            {
                throw new AbpException($"Amap request service returns error! HttpStatusCode: {response.StatusCode}, ReasonPhrase: {response.ReasonPhrase}");
            }

            var resultContent = await response.Content.ReadAsStringAsync();

            var amapResponse = JsonSerializer.Deserialize <AmapPositiveHttpResponse>(resultContent);

            if (!amapResponse.IsSuccess())
            {
                var localizerFactory = ServiceProvider.GetRequiredService <IStringLocalizerFactory>();
                var localizerError   = amapResponse.GetErrorMessage().Localize(localizerFactory);
                if (Options.VisableErrorToClient)
                {
                    var localizer = ServiceProvider.GetRequiredService <IStringLocalizer <AmapLocationResource> >();
                    localizerError = localizer["ResolveLocationFailed", localizerError];
                    throw new UserFriendlyException(localizerError);
                }
                throw new AbpException($"Resolution address failed:{localizerError}!");
            }
            if (amapResponse.Count <= 0)
            {
                var localizer      = ServiceProvider.GetRequiredService <IStringLocalizer <AmapLocationResource> >();
                var localizerError = localizer["ResolveLocationZero"];
                if (Options.VisableErrorToClient)
                {
                    throw new UserFriendlyException(localizerError);
                }
                throw new AbpException(localizerError);
            }

            var locations       = amapResponse.Geocodes[0].Location.Split(",");
            var postiveLocation = new GecodeLocation
            {
                Longitude = double.Parse(locations[0]),
                Latitude  = double.Parse(locations[1]),
                Level     = amapResponse.Geocodes[0].Level
            };

            postiveLocation.AddAdditional("Geocodes", amapResponse.Geocodes);

            return(postiveLocation);
        }