private void PlacePickedCallBack(Place place, NSError error)
 {
     if (place != null)
     {
         var         name        = place.Name;
         var         placeId     = place.PlaceID;
         var         coordinate  = place.Coordinate;
         Coordinates coordinates = new Coordinates(coordinate.Latitude, coordinate.Longitude);
         var         phone       = place.PhoneNumber;
         var         address     = place.FormattedAddress;
         var         attribution = place.Attributions?.ToString();
         var         weburi      = place.Website?.ToString();
         var         priceLevel  = (long)place.PriceLevel;
         var         rating      = place.Rating;
         var         swlatitude  = place.Viewport?.SouthWest.Latitude;
         var         swlongitude = place.Viewport?.SouthWest.Longitude;
         var         nelatitude  = place.Viewport?.NorthEast.Latitude;
         var         nelongitude = place.Viewport?.NorthEast.Longitude;
         Abstractions.CoordinateBounds bounds = null;
         if (swlatitude != null && swlongitude != null && nelatitude != null && nelongitude != null)
         {
             bounds = new Abstractions.CoordinateBounds(new Coordinates(swlatitude.Value, swlongitude.Value), new Coordinates(nelatitude.Value, nelongitude.Value));
         }
         Places places = new Places(name, placeId, coordinates, phone, address, attribution, weburi, Convert.ToInt32(priceLevel), rating, bounds);
         OnPlaceSelected(new PlacePickedEventArgs(currentRequest.Value, false, places));
     }
     else if (error != null)
     {
         OnPlaceSelected(new PlacePickedEventArgs(currentRequest.Value, new Exception(error.LocalizedFailureReason)));
     }
     else
     {
         OnPlaceSelected(new PlacePickedEventArgs(currentRequest.Value, true));
     }
 }
        public void DidPickPlace(PlacePickerViewController viewController, Place place)
        {
            if (place != null)
            {
                var         name        = place.Name;
                var         placeId     = place.Id;
                var         coordinate  = place.Coordinate;
                Coordinates coordinates = new Coordinates(coordinate.Latitude, coordinate.Longitude);
                var         phone       = place.PhoneNumber;
                var         address     = place.FormattedAddress;
                var         attribution = place.Attributions?.ToString();
                var         weburi      = place.Website?.ToString();
                var         priceLevel  = (long)place.PriceLevel;
                var         rating      = place.Rating;
                var         swlatitude  = place.Viewport?.SouthWest.Latitude;
                var         swlongitude = place.Viewport?.SouthWest.Longitude;
                var         nelatitude  = place.Viewport?.NorthEast.Latitude;
                var         nelongitude = place.Viewport?.NorthEast.Longitude;
                Abstractions.CoordinateBounds bounds = null;
                if (swlatitude != null && swlongitude != null && nelatitude != null && nelongitude != null)
                {
                    bounds = new Abstractions.CoordinateBounds(new Coordinates(swlatitude.Value, swlongitude.Value), new Coordinates(nelatitude.Value, nelongitude.Value));
                }
                Places places = new Places(name, placeId, coordinates, phone, address, attribution, weburi, Convert.ToInt32(priceLevel), rating, bounds);
                OnPlaceSelected(new PlacePickedEventArgs(currentRequest.Value, false, places));
            }
            else
            {
                OnPlaceSelected(new PlacePickedEventArgs(currentRequest.Value, true));
            }

            DismissViewController(true, null);
            // DismissViewController(false,null);
        }
        /// <summary>
        /// Displays Place Picker UI.
        /// </summary>
        /// <param name="bounds"></param>
        /// <returns></returns>
        public Task <Places> Display(Abstractions.CoordinateBounds bounds = null)
        {
            currentRequest = GetRequestId();
            var ntcs = new TaskCompletionSource <Places>(currentRequest);

            if (Interlocked.CompareExchange(ref this.completionSource, ntcs, null) != null)
            {
                throw new InvalidOperationException("Only one operation can be active at a time");
            }
            Google.Maps.CoordinateBounds iosBound;
            PlacePickerConfig            config;

            if (bounds != null)
            {
                var northEast = new CLLocationCoordinate2D(bounds.Northeast.Latitude, bounds.Northeast.Longitude);
                var southwest = new CLLocationCoordinate2D(bounds.Southwest.Latitude, bounds.Southwest.Longitude);
                iosBound = new Google.Maps.CoordinateBounds(northEast, southwest);
                config   = new PlacePickerConfig(iosBound);
            }
            else
            {
                config = new PlacePickerConfig(null);
            }
            picker = new PlacePicker(config);
            picker.PickPlaceWithCallback(PlacePickedCallBack);
            EventHandler <PlacePickedEventArgs> handler = null;

            handler = (s, e) =>
            {
                var tcs = Interlocked.Exchange(ref this.completionSource, null);
                PlacePicked -= handler;

                if (e.RequestId != currentRequest)
                {
                    return;
                }
                if (e.IsCanceled)
                {
                    tcs.SetResult(null);
                }
                else if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else
                {
                    tcs.SetResult(e.Places);
                }
            };
            PlacePicked += handler;
            return(completionSource.Task);
        }
示例#4
0
 /// <summary>
 /// Generation of Places Object
 /// </summary>
 public Places(string Name = null, string PlaceId = null, Coordinates Coordinates = null, string Phone = null, string Address = null, string Attribution = null, string WebUri = null, int PriceLevel = -1, float Rating = -1, CoordinateBounds bounds = null)
 {
     this.Name         = Name;
     this.PlaceId      = PlaceId;
     this.Coordinates  = Coordinates;
     this.Phone        = Phone;
     this.Address      = Address;
     this.Attributions = Attributions;
     this.WebUri       = WebUri;
     this.PriceLevel   = PriceLevel;
     this.Rating       = Rating;
     this.ViewPort     = bounds;
 }
示例#5
0
        /// <summary>
        /// Displays Place Picker UI.
        /// </summary>
        /// <param name="bounds"></param>
        /// <returns></returns>
        ///

        public Task <Places> Display(Abstractions.CoordinateBounds bounds = null)
        {
            UIViewController CurrentController = null;
            UIWindow         window            = UIApplication.SharedApplication.KeyWindow;

            if (window == null)
            {
                throw new InvalidOperationException("There's no current active window");
            }
            if (window.WindowLevel == UIWindowLevel.Normal)
            {
                CurrentController = window.RootViewController;
            }

            if (CurrentController == null)
            {
                window = UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel).FirstOrDefault(w => w.RootViewController != null && w.WindowLevel == UIWindowLevel.Normal);
                if (window == null)
                {
                    throw new InvalidOperationException("Could not find current view controller");
                }
                else
                {
                    CurrentController = window.RootViewController;
                }
            }
            while (CurrentController.PresentedViewController != null)
            {
                CurrentController = CurrentController.PresentedViewController;
            }
            currentRequest = GetRequestId();
            var ntcs = new TaskCompletionSource <Places>(currentRequest);

            if (Interlocked.CompareExchange(ref this.completionSource, ntcs, null) != null)
            {
                throw new InvalidOperationException("Only one operation can be active at a time");
            }
            Google.Maps.CoordinateBounds iosBound;
            PlacePickerConfig            config;

            if (bounds != null)
            {
                var northEast = new CLLocationCoordinate2D(bounds.Northeast.Latitude, bounds.Northeast.Longitude);
                var southwest = new CLLocationCoordinate2D(bounds.Southwest.Latitude, bounds.Southwest.Longitude);
                iosBound = new Google.Maps.CoordinateBounds(northEast, southwest);
                config   = new PlacePickerConfig(iosBound);
            }
            else
            {
                config = new PlacePickerConfig(null);
            }
            PlacePickerController controller = new PlacePickerController(config, currentRequest);

            CurrentController.PresentViewController(controller, true, null);
            EventHandler <PlacePickedEventArgs> handler = null;

            handler = (s, e) =>
            {
                var tcs = Interlocked.Exchange(ref this.completionSource, null);
                PlacePickerController.PlacePicked -= handler;
                CurrentController.DismissViewController(false, null);
                if (e.RequestId != currentRequest)
                {
                    return;
                }
                if (e.IsCanceled)
                {
                    tcs.SetResult(null);
                }
                else if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else
                {
                    tcs.SetResult(e.Places);
                }
            };
            PlacePickerController.PlacePicked += handler;
            return(completionSource.Task);
        }