예제 #1
0
        ///<inheritdoc/>
        public async Task <IEnumerable <IPlaceResult> > GetPredictions(string query, MapSpan bounds)
        {
            if (apiClient == null || !apiClient.IsConnected)
            {
                Connect();
            }

            List <IPlaceResult> result = new List <IPlaceResult>();

            double mDistanceInMeters = bounds.Radius.Meters;

            double latRadian = bounds.LatitudeDegrees;

            double degLatKm  = 110.574235;
            double degLongKm = 110.572833 * Math.Cos(latRadian);
            double deltaLat  = mDistanceInMeters / 1000.0 / degLatKm;
            double deltaLong = mDistanceInMeters / 1000.0 / degLongKm;

            double minLat  = bounds.Center.Latitude - deltaLat;
            double minLong = bounds.Center.Longitude - deltaLong;
            double maxLat  = bounds.Center.Latitude + deltaLat;
            double maxLong = bounds.Center.Longitude + deltaLong;

            if (buffer != null)
            {
                buffer.Dispose();
                buffer = null;
            }

            buffer = await PlacesClass.GeoDataApi.GetAutocompletePredictionsAsync(
                apiClient,
                query,
                new LatLngBounds(new LatLng(minLat, minLong), new LatLng(maxLat, maxLong)),
                null);

            if (buffer != null)
            {
                result.AddRange(buffer.Select(i =>
                                              new TKNativeAndroidPlaceResult
                {
                    Description = i.GetPrimaryText(null),
                    Subtitle    = i.GetSecondaryText(null),
                    PlaceId     = i.PlaceId,
                }));
            }

            return(result);
        }
예제 #2
0
        public async Task GetPredictionsFromPlacesAPI(string searchText, LatLngBounds bounds)
        {
            if (this._apiClient == null || !this._apiClient.IsConnected)
            {
                ConnectToPlacesAPI();
            }

            //List<IPlaceResult> result = new List<IPlaceResult>();

            if (autocompletePredictionBuffer != null)
            {
                autocompletePredictionBuffer.Dispose();
                autocompletePredictionBuffer = null;
            }

            autocompletePredictionBuffer = await PlacesClass.GeoDataApi.GetAutocompletePredictionsAsync(this._apiClient, searchText, bounds, null);

            if (autocompletePredictionBuffer != null)
            {
                if (autocompletePredictionBuffer.Status.IsSuccess)
                {
                    if (autocompletePredictionBuffer.Count > 0)
                    {
                        ClearRoute(null, null);

                        IAutocompletePrediction pred;
                        List <string>           placeIds = new List <string>();

                        for (int i = 0; i < autocompletePredictionBuffer.Count; i++)
                        {
                            pred = autocompletePredictionBuffer.ElementAt(i);
                            placeIds.Add(pred.PlaceId);
                        }

                        PlaceBuffer placeBuffer = await PlacesClass.GeoDataApi.GetPlaceByIdAsync(this._apiClient, placeIds.ToArray());

                        if (placeBuffer == null || !placeBuffer.Any())
                        {
                            System.Diagnostics.Debug.WriteLine("No results found for individual place details for search: " + searchText);
                            return;
                        }
                        else
                        {
                            IPlace placeDetails;
                            LatLngBounds.Builder builder = new LatLngBounds.Builder();

                            for (int i = 0; i < placeBuffer.Count; i++)
                            {
                                placeDetails = placeBuffer.ElementAt(i);

                                this.AddMarker(placeDetails.NameFormatted.ToString(), placeDetails.LatLng);

                                builder.Include(placeDetails.LatLng);
                            }

                            LatLngBounds newBounds = builder.Build();
                            CameraUpdate cu        = CameraUpdateFactory.NewLatLngBounds(newBounds, 120);
                            googleMap.AnimateCamera(cu);
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("No results found for search: " + searchText);
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(autocompletePredictionBuffer.Status.StatusMessage);
                }

                //result.AddRange(this._buffer.Select(i =>
                //    new TKNativeAndroidPlaceResult
                //    {
                //        Description = i.GetPrimaryText(null),
                //        Subtitle = i.GetSecondaryText(null),
                //        PlaceId = i.PlaceId,
                //    }));
            }

            //return result;
        }