Пример #1
0
        public Dictionary <string, string> getLocationByText(string locationText)
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = "AIzaSyBA58FFbrOgnkHm5k3-i1cF2lJOhfouQ1I", //this.ApiKey,
                Input = locationText                               //"jagtvej 2200"
            };
            //CancellationTokenSource tokenSource = new CancellationTokenSource(new System.TimeSpan(0, 0, 2));
            PlacesQueryAutoCompleteResponse response;
            var task = GooglePlaces.QueryAutoComplete.QueryAsync(request);

            //task.RunSynchronously();
            task.Wait(new System.TimeSpan(0, 0, 2));

            if (task.IsCompleted)
            {
                response = task.Result;
            }
            else
            {
                response = new PlacesQueryAutoCompleteResponse();
            }
            var result = response.Predictions?.Where(x => x.PlaceId != null)?.ToDictionary(x => x.PlaceId, x => string.Join(",", string.Concat(x.StructuredFormatting.MainText, ",", x.StructuredFormatting.SecondaryText).Split(",").Take(2)));

            return(result ?? new Dictionary <string, string>());
        }
Пример #2
0
        public void PlacesQueryAutoCompleteWhenLanguageTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key      = this.ApiKey,
                Input    = "jagtvej 2200 København",
                Language = Language.Danish
            };

            var response = GooglePlaces.QueryAutoComplete.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);

            var results = response.Predictions.ToArray();

            Assert.IsNotNull(results);
            Assert.IsNotEmpty(results);
            Assert.AreEqual(5, results.Length);

            var result = results.FirstOrDefault();

            Assert.IsNotNull(result);

            var description = result.Description.ToLower();

            Assert.IsTrue(description.Contains("2200"), "1");
            Assert.IsTrue(description.Contains("jagtvej"), "2");
            Assert.IsTrue(description.Contains("københavn"), "3");
        }
Пример #3
0
        public void GetQueryStringParametersTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = "key",
                Input = "input"
            };

            var queryStringParameters = request.GetQueryStringParameters();

            Assert.IsNotNull(queryStringParameters);

            var key         = queryStringParameters.FirstOrDefault(x => x.Key == "key");
            var keyExpected = request.Key;

            Assert.IsNotNull(key);
            Assert.AreEqual(keyExpected, key.Value);

            var input         = queryStringParameters.FirstOrDefault(x => x.Key == "input");
            var inputExpected = request.Input;

            Assert.IsNotNull(input);
            Assert.AreEqual(inputExpected, input.Value);

            var language = queryStringParameters.FirstOrDefault(x => x.Key == "language");

            Assert.IsNotNull(language);
            Assert.AreEqual("en", language.Value);
        }
Пример #4
0
        public void PlacesQueryAutoCompleteWhenLanguageTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key      = this.ApiKey,
                Input    = "jagtvej 2200 K�benhavn",
                Language = Language.Danish
            };

            var response = GooglePlaces.QueryAutoComplete.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);

            var results = response.Predictions.ToArray();

            Assert.IsNotNull(results);
            Assert.IsNotEmpty(results);
            Assert.GreaterOrEqual(results.Length, 3);

            var result = results.FirstOrDefault();

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Description, "Jagtvej, 2200 K�benhavn, Danmark");
        }
Пример #5
0
        public void ConstructorDefaultTest()
        {
            var request = new PlacesQueryAutoCompleteRequest();

            Assert.IsNull(request.Offset);
            Assert.IsNull(request.Radius);
            Assert.IsNull(request.Location);
            Assert.AreEqual(Language.English, request.Language);
        }
        public void GetQueryStringParametersTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = "abc",
                Input = "abc"
            };

            Assert.DoesNotThrow(() => request.GetQueryStringParameters());
        }
Пример #7
0
        public void PlacesQueryAutoCompleteWhenApiKeyIsStringEmptyTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key = string.Empty
            };

            var exception = Assert.Throws <ArgumentException>(() => GooglePlaces.QueryAutoComplete.Query(request));

            Assert.AreEqual(exception.Message, "ApiKey must be provided");
        }
Пример #8
0
        public void PlacesQueryAutoCompleteWhenKeyIsNullTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = null,
                Input = "test"
            };

            var exception = Assert.Throws <ArgumentException>(() => GooglePlaces.QueryAutoComplete.Query(request));

            Assert.AreEqual(exception.Message, "Key is required");
        }
Пример #9
0
        public void PlacesQueryAutoCompleteWhenInputIsStringEmptyTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = this.ApiKey,
                Input = string.Empty
            };

            var exception = Assert.Throws <ArgumentException>(() => GooglePlaces.QueryAutoComplete.Query(request));

            Assert.AreEqual(exception.Message, "Input is required");
        }
Пример #10
0
        public void PlacesQueryAutoCompleteWhenAsyncTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = this.ApiKey,
                Input = "jagtvej 2200"
            };
            var response = GooglePlaces.QueryAutoComplete.QueryAsync(request).Result;

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);
        }
Пример #11
0
        public void PlacesQueryAutoCompleteWhenInputIsNullTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = this.apiKey,
                Input = null
            };

            var exception = Assert.Throws <ArgumentException>(() => GooglePlaces.QueryAutoComplete.Query(request));

            Assert.AreEqual(exception.Message, "Input must not null or empty");
        }
Пример #12
0
        public void PlacesQueryAutoCompleteWhenRadiusIsGereaterThanFiftyThousandTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key    = this.ApiKey,
                Input  = "abc",
                Radius = 50001
            };

            var exception = Assert.Throws <ArgumentException>(() => GooglePlaces.QueryAutoComplete.Query(request));

            Assert.AreEqual(exception.Message, "Radius must be greater than or equal to 1 and less than or equal to 50.000");
        }
        public void GetUriTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = "abc",
                Input = "abc"
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/maps/api/place/queryautocomplete/json?key={request.Key}&input={request.Input}&language={request.Language.ToCode()}", uri.PathAndQuery);
        }
Пример #14
0
        public void PlacesAutoCompleteWhenLocationTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key      = this.ApiKey,
                Input    = "jagtvej 2200 København",
                Location = new Coordinate(1, 1)
            };

            var response = GooglePlaces.QueryAutoComplete.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);
        }
        public void GetUriWhenLocationTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key      = "abc",
                Input    = "abc",
                Location = new Location(1, 1)
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/maps/api/place/queryautocomplete/json?key={request.Key}&input={request.Input}&language={request.Language.ToCode()}&location={Uri.EscapeDataString(request.Location.ToString())}", uri.PathAndQuery);
        }
Пример #16
0
        public void PlacesAutoCompleteWhenOffsetTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key    = this.ApiKey,
                Input  = "jagtvej 2200 København",
                Offset = "offset"
            };

            var response = GooglePlaces.QueryAutoComplete.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);
        }
Пример #17
0
        public void GetQueryStringParametersWhenKeyIsStringEmptyTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key = string.Empty
            };

            var exception = Assert.Throws <ArgumentException>(() =>
            {
                var parameters = request.GetQueryStringParameters();
                Assert.IsNull(parameters);
            });

            Assert.AreEqual(exception.Message, "'Key' is required");
        }
Пример #18
0
        public void GetQueryStringParametersWhenInputIsNullTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = this.ApiKey,
                Input = null
            };

            var exception = Assert.Throws <ArgumentException>(() =>
            {
                var parameters = request.GetQueryStringParameters();
                Assert.IsNull(parameters);
            });

            Assert.AreEqual(exception.Message, "Input is required");
        }
Пример #19
0
        public void GetQueryStringParametersWhenRadiusIsGereaterThanFiftyThousandTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key    = "key",
                Input  = "input",
                Radius = 50001
            };

            var exception = Assert.Throws <ArgumentException>(() =>
            {
                var parameters = request.GetQueryStringParameters();
                Assert.IsNull(parameters);
            });

            Assert.AreEqual(exception.Message, "'Radius' must be greater than or equal to 1 and less than or equal to 50.000");
        }
Пример #20
0
        public void GetQueryStringParametersWhenRadiusIsLessThanOneTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key    = this.ApiKey,
                Input  = "abc",
                Radius = 0
            };

            var exception = Assert.Throws <ArgumentException>(() =>
            {
                var parameters = request.GetQueryStringParameters();
                Assert.IsNull(parameters);
            });

            Assert.AreEqual(exception.Message, "Radius must be greater than or equal to 1 and less than or equal to 50.000");
        }
Пример #21
0
        public void PlacesQueryAutoCompleteWhenInvalidKeyTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = "test",
                Input = "jagtvej 2200 K�benhavn"
            };

            var exception = Assert.Throws <AggregateException>(() => GooglePlaces.QueryAutoComplete.QueryAsync(request).Wait());

            Assert.IsNotNull(exception);

            var innerException = exception.InnerExceptions.FirstOrDefault();

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(ApiException), innerException.GetType());
            Assert.AreEqual("RequestDenied: The provided API key is invalid.", innerException.Message);
        }
Пример #22
0
        public void PlacesQueryAutoCompleteWhenAsyncAndCancelledTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = this.ApiKey,
                Input = "jagtvej 2200"
            };

            var cancellationTokenSource = new CancellationTokenSource();
            var task = GooglePlaces.QueryAutoComplete.QueryAsync(request, cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();

            var exception = Assert.Throws <OperationCanceledException>(() => task.Wait(cancellationTokenSource.Token));

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "The operation was canceled.");
        }
Пример #23
0
        public void PlacesQueryAutoCompleteTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key    = this.ApiKey,
                Input  = "jagtvej 2200",
                Sensor = true
            };
            var response = GooglePlaces.QueryAutoComplete.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);

            var results = response.Predictions.ToArray();

            Assert.IsNotNull(results);
            Assert.AreEqual(5, results.Length);
        }
