Exemplo n.º 1
0
        public static async Task <List <DistanceMatrixDistance> > GetDistancesAsync(string apiKey, string originAddress,
                                                                                    List <string> destinationAddresses, GeoUnit geoUnit = GeoUnit.Metric,
                                                                                    RequestOutputFormat outputFormat = RequestOutputFormat.Json)
        {
            var result = await GetDistanceAsync(apiKey, new List <string> {
                originAddress
            },
                                                destinationAddresses,
                                                geoUnit, outputFormat);

            if (result?.Status == "OK" && result.Rows?.Length > 0 && result.Rows[0].Elements?.Length > 0)
            {
                return(result.Rows[0].Elements?.ToList());
            }

            return(null);
        }
Exemplo n.º 2
0
        public static async Task <GeoAddress> GetGeoAddressAsync(string apiKey, string originAddress,
                                                                 GeoUnit geoUnit = GeoUnit.Metric, RequestOutputFormat outputFormat = RequestOutputFormat.Json)
        {
            if (apiKey.IsNotNullOrEmpty() && originAddress.IsNotNullOrEmpty())
            {
                var path = $"{(outputFormat == RequestOutputFormat.Json ? "json" : "xml")}?key={apiKey}";
                if (geoUnit != GeoUnit.Metric)
                {
                    path += "&units=imperial";
                }

                path += $"&address={originAddress}";
                using (var rest = new Rest(new Uri(RequestUrl)))
                {
                    var result = await rest.GetAsync <string>(path);

                    if (result.IsNotNullOrEmpty())
                    {
                        var jobject = JObject.Parse(result);
                        if (jobject.ContainsKey("results"))
                        {
                            var valueResult = jobject["results"].ToObject <GeoAddress[]>();
                            if (valueResult?.Length > 0)
                            {
                                return(valueResult[0]);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        public static Task <DistanceMatrixResponse> GetDistanceAsync(string apiKey, List <string> originAddresses,
                                                                     List <string> destinationAddresses,
                                                                     GeoUnit geoUnit = GeoUnit.Metric, RequestOutputFormat outputFormat = RequestOutputFormat.Json)
        {
            if (apiKey.IsNotNullOrEmpty() && originAddresses?.Count > 0 &&
                destinationAddresses?.Count() > 0)
            {
                var path = $"{(outputFormat == RequestOutputFormat.Json ? "json" : "xml")}?key={apiKey}";
                if (geoUnit != GeoUnit.Metric)
                {
                    path += "&units=imperial";
                }
                var origins      = string.Join("|", originAddresses.Select(item => item.Trim()));
                var destinations = string.Join("|", destinationAddresses.Select(item => item.Trim()));

                path += $"&origins={origins}&destinations={destinations}";
                using (var rest = new Rest(new Uri(RequestUrl)))
                {
                    return(rest.GetAsync <DistanceMatrixResponse>(path));
                }
            }

            return(Task.FromResult(default(DistanceMatrixResponse)));
        }