@@ -33,7 +33,6 @@ import com.google.android.gms.common.GoogleApiAvailability
3333import com.google.android.gms.common.api.GoogleApiClient
3434import com.google.android.gms.location.LocationListener
3535import com.google.android.gms.location.LocationServices
36- import com.google.android.gms.location.places.Places
3736import com.google.android.gms.maps.CameraUpdateFactory
3837import com.google.android.gms.maps.GoogleMap
3938import com.google.android.gms.maps.GoogleMap.MAP_TYPE_NORMAL
@@ -46,6 +45,7 @@ import com.google.android.gms.maps.model.LatLng
4645import com.google.android.gms.maps.model.MapStyleOptions
4746import com.google.android.gms.maps.model.Marker
4847import com.google.android.gms.maps.model.MarkerOptions
48+ import com.google.android.libraries.places.api.Places
4949import com.google.android.material.floatingactionbutton.FloatingActionButton
5050import com.google.maps.GeoApiContext
5151import com.schibstedspain.leku.geocoder.AndroidGeocoderDataSource
@@ -88,6 +88,7 @@ const val TIME_ZONE_DISPLAY_NAME = "time_zone_display_name"
8888const val MAP_STYLE = " map_style"
8989const val UNNAMED_ROAD_VISIBILITY = " unnamed_road_visibility"
9090private const val GEOLOC_API_KEY = " geoloc_api_key"
91+ private const val PLACES_API_KEY = " places_api_key"
9192private const val LOCATION_KEY = " location_key"
9293private const val LAST_LOCATION_QUERY = " last_location_query"
9394private const val OPTIONS_HIDE_STREET = " street"
@@ -143,7 +144,7 @@ class LocationPickerActivity : AppCompatActivity(),
143144 private var shouldReturnOkOnBackPressed = false
144145 private var enableSatelliteView = true
145146 private var enableLocationPermissionRequest = true
146- private var isGooglePlacesEnabled = false
147+ private var googlePlacesApiKey : String? = null
147148 private var isGoogleTimeZoneEnabled = false
148149 private var searchZone: String? = null
149150 private var searchZoneRect: SearchZoneRect ? = null
@@ -247,7 +248,13 @@ class LocationPickerActivity : AppCompatActivity(),
247248 }
248249
249250 private fun setUpMainVariables () {
250- val placesDataSource = GooglePlacesDataSource (Places .getGeoDataClient(this ))
251+ var placesDataSource: GooglePlacesDataSource ? = null
252+ if (! Places .isInitialized() && ! googlePlacesApiKey.isNullOrEmpty()) {
253+ googlePlacesApiKey?.let {
254+ Places .initialize(applicationContext, it)
255+ }
256+ placesDataSource = GooglePlacesDataSource (Places .createClient(this ))
257+ }
251258 val geocoder = Geocoder (this , Locale .getDefault())
252259 apiInteractor = GoogleGeocoderDataSource (NetworkClient (), AddressBuilder ())
253260 val geocoderRepository = GeocoderRepository (AndroidGeocoderDataSource (geocoder), apiInteractor!! )
@@ -352,7 +359,7 @@ class LocationPickerActivity : AppCompatActivity(),
352359 updateAddressLayoutVisibility()
353360 updateVoiceSearchVisibility()
354361
355- if (isGooglePlacesEnabled ) {
362+ if (! googlePlacesApiKey.isNullOrEmpty() ) {
356363 geocoderPresenter?.enableGooglePlaces()
357364 }
358365 }
@@ -710,8 +717,8 @@ class LocationPickerActivity : AppCompatActivity(),
710717 if (savedInstanceState.keySet().contains(GEOLOC_API_KEY )) {
711718 apiInteractor?.setApiKey(savedInstanceState.getString(GEOLOC_API_KEY , " " ))
712719 }
713- if (savedInstanceState.keySet().contains(ENABLE_GOOGLE_PLACES )) {
714- isGooglePlacesEnabled = savedInstanceState.getBoolean( ENABLE_GOOGLE_PLACES , false )
720+ if (savedInstanceState.keySet().contains(PLACES_API_KEY )) {
721+ googlePlacesApiKey = savedInstanceState.getString( PLACES_API_KEY , " " )
715722 }
716723 if (savedInstanceState.keySet().contains(ENABLE_GOOGLE_TIME_ZONE )) {
717724 isGoogleTimeZoneEnabled = savedInstanceState.getBoolean(ENABLE_GOOGLE_TIME_ZONE , false )
@@ -778,8 +785,8 @@ class LocationPickerActivity : AppCompatActivity(),
778785 if (transitionBundle.keySet().contains(GEOLOC_API_KEY )) {
779786 apiInteractor!! .setApiKey(transitionBundle.getString(GEOLOC_API_KEY , " " ))
780787 }
781- if (transitionBundle.keySet().contains(ENABLE_GOOGLE_PLACES )) {
782- isGooglePlacesEnabled = transitionBundle.getBoolean( ENABLE_GOOGLE_PLACES , false )
788+ if (transitionBundle.keySet().contains(PLACES_API_KEY )) {
789+ googlePlacesApiKey = transitionBundle.getString( PLACES_API_KEY , " " )
783790 }
784791 if (transitionBundle.keySet().contains(ENABLE_GOOGLE_TIME_ZONE )) {
785792 isGoogleTimeZoneEnabled = transitionBundle.getBoolean(ENABLE_GOOGLE_TIME_ZONE , false )
@@ -1155,10 +1162,6 @@ class LocationPickerActivity : AppCompatActivity(),
11551162 .addOnConnectionFailedListener(this )
11561163 .addApi(LocationServices .API )
11571164
1158- if (isGooglePlacesEnabled) {
1159- googleApiClientBuilder.addApi(Places .GEO_DATA_API )
1160- }
1161-
11621165 googleApiClient = googleApiClientBuilder.build()
11631166 googleApiClient?.connect()
11641167 }
@@ -1215,7 +1218,7 @@ class LocationPickerActivity : AppCompatActivity(),
12151218 private var shouldReturnOkOnBackPressed = false
12161219 private var lekuPois: List <LekuPoi >? = null
12171220 private var geolocApiKey: String? = null
1218- private var googlePlacesEnabled = false
1221+ private var googlePlacesApiKey : String? = null
12191222 private var googleTimeZoneEnabled = false
12201223 private var voiceSearchEnabled = true
12211224 private var mapStyle: Int? = null
@@ -1285,8 +1288,8 @@ class LocationPickerActivity : AppCompatActivity(),
12851288 return this
12861289 }
12871290
1288- fun withGooglePlacesEnabled ( ): Builder {
1289- this .googlePlacesEnabled = true
1291+ fun withGooglePlacesApiKey ( apiKey : String ): Builder {
1292+ this .googlePlacesApiKey = apiKey
12901293 return this
12911294 }
12921295
@@ -1339,8 +1342,10 @@ class LocationPickerActivity : AppCompatActivity(),
13391342 geolocApiKey?.let {
13401343 intent.putExtra(GEOLOC_API_KEY , geolocApiKey)
13411344 }
1345+ googlePlacesApiKey?.let {
1346+ intent.putExtra(PLACES_API_KEY , googlePlacesApiKey)
1347+ }
13421348 mapStyle?.let { style -> intent.putExtra(MAP_STYLE , style) }
1343- intent.putExtra(ENABLE_GOOGLE_PLACES , googlePlacesEnabled)
13441349 intent.putExtra(ENABLE_GOOGLE_TIME_ZONE , googleTimeZoneEnabled)
13451350 intent.putExtra(ENABLE_VOICE_SEARCH , voiceSearchEnabled)
13461351 intent.putExtra(UNNAMED_ROAD_VISIBILITY , unnamedRoadVisible)
0 commit comments