Пример #24
0
        public void PlacesQueryAutoCompleteWhenInputIsStringEmptyTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = this.ApiKey,
                Input = string.Empty
            };

            var exception = Assert.Throws <AggregateException>(() => GooglePlaces.QueryAutoComplete.QueryAsync(request).Wait());

            Assert.IsNotNull(exception);

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(ApiException), innerException.GetType());
            Assert.AreEqual(innerException.Message, "Input is required");
        }
Пример #25
0
        public void PlacesQueryAutoCompleteWhenKeyIsNullTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = null,
                Input = "test"
            };

            var exception = Assert.Throws <AggregateException>(() => GooglePlaces.QueryAutoComplete.QueryAsync(request).Wait());

            Assert.IsNotNull(exception);

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(ApiException), innerException.GetType());
            Assert.AreEqual(innerException.Message, "Key is required");
        }
Пример #26
0
        public void PlacesQueryAutoCompleteWhenRadiusIsGereaterThanFiftyThousandTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key    = this.ApiKey,
                Input  = "abc",
                Radius = 50001
            };

            var exception = Assert.Throws <AggregateException>(() => GooglePlaces.QueryAutoComplete.QueryAsync(request).Wait());

            Assert.IsNotNull(exception);

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(ApiException), innerException.GetType());
            Assert.AreEqual(innerException.Message, "Radius must be greater than or equal to 1 and less than or equal to 50.000");
        }
Пример #27
0
        public void PlacesQueryAutoCompleteWhenInputIsNullTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = this.ApiKey,
                Input = null
            };

            var exception = Assert.Throws <AggregateException>(() => GooglePlaces.QueryAutoComplete.Query(request));

            Assert.IsNotNull(exception);
            Assert.AreEqual("One or more errors occurred.", exception.Message);

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(GoogleApiException), innerException.GetType());
            Assert.AreEqual(innerException.Message, "Input is required");
        }
Пример #28
0
        public void PlacesQueryAutoCompleteTest()
        {
            var request = new PlacesQueryAutoCompleteRequest
            {
                Key   = this.ApiKey,
                Input = "jagtvej 2200 København"
            };

            var response = GooglePlaces.QueryAutoComplete.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);

            var results = response.Predictions.ToArray();

            Assert.IsNotNull(results);
            Assert.IsNotEmpty(results);
            Assert.AreEqual(5, results.Length);

            var result = results.FirstOrDefault();

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Terms);
            Assert.IsNotNull(result.PlaceId);
            Assert.IsNotNull(result.StructuredFormatting);

            var description = result.Description.ToLower();

            Assert.IsTrue(description.Contains("2200"), "1");
            Assert.IsTrue(description.Contains("jagtvej"), "2");
            Assert.IsTrue(description.Contains("copenhagen"), "3");

            var matchedSubstrings = result.MatchedSubstrings.ToArray();

            Assert.IsNotNull(matchedSubstrings);
            Assert.AreEqual(3, matchedSubstrings.Length);

            var types = result.Types.ToArray();

            Assert.IsNotNull(types);
            Assert.Contains(PlaceLocationType.Route, types);
            Assert.Contains(PlaceLocationType.Geocode, types);
        }
Пример #29
0
        public void PlacesQueryAutoCompleteTest()
        {
            var _request = new PlacesQueryAutoCompleteRequest
            {
                Key      = this._apiKey,
                Input    = "jagtvej 2200",
                Sensor   = true,
                Language = "en",
            };
            var _response = GooglePlaces.QueryAutoComplete.Query(_request);
            var _results  = _response.Predictions.ToList();

            Assert.AreEqual(_results[0].Description, "Jagtvej 2200, Nuuk, Greenland");
            Assert.AreEqual(_results[1].Description, "Jagtvej 2200, Denmark");
            Assert.AreEqual(_results[2].Description, "Jagtvej 2200, Hillerød, Denmark");
            Assert.AreEqual(_results[3].Description, "Jagtvej 2200, Fredensborg, Denmark");
            Assert.AreEqual(_results[4].Description, "Jagtvej 2200, Lemvig, Denmark");
            Assert.AreEqual(5, _results.Count);
        }
Пример #30
0
        public void PlacesQueryAutoCompleteTest()
        {
            var _request = new PlacesQueryAutoCompleteRequest
            {
                ApiKey   = _apiKey,
                Input    = "jagtvej 2200",
                Sensor   = true,
                Language = "en",
            };
            var _response = GooglePlaces.QueryAutoComplete.Query(_request);
            var _results  = _response.Predictions.ToList();

            Assert.AreEqual(_results[0].Description, "Jagtvej 2200, Copenhagen, Denmark");
            Assert.AreEqual(_results[1].Description, "2200 Jagtvej, Nuuk, Greenland");
            Assert.AreEqual(_results[2].Description, "Jagtvej 2200, Odense, Denmark");
            Assert.AreEqual(_results[3].Description, "Jagtvej 2200, Esbjerg, Denmark");
            Assert.AreEqual(_results[4].Description, "Jagtvej 2200, Naestved, Denmark");
            Assert.AreEqual(5, _results.Count);
        }