Forum Xamarin.iOS

How to display the Google Places API PlacePicker in Xamarin.iOS

I have a PCL application, specifically focused on iOS as I have written the Android application natively. I am trying to get a PlacePicker to display in my view using the Google Maps/Places API.

I have a key, I can get a MapView displaying in the same view without any issue, but the PlacePicker is supposed to display the correct control when you assign the PlacePickerCallback, but every time I fire up my app in the simulator it quickly shows a map with a marker and then the maps disappears. I am using a PageRenderer to navigate from Forms to Native so I can load the Google Maps API component. Below is the code I am using to attempt to render the PlacePicker in my view. There is some tertiary code and test values that are of no consequence, right now the goal is just to attempt to get the PlacePicker to display.

public class FindGymPageRenderer : PageRenderer
{
    private MapView mapView;

    public override void LoadView ()
    {
        base.LoadView ();
        CameraPosition camera = CameraPosition.FromCamera (latitude: 37.797865, 
            longitude: -122.402526, 
            zoom: 6);
        mapView = MapView.FromCamera (CGRect.Empty, camera);
        mapView.MyLocationEnabled = true;
        mapView.MapType = MapViewType.Normal;
        mapView.SetMinMaxZoom (0, 20);

        VisibleRegion region = new VisibleRegion ();
        CoreLocation.CLLocationCoordinate2D center = new CoreLocation.CLLocationCoordinate2D(51.5108396, -0.0922251);
        CoreLocation.CLLocationCoordinate2D farLeft = new CoreLocation.CLLocationCoordinate2D(center.Latitude - 0.001, center.Longitude - 0.001);
        CoreLocation.CLLocationCoordinate2D farRight = new CoreLocation.CLLocationCoordinate2D(center.Latitude + 0.001, center.Longitude + 0.001);
        region.FarLeft = farLeft;
        region.FarRight = farRight;
        CoordinateBounds bounds = new CoordinateBounds (region);
        PlacePickerConfig pickerConfig = new PlacePickerConfig (bounds);
        PlacePicker picker = new PlacePicker (pickerConfig);
        try{
        picker.PickPlaceWithCallback (PlaceResultHandlerCallback);
        }
        catch(Exception ex){
            Console.WriteLine (ex.Message);
        }

        //View = mapView;
    }

    public void PlaceResultHandlerCallback(Place place, Foundation.NSError error){
        if (place != null) {
            Console.WriteLine (place.Coordinate.Latitude);
        }
    }

    public override void ViewWillAppear (bool animated)
    {
        base.ViewWillAppear (animated);
        //mapView.StartRendering ();
    }

    public override void ViewWillDisappear (bool animated)
    {   
        //mapView.StopRendering ();
        base.ViewWillDisappear (animated);
    }
}

Best Answer

Answers

  • DeliciousBiscuitDeliciousBiscuit USMember

    Ryan,
    I had a feeling that either the dialog was getting pushed to the back as far as visual stacked items is concerned or something was getting descoped, which appears to be the case. When making the call natively in Android the Intent is made inline, which is why I had done the same here. I really appreciate the clean, concise response you provided, extremely professional. Thanks again.

Sign In or Register to comment.