Google Android Place API Examples
Place and map dependencies
implementation 'com.google.android.gms:play-services-maps:11.6.0' implementation 'com.google.android.gms:play-services-places:11.6.0'
Field variables
val PLACE_AUTOCOMPLETE_REQUEST_CODE = 1001 val PLACE_PICKER_REQUEST = 1002 val REQUEST_PERMISSIONS = 100 protected lateinit var mGeoDataClient: GeoDataClient private var mGoogleApiClient: GoogleApiClient? = null private var mPlaceDetectionClient: PlaceDetectionClient? = nullInitialize Google api in the onCreate functionoverride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Construct a GeoDataClient. mGeoDataClient = Places.getGeoDataClient(this, null) mGoogleApiClient = GoogleApiClient.Builder(this) .addApi(Places.GEO_DATA_API) .addApi(Places.PLACE_DETECTION_API) .enableAutoManage(this, this) .build() // Construct a PlaceDetectionClient. mPlaceDetectionClient = Places.getPlaceDetectionClient(this, null) initAutoComplete() }PlacePicker example
private fun launchPlacePicker() { try { val builder = PlacePicker.IntentBuilder() startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST) } catch (e : GooglePlayServicesNotAvailableException) { e.printStackTrace() } catch (e : Exception) { e.printStackTrace() } }CurrentPlace example
private fun currentPlaces() { if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { var placeResult = mPlaceDetectionClient!!.getCurrentPlace(null) placeResult.addOnCompleteListener({ task -> var likelyPlaces = task.getResult() for (placeLikelihood in likelyPlaces) { Log.i(TAG, String.format("Place '%s' has likelihood: %g", placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood())) } likelyPlaces.release() }) } else { var PERMISSIONS_REQUIRED = arrayOf( Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION) ActivityCompat.requestPermissions(this, PERMISSIONS_REQUIRED, REQUEST_PERMISSIONS) } }Place auto complete example
private fun autoComplete() { try { val intent = PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN) .build(this) startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE) } catch (e: GooglePlayServicesRepairableException) { // TODO: Handle the error. } catch (e: GooglePlayServicesNotAvailableException) { // TODO: Handle the error. } }Handle permissions
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) Log.d("MainActivity", "requestCode: " + requestCode) Log.d("MainActivity", "Permissions:" + Arrays.toString(permissions)) Log.d("MainActivity", "grantResults: " + Arrays.toString(grantResults)) //do something to handle the results } Handle activity results
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { val place = PlaceAutocomplete.getPlace(this, data) Log.i(TAG, "Place: " + place.getName()) } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { val status = PlaceAutocomplete.getStatus(this, data) // TODO: Handle the error. Log.i(TAG, status.getStatusMessage()) } else if (resultCode == Activity.RESULT_CANCELED) { // The user canceled the operation. } } else if (requestCode == PLACE_PICKER_REQUEST) { if (resultCode == RESULT_OK) { val place = PlacePicker.getPlace(data, this) val toastMsg = String.format("Place: %s", place.getName()) Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show() } } }
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts