Skip to content

Commit

Permalink
Places autocomplete example (#215)
Browse files Browse the repository at this point in the history
* Rename module library to mapzen-android-sdk

* Rename module sample to mapzen-android-sdk-sample

* Add core module

* Add places module

* Deploy places module

* mapzen-android-sdk & mapzen-places-api depend on core module

* Create places sample app module

* Update sample app deploy scripts

* Move sample projects into samples

* Add GeoDataApi and autocomplete method

* Add Places api and GeoDataApi implementation

* Add LatLng

* Add checkstyle task

* Implement LatLngBounds methods

* Better LatLng initialization

* Add AutocompletePredictionResult

* Return AutocompletePendingResult when GeoDataApi autocomplete called

* Add AutocompletePrediction

* Add DataBuffer interface

* Update AutocompletePredictionBuffer to implement DataBuffer

* Map pelias results to AutocompletePrediction objs

* Checkstyle and javadocs

* Add GeoDataApi autocomplete example

* Add verify task to places

* Rm gitignores

* Fix test

* dont abort on lint errors for places module
  • Loading branch information
sarahsnow1 authored and ecgreb committed Dec 14, 2016
1 parent 61968d0 commit b38aa14
Show file tree
Hide file tree
Showing 29 changed files with 1,056 additions and 38 deletions.
1 change: 1 addition & 0 deletions config/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<suppress checks="JavadocType" files=".*src/test/java/*" />
<suppress checks="JavadocStyle" files=".*src/test/java/*" />
<suppress checks="VisibilityModifier" files=".*src/test/java/*" />
<suppress checks="ConstantName" files="src/main/java/com/mapzen/places/api/Places.java"/>
</suppressions>
9 changes: 8 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,12 @@ task verify(dependsOn: ['compileDebugSources',
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')

dependencies {

compile 'com.mapzen.android:lost:2.1.2'
compile 'com.mapzen.android:pelias-android-sdk:1.0.0'

testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:1.7.1'
testCompile 'org.powermock:powermock:1.6.4'
testCompile 'org.powermock:powermock-module-junit4:1.6.4'
testCompile 'org.powermock:powermock-api-mockito:1.6.4'
}
3 changes: 1 addition & 2 deletions mapzen-android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ dependencies {
compile 'com.mapzen:mapzen-core:0.0.1-SNAPSHOT'

compile "com.mapzen.tangram:tangram:$tangram_version"
compile 'com.mapzen.android:lost:2.1.2'
compile 'com.mapzen:on-the-road:1.1.1'
compile 'com.mapzen.android:pelias-android-sdk:1.0.0'

compile 'com.google.dagger:dagger:2.0'
compile 'javax.annotation:javax.annotation-api:1.2'

Expand Down
22 changes: 20 additions & 2 deletions mapzen-places-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ release {

afterReleaseBuild.dependsOn uploadArchives


android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
Expand All @@ -51,6 +50,9 @@ android {
testOptions {
unitTests.returnDefaultValues = true
}
lintOptions {
abortOnError false
}
}

tasks.withType(Test) {
Expand All @@ -61,13 +63,29 @@ tasks.withType(Test) {
}
}

task checkstyle(type: Checkstyle) {
configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
source 'src'
include '**/*.java'
exclude '**/gen/**'

classpath = files()
}

task verify(dependsOn: ['compileDebugSources',
'test',
'checkstyle',
'lint'])

dependencies {
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.mapzen:mapzen-core:0.0.1-SNAPSHOT'

testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:1.7.1'
testCompile 'org.powermock:powermock:1.6.4'
testCompile 'org.powermock:powermock-module-junit4:1.6.4'
testCompile 'org.powermock:powermock-api-mockito:1.6.4'
}


apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mapzen.places.api;

/**
* Filter for customizing autocomplete results from {@link GeoDataApi}.
*/
public class AutocompleteFilter {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.mapzen.places.api;

import android.support.annotation.Nullable;
import android.text.style.CharacterStyle;

import java.util.List;

/**
* Represents a place returned from {@link GeoDataApi#getAutocompletePredictions(
* com.mapzen.android.lost.api.LostApiClient, String, LatLngBounds, AutocompleteFilter)}.
*/
public class AutocompletePrediction {

private final String placeId;
private final String primaryText;

/**
* Constructs a new prediction given an id an primary text.
* @param id
* @param text
*/
public AutocompletePrediction(String id, String text) {
placeId = id;
primaryText = text;
}

/**
* Not implemented yet.
* @param characterStyle
* @return
*/
public CharSequence getFullText(@Nullable CharacterStyle characterStyle) {
throw new RuntimeException("Not implemented yet");
}

/**
* Returns the prediction's primary text.
* @param characterStyle
* @return
*/
public CharSequence getPrimaryText(@Nullable CharacterStyle characterStyle) {
return primaryText;
}

/**
* Not implemented yet.
* @param characterStyle
* @return
*/
public CharSequence getSecondaryText(@Nullable CharacterStyle characterStyle) {
throw new RuntimeException("Not implemented yet");
}

/**
* Return's the prediction's id.
* @return
*/
@Nullable
public String getPlaceId() {
return placeId;
}

/**
* Not implemented yet.
* @return
*/
@Nullable
public List<Integer> getPlaceTypes() {
throw new RuntimeException("Not implemented yet");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mapzen.places.api;

import com.mapzen.android.lost.api.Result;
import com.mapzen.android.lost.api.Status;

import java.util.List;

/**
* Represents a list of autocomplete results.
*/
public class AutocompletePredictionBuffer implements Result, DataBuffer<AutocompletePrediction> {

private final Status status;
private final List<AutocompletePrediction> predictions;

/**
* Constructs a new buffer given a status and list of autocomplete results.
* @param status
* @param predictions
*/
public AutocompletePredictionBuffer(Status status, List<AutocompletePrediction> predictions) {
this.status = status;
this.predictions = predictions;
}

@Override public Status getStatus() {
return status;
}

@Override public int getCount() {
if (predictions == null) {
return 0;
}
return predictions.size();
}

@Override public AutocompletePrediction get(int index) {
if (predictions == null || index < 0 || index > predictions.size() - 1) {
return null;
}
return predictions.get(index);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mapzen.places.api;

/**
* Generic interface for representing data contained in a buffer.
* @param <T>
*/
public interface DataBuffer<T> {
/**
* Returns the number of objects in the buffer.
* @return
*/
int getCount();

/**
* Returns the object at a given index.
* @param index
* @return
*/
T get(int index);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mapzen.places.api;

import com.mapzen.android.lost.api.LostApiClient;
import com.mapzen.android.lost.api.PendingResult;

/**
* Main entry point for the Mapzen Places Geo Data API.
*/
public interface GeoDataApi {
/**
* Returns an object which can be used to retrieve autocomplete results.
* @param client
* @param query
* @param bounds
* @param filter
* @return
*/
PendingResult<AutocompletePredictionBuffer> getAutocompletePredictions(LostApiClient client,
String query, LatLngBounds bounds, AutocompleteFilter filter);
}
46 changes: 46 additions & 0 deletions mapzen-places-api/src/main/java/com/mapzen/places/api/LatLng.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.mapzen.places.api;

/**
* Represents a pair of coordinates as degrees.
*/
public class LatLng {

private static final double LAT_MIN = -90;
private static final double LAT_MAX = 90;
private static final double LNG_MIN = -180;
private static final double LNG_MAX = 180;
private static final double ALL_LNGS = 360;

private final double latitude;
private final double longitude;

/**
* Constructs a new object given a latitude and longitude in degrees.
* @param lat
* @param lng
*/
public LatLng(double lat, double lng) {
if (LNG_MIN <= lng && lng < LNG_MAX) {
this.longitude = lng;
} else {
this.longitude = ((lng - LNG_MAX) % ALL_LNGS + ALL_LNGS) % ALL_LNGS - LNG_MAX;
}
this.latitude = Math.max(LAT_MIN, Math.min(LAT_MAX, lat));
}

/**
* Latitude, in degrees. This value is in the range [-90, 90].
* @return
*/
public double getLatitude() {
return latitude;
}

/**
* Longitude, in degrees. This value is in the range [-180, 180].
* @return
*/
public double getLongitude() {
return longitude;
}
}
Loading

0 comments on commit b38aa14

Please sign in to comment.