Skip to content

Commit

Permalink
Google Places Integration (#139)
Browse files Browse the repository at this point in the history
* Added Google Places

* Added Google Places data source to Geocoder Presenter

* Added places in builder and now working

* Only take 3 results

* extracted code

* Zipped request results

* removed unused imports

* commneted places for the sample

* Chnaged version and added places documentation to Readme

* changed text

* Geocoder Presenter documentation updated

* Updated sample version string and tweet message
  • Loading branch information
ferranpons authored Jan 19, 2018
1 parent 8d1e2ef commit fd0e4d6
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build/
local.properties
project.properties
app/src/debug/
app/src/debug/*.xml

.idea/.name
.idea/*.xml
Expand Down
54 changes: 45 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[![Build Status](https://travis-ci.org/SchibstedSpain/Leku.svg?branch=master)](https://travis-ci.org/SchibstedSpain/Leku) [ ![Bintray](https://api.bintray.com/packages/schibstedspain/maven/leku/images/download.svg) ](https://bintray.com/schibstedspain/maven/leku/_latestVersion)

Location picker component for Android that uses Google Maps and returns a latitude, longitude and an address based on the location picked in the LocationPickerActivity provided.
Component library for Android that uses Google Maps and returns a latitude, longitude and an address based on the location picked with the Activity provided.
</div>

<br/>
Expand Down Expand Up @@ -47,6 +47,7 @@ Location picker component for Android that uses Google Maps and returns a latitu
* Search by voice
* Search by text
* Geo Location by GPS, network
* Google Places
* Pick locations using "touch" gestures on the map
* Customization (Theme and layout)
* Events Tracking
Expand All @@ -57,9 +58,9 @@ Location picker component for Android that uses Google Maps and returns a latitu

### Prerequisites

minSdkVersion >= 15
Google Play Services = 11.4.2
Support Library = 26.1.0
minSdkVersion >= 15<br/>
Google Play Services = 11.8.0<br/>
Support Library = 27.0.2

### Download

Expand All @@ -75,14 +76,14 @@ Include the dependency in your app `build.gradle`:

```groovy
dependencies {
implementation 'com.schibstedspain.android:leku:3.7.0'
implementation 'com.schibstedspain.android:leku:4.0.0'
}
```

Alternatively, if you are using a different version of Google Play Services than `11.8.0` use this instead:

```groovy
implementation ('com.schibstedspain.android:leku:3.6.2') {
implementation ('com.schibstedspain.android:leku:4.0.0') {
exclude group: 'com.google.android.gms'
exclude group: 'com.android.support'
}
Expand Down Expand Up @@ -137,7 +138,7 @@ To use the LocationPickerActivity first you need to add these lines to your Andr
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
android:resource="@xml/leku_searchable" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
Expand Down Expand Up @@ -196,6 +197,38 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
That's all folks!


#### Google Places

Leku now supports Google Places queries using the search box. If you want to enable it these are the steps you need to follow:

1. You need to replace your old `com.google.android.maps.v2.API_KEY` meta-data for the `com.google.android.geo.API_KEY`

```xml
<!-- Use this if only using Maps and not Places
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key"
/>
-->

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key"/>
```

2. Enable Google Places API for Android on your [google developer console](https://console.developers.google.com/).

3. Enable it when instantiating LocationPickerActivity by adding `.withGooglePlacesEnabled()`:

```java
Intent intent = new LocationPickerActivity.Builder()
**.withGooglePlacesEnabled()**
.build(getApplicationContext());
```

And you are good to go. :)


#### Localization

If you would like to add more language translations the only thing you have to do is:
Expand Down Expand Up @@ -358,8 +391,11 @@ private GeocoderPresenter geocoderPresenter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
***
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
geocoderPresenter = new GeocoderPresenter(getApplicationContext(), new GeocoderInteractor(geocoder));
GooglePlacesDataSource placesDataSource = new GooglePlacesDataSource(Places.getGeoDataClient(this, null));
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
apiInteractor = new GoogleGeocoderDataSource(new NetworkClient(), new AddressBuilder());
GeocoderRepository geocoderRepository = new GeocoderRepository(new AndroidGeocoderDataSource(geocoder), apiInteractor);
geocoderPresenter = new GeocoderPresenter(new ReactiveLocationProvider(getApplicationContext()), geocoderRepository, placesDataSource);
geocoderPresenter.setUI(this);
***
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@
android:value="@integer/google_play_services_version"
/>

<!-- Use this if only using Maps and not Places
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key"
/>
-->

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key"/>

<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void onClick(View view) {
//.withCityHidden()
//.withZipCodeHidden()
//.withSatelliteViewHidden()
//.withGooglePlacesEnabled()
.build(getApplicationContext());

//this is optional if you want to return RESULT_OK if you don't set the latitude/longitude and click back button
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<string name="google_maps_key"></string>
<string name="launch_map_picker" translatable="false">LAUNCH MAP LOCATION PICKER ACTIVITY</string>
<string name="launch_map_picker_with_pois" translatable="false">LAUNCH MAP WITH POIS</string>
<string name="leku_lib_version">version 3.7.0</string>
<string name="leku_lib_version">version 4.0.0</string>
</resources>
5 changes: 3 additions & 2 deletions leku/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ version = "${versionMajor}.${versionMinor}.${versionPatch}"

android {
compileSdkVersion 27
buildToolsVersion '27.0.2'
buildToolsVersion '27.0.3'
resourcePrefix 'leku_'

defaultConfig {
Expand Down Expand Up @@ -56,6 +56,7 @@ dependencies {
def playServicesVersion = '11.8.0'
implementation "com.google.android.gms:play-services-maps:$playServicesVersion"
implementation "com.google.android.gms:play-services-location:$playServicesVersion"
implementation "com.google.android.gms:play-services-places:$playServicesVersion"

implementation 'pl.charmas.android:android-reactive-location2:2.0@aar'

Expand Down Expand Up @@ -86,7 +87,7 @@ twitterPlugin {
consumerSecret = System.getenv()['TWITTER_API_SECRET']
accessToken = System.getenv()['TWITTER_ACCESS_TOKEN']
accessTokenSecret = System.getenv()['TWITTER_ACCESS_TOKEN_SECRET']
message = "New Leku version ${versionMajor}.${versionMinor}.${versionPatch} available for download. Check out the details at https://github.com/SchibstedSpain/Leku"
message = "New Leku version ${versionMajor}.${versionMinor}.${versionPatch} available for download. Check out the details at https://github.com/SchibstedSpain/Leku @SchibstedEng #Android #library #AndroidDev"
}

def getVersionMajor() {
Expand Down
2 changes: 1 addition & 1 deletion leku/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- YOU NEED TO INCLUDE THIS CODE IN YOUR APP
<activity
android:name="com.schibstedspain.leku.LocationPickerActivity"
android:label="@string/title_activity_location_picker"
android:label="@string/leku_title_activity_location_picker"
android:windowSoftInputMode="adjustPan"
android:parentActivityName=".MainActivity">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
Expand All @@ -51,6 +52,7 @@
import com.schibstedspain.leku.geocoder.GeocoderViewInterface;
import com.schibstedspain.leku.geocoder.api.AddressBuilder;
import com.schibstedspain.leku.geocoder.api.NetworkClient;
import com.schibstedspain.leku.geocoder.places.GooglePlacesDataSource;
import com.schibstedspain.leku.permissions.PermissionUtils;
import com.schibstedspain.leku.tracker.TrackEvents;
import java.util.ArrayList;
Expand Down Expand Up @@ -79,6 +81,7 @@ public class LocationPickerActivity extends AppCompatActivity
public static final String BACK_PRESSED_RETURN_OK = "back_pressed_return_ok";
public static final String ENABLE_SATELLITE_VIEW = "enable_satellite_view";
public static final String ENABLE_LOCATION_PERMISSION_REQUEST = "enable_location_permission_request";
public static final String ENABLE_GOOGLE_PLACES = "enable_google_places";
public static final String POIS_LIST = "pois_list";
public static final String LEKU_POI = "leku_poi";
private static final String GEOLOC_API_KEY = "geoloc_api_key";
Expand Down Expand Up @@ -126,6 +129,7 @@ public class LocationPickerActivity extends AppCompatActivity
private boolean shouldReturnOkOnBackPressed = false;
private boolean enableSatelliteView = true;
private boolean enableLocationPermissionRequest = true;
private boolean isGooglePlacesEnabled = false;
private String searchZone;
private List<LekuPoi> poisList;
private Map<String, LekuPoi> lekuPoisMarkersMap;
Expand Down Expand Up @@ -160,10 +164,11 @@ protected void track(TrackEvents event) {
}

private void setUpMainVariables() {
GooglePlacesDataSource placesDataSource = new GooglePlacesDataSource(Places.getGeoDataClient(this, null));
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
apiInteractor = new GoogleGeocoderDataSource(new NetworkClient(), new AddressBuilder());
GeocoderRepository geocoderRepository = new GeocoderRepository(new AndroidGeocoderDataSource(geocoder), apiInteractor);
geocoderPresenter = new GeocoderPresenter(new ReactiveLocationProvider(getApplicationContext()), geocoderRepository);
geocoderPresenter = new GeocoderPresenter(new ReactiveLocationProvider(getApplicationContext()), geocoderRepository, placesDataSource);
geocoderPresenter.setUI(this);
progressBar = findViewById(R.id.loading_progress_bar);
progressBar.setVisibility(View.GONE);
Expand Down Expand Up @@ -287,6 +292,10 @@ private void updateValuesFromBundle(Bundle savedInstanceState) {
getSavedInstanceParams(savedInstanceState);
}
updateAddressLayoutVisibility();

if (isGooglePlacesEnabled) {
geocoderPresenter.enableGooglePlaces();
}
}

private void setUpMapIfNeeded() {
Expand Down Expand Up @@ -668,6 +677,9 @@ private void getSavedInstanceParams(Bundle savedInstanceState) {
if (savedInstanceState.keySet().contains(GEOLOC_API_KEY)) {
apiInteractor.setApiKey(savedInstanceState.getString(GEOLOC_API_KEY));
}
if (savedInstanceState.keySet().contains(ENABLE_GOOGLE_PLACES)) {
isGooglePlacesEnabled = savedInstanceState.getBoolean(ENABLE_GOOGLE_PLACES, false);
}
if (savedInstanceState.keySet().contains(SEARCH_ZONE)) {
searchZone = savedInstanceState.getString(SEARCH_ZONE);
}
Expand Down Expand Up @@ -709,6 +721,9 @@ private void getTransitionBundleParams(Bundle transitionBundle) {
if (transitionBundle.keySet().contains(GEOLOC_API_KEY)) {
apiInteractor.setApiKey(transitionBundle.getString(GEOLOC_API_KEY));
}
if (transitionBundle.keySet().contains(ENABLE_GOOGLE_PLACES)) {
isGooglePlacesEnabled = transitionBundle.getBoolean(ENABLE_GOOGLE_PLACES, false);
}
}

private void setLayoutVisibilityFromBundle(Bundle transitionBundle) {
Expand Down Expand Up @@ -1027,10 +1042,15 @@ private void centerToPoi(LekuPoi lekuPoi) {
}

private synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
GoogleApiClient.Builder googleApiClientBuilder = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
.addApi(LocationServices.API);

if (isGooglePlacesEnabled) {
googleApiClientBuilder.addApi(Places.GEO_DATA_API);
}

googleApiClient = googleApiClientBuilder.build();
googleApiClient.connect();
}

Expand Down Expand Up @@ -1082,6 +1102,7 @@ public static class Builder {
private boolean shouldReturnOkOnBackPressed = false;
private List<LekuPoi> lekuPois;
private String geolocApiKey = null;
private boolean googlePlacesEnabled = false;

public Builder() {
}
Expand Down Expand Up @@ -1140,6 +1161,11 @@ public Builder withGeolocApiKey(String apiKey) {
return this;
}

public Builder withGooglePlacesEnabled() {
this.googlePlacesEnabled = true;
return this;
}

public Intent build(Context context) {
Intent intent = new Intent(context, LocationPickerActivity.class);

Expand All @@ -1163,6 +1189,7 @@ public Intent build(Context context) {
if (geolocApiKey != null) {
intent.putExtra(GEOLOC_API_KEY, geolocApiKey);
}
intent.putExtra(ENABLE_GOOGLE_PLACES, googlePlacesEnabled);

return intent;
}
Expand Down
Loading

0 comments on commit fd0e4d6

Please sign in to comment.