Exemple #1
0
        public virtual async Task <GecodeLocation> GeocodeAsync(string address, string city = null)
        {
            var requestParamters = new Dictionary <string, string>
            {
                { "address", address },
                { "ak", Options.AccessKey },
                { "output", Options.Output },
                { "ret_coordtype", Options.ReturnCoordType }
            };

            if (!city.IsNullOrWhiteSpace())
            {
                requestParamters.Add("city", city);
            }
            var baiduMapUrl  = "http://api.map.baidu.com";
            var baiduMapPath = "/geocoding/v3";

            if (Options.CaculateAKSN)
            {
                // TODO: 百度的文档不明不白,sn的算法在遇到特殊字符会验证失败,有待完善
                var sn = BaiduAKSNCaculater.CaculateAKSN(Options.AccessSecret, baiduMapPath, requestParamters);
                requestParamters.Add("sn", sn);
            }
            var requestUrl      = BuildRequestUrl(baiduMapUrl, baiduMapPath, requestParamters);
            var responseContent = await MakeRequestAndGetResultAsync(requestUrl);

            var baiduLocationResponse = JsonSerializer.Deserialize <BaiduGeocodeResponse>(responseContent);

            if (!baiduLocationResponse.IsSuccess())
            {
                var localizerFactory          = ServiceProvider.GetRequiredService <IStringLocalizerFactory>();
                var localizerErrorMessage     = baiduLocationResponse.GetErrorMessage(Options.VisableErrorToClient).Localize(localizerFactory);
                var localizerErrorDescription = baiduLocationResponse.GetErrorMessage(Options.VisableErrorToClient).Localize(localizerFactory);
                var localizer = ServiceProvider.GetRequiredService <IStringLocalizer <BaiduLocationResource> >();
                localizerErrorMessage = localizer["ResolveLocationFailed", localizerErrorMessage, localizerErrorDescription];
                if (Options.VisableErrorToClient)
                {
                    throw new UserFriendlyException(localizerErrorMessage);
                }
                throw new AbpException($"Resolution address failed:{localizerErrorMessage}!");
            }
            var location = new GecodeLocation
            {
                Confidence = baiduLocationResponse.Result.Confidence,
                Latitude   = baiduLocationResponse.Result.Location.Lat,
                Longitude  = baiduLocationResponse.Result.Location.Lng,
                Level      = baiduLocationResponse.Result.Level
            };

            location.AddAdditional("BaiduLocation", baiduLocationResponse.Result);

            return(location);
        }
Exemple #2
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);
        }
Exemple #3
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);
        }