Xamarin.iOS Can I use PlacePicker in my iOS application

169 views Asked by At

how can I implement PlacePicker in my project as I can this doable in iOS native by implement this code

@IBAction func pickPlace(_ sender: UIButton) {
  let config = GMSPlacePickerConfig(viewport: nil)
  let placePicker = GMSPlacePicker(config: config)

  placePicker.pickPlace(callback: { (place, error) -> Void in
    if let error = error {
      print("Pick Place error: \(error.localizedDescription)")
      return
    }

    guard let place = place else {
      print("No place selected")
      return
    }

    print("Place name \(place.name)")
    print("Place address \(place.formattedAddress)")
    print("Place attributions \(place.attributions)")
  })
}

how can I do the same in Xamarin.iOS

First I need nuget to include in my project

Second how can I implement the same code

1

There are 1 answers

0
Mina Fawzy On

First follow instruction to get google api key

Second Call this method in FinishedLaunching

string googleApiKey = YOUR_KEY_HERE;

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
 ConfigureGoogleMaps();
        }

 void ConfigureGoogleMaps()
        {
            PlacesClient.ProvideApiKey(googleApiKey);
            MapServices.ProvideAPIKey(googleApiKey);
        } 

Third add this keys in your Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>

Fourth ask user for permission to use his location Info.plist

<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>

Fifth now we call PlacePicker

var config = new PlacePickerConfig(null);
            var placePicker = new PlacePicker(config);
            placePicker.PickPlaceWithCallback((result, error) => {

                if(error != null){
                    return;    
                }
                if(result != null){
                    SelectedPlace = result;
                    street.Text = result.FormattedAddress;   
                }

            });

If you want know what equivalent to any method name in native looking here