Skip to content

Commit

Permalink
added service for reverse geocoding api integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ichchhie committed Feb 27, 2020
1 parent d80e20c commit b3b0243
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 89 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

//toaster library
implementation 'com.github.Ichchhie:Toaster-Library:0.3.0'
implementation 'com.github.Ichchhie:Toaster-Library:0.4.0'

implementation ('com.mapbox.mapboxsdk:mapbox-android-sdk:8.6.2'){
exclude group: 'group_name', module: 'module_name'
}

// implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.42.5'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.42.5'
// implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.42.5'
//graphhopper navigation
// implementation 'com.graphhopper.navigation:navigation-android:0.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kathmandulivinglabs.osmnavigationapp;

public interface Constants {
String TAG = "minion";
int REVERSE_GEO_CODE_RADIUS = 2;
String TOKEN = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJpY2hjaGhhIiwiZXhwIjoxNTgzMzgyOTM0LCJpYXQiOjE1ODE2NTQ5MzR9.MR0K8MBvoWWxvTIoaOJS3fwYyTkghEalv5yRVCrGh_4";
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.kathmandulivinglabs.osmnavigationapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.android.material.snackbar.Snackbar;
import com.kathmandulivinglabs.navigationlibrary.BaatoUtil;
import com.kathmandulivinglabs.navigationlibrary.ToasterMessage;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.kathmandulivinglabs.navigationlibrary.models.Geometry;
import com.kathmandulivinglabs.navigationlibrary.services.Baato;
import com.kathmandulivinglabs.navigationlibrary.services.ToasterMessage;
import com.kathmandulivinglabs.navigationlibrary.utilities.BaatoUtil;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;
Expand All @@ -22,7 +22,6 @@
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;

import java.util.List;
Expand All @@ -37,7 +36,7 @@ public class MainActivity extends AppCompatActivity implements OnMapReadyCallbac
private static final String TAG = "apple";
private MapboxMap mapboxMap;
private MapView mapView;
private NavigationMapRoute navigationMapRoute;
private NavigationRoute navigationMapRoute;
String encoded = "wv{gD_`lhOQvAO^c@h@Yj@IZKhADXj@|ABrAElB@NLLp@LHFDJJvDb@pBx@~B`AjBBP@fCArBW|KSxEQbHGbAYhBKVmA`C}A`EuBrGcAxC}@hBA^BJb@`@dBr@tBbAh@RdDbBs@fCYxAmBnKy@`F_Gj[]zBS~AWjDC~AA|BBbBj@|JpB~YRrDbAjONlDD~AAbCuArj@o@nSkB~w@CzB{@x\\e@rLa@jN@zBO|DE|EQjBKp@W~@Sf@o@dAs@x@i@d@_@RkAj@_AP_AJmCDkBD}IDoFF_ETc@@iARyAf@UKa@_@YKk@KuBGu@Kq@MkA[k@SsB{@wCwAgAo@g@SsDiBa@OcJmE{IcEiAm@kJmEmAc@U?QA[IUK]EmI_@wBCk@Dm@FiCt@k@ViNvJ_BlAcAj@{@Xc@JuALsGN{@F_@Da@NeAn@WXWb@]fAIf@Al@Bt@`AvKVhDDpAMjB[jASb@yAzBiAvAuCfEYxAe@SmE}AsGeA_FiBwG_CsB_@sGq@{AWcA]eASuC{@g@IiDs@_BWy@FiG`BqCx@sFvA{JlC_APq@DqA?[CcAOgCg@qFqAgGkA{@Gk@@i@Dg@Je@NmAl@_^~R^p@lAlAdAv@^T~Al@t@l@F?b@MjChANBl@ATIDCFl@APNH?Ho@`CTf@HJ`@FJN?Je@fA[d@CJBJHJ@POv@HPFVd@ZBJAJa@v@bAf@JjAw@`@yAf@s@d@SHQAAf@c@C@p@YAm@R?n@SPkAPAB?|@g@HCLaAB";

@Override
Expand All @@ -50,6 +49,7 @@ protected void onCreate(Bundle savedInstanceState) {
mapView.getMapAsync(this);
ToasterMessage.s(this, "Hello Good Morning");
Geometry geometry = BaatoUtil.getGeoJsonFromEncodedPolyLine(encoded);
Log.d(TAG, "onCreate: " + Baato.search(Constants.TOKEN, "chardhunga").toString());
NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken())
.origin(Point.fromLngLat(85.4278774, 27.6721352))
Expand All @@ -70,9 +70,9 @@ public void onMapReady(@NonNull MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> {
initializeLocationComponent(mapboxMap);
navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap);
// navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap);
mapboxMap.addOnMapLongClickListener(this);
Snackbar.make(mapView, "Long press to select route", Snackbar.LENGTH_SHORT).show();
// Snackbar.make(mapView, "Long press to select route", Snackbar.LENGTH_SHORT).show();
});
}

Expand All @@ -92,7 +92,7 @@ public void onResponse(Call<DirectionsResponse> call, Response<DirectionsRespons
&& response.body() != null
&& !response.body().routes().isEmpty()) {
List<DirectionsRoute> routes = response.body().routes();
navigationMapRoute.addRoutes(routes);
// navigationMapRoute.addRoutes(routes);
}
}

Expand All @@ -111,18 +111,18 @@ public void onResume() {
protected void onStart() {
super.onStart();
mapView.onStart();
if (navigationMapRoute != null) {
navigationMapRoute.onStart();
}
// if (navigationMapRoute != null) {
// navigationMapRoute.onStart();
// }
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
if (navigationMapRoute != null) {
navigationMapRoute.onStop();
}
// if (navigationMapRoute != null) {
// navigationMapRoute.onStop();
// }
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.kathmandulivinglabs.navigationlibrary.models;

public class Geocode {
public double lat,lon;

public Geocode(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Place {
private Integer searchRank;
private Integer addressRank;
private String geometryType;
private Centroid centroid;
private Geocode centroid;

private List<String> tags;
private String country;
Expand Down Expand Up @@ -110,11 +110,11 @@ public void setGeometryType(String geometryType) {
this.geometryType = geometryType;
}

public Centroid getCentroid() {
public Geocode getCentroid() {
return centroid;
}

public void setCentroid(Centroid centroid) {
public void setCentroid(Geocode centroid) {
this.centroid = centroid;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.kathmandulivinglabs.navigationlibrary.responses;

public class ReverseGeoCodeResponse {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import static android.content.ContentValues.TAG;

public class Baato {
public static Object search(String api_key, String query) {
Search search = new Search(api_key, query);
Log.d(TAG, "search:string " + search.doSearch().equals(String.class));
Log.d(TAG, "search:list " + search.doSearch().equals(List.class));
return search.doSearch();
}
// public static Object search(String api_key, String query) {
// SearchQuery searchQuery = new SearchQuery(api_key, query);
// Log.d(TAG, "search:string " + searchQuery.doSearch().equals(String.class));
// Log.d(TAG, "search:list " + searchQuery.doSearch().equals(List.class));
// return searchQuery.doSearch();
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.kathmandulivinglabs.navigationlibrary.services;

import android.content.Context;
import android.location.Geocoder;

import androidx.annotation.NonNull;

import com.kathmandulivinglabs.navigationlibrary.application.App;
import com.kathmandulivinglabs.navigationlibrary.models.Geocode;
import com.kathmandulivinglabs.navigationlibrary.models.Geometry;
import com.kathmandulivinglabs.navigationlibrary.models.Place;
import com.kathmandulivinglabs.navigationlibrary.requests.QueryAPI;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BaatoReverseGeoCodeService {
private Context context;
private BaatoReverseGeoCodeRequestListener baatoSearchRequestListener;
private String accessToken;
private int radius;
private Geocode geocode;

public interface BaatoReverseGeoCodeRequestListener {
/**
* onSuccess method called after it is successful
* onFailed method called if it can't places
*/
void onSuccess(List<Place> places);

void onFailed(Throwable error);
}

public BaatoReverseGeoCodeService(Context context) {
this.context = context;
}

/**
* Set the accessToken.
*/
public BaatoReverseGeoCodeService setAccessToken(@NonNull String accessToken) {
this.accessToken = accessToken;
return this;
}

/**
* Set the query to search.
*/
public BaatoReverseGeoCodeService setGeoCode(@NonNull Geocode geoCode) {
this.geocode = geoCode;
return this;
}

public BaatoReverseGeoCodeService setRadius(@NonNull int radius) {
this.radius = radius;
return this;
}

/**
* Method to set the UpdateListener for the AppUpdaterUtils actions
*
* @param baatoSearchRequestListener the listener to be notified
* @return this
*/
public BaatoReverseGeoCodeService withListener(BaatoReverseGeoCodeRequestListener baatoSearchRequestListener) {
this.baatoSearchRequestListener = baatoSearchRequestListener;
return this;
}

public void doReverseGeoCode() {
QueryAPI queryAPI = App.retrofit(accessToken).create(QueryAPI.class);
queryAPI.performReverseGeoCode(geocode.lat, geocode.lon, radius).enqueue(new Callback<List<Place>>() {
@Override
public void onResponse(Call<List<Place>> call, Response<List<Place>> response) {
if (response.isSuccessful() && response.body() != null)
baatoSearchRequestListener.onSuccess(response.body());
else {
try {
baatoSearchRequestListener.onFailed(new Throwable(response.errorBody().string()));
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public void onFailure(Call<List<Place>> call, Throwable throwable) {
baatoSearchRequestListener.onFailed(throwable);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.kathmandulivinglabs.navigationlibrary.services;

import android.content.Context;

import androidx.annotation.NonNull;

import com.kathmandulivinglabs.navigationlibrary.application.App;
import com.kathmandulivinglabs.navigationlibrary.models.Place;
import com.kathmandulivinglabs.navigationlibrary.requests.QueryAPI;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BaatoSearchService {
private Context context;
private BaatoSearchRequestListener baatoSearchRequestListener;
private String accessToken, query;

public interface BaatoSearchRequestListener {
/**
* onSuccess method called after it is successful
* onFailed method called if it can't places
*/
void onSuccess(List<Place> places);

void onFailed(Throwable error);
}

public BaatoSearchService(Context context) {
this.context = context;
}

/**
* Set the accessToken.
*/
public BaatoSearchService setAccessToken(@NonNull String accessToken) {
this.accessToken = accessToken;
return this;
}

/**
* Set the query to search.
*/
public BaatoSearchService setQuery(@NonNull String query) {
this.query = query;
return this;
}

/**
* Method to set the UpdateListener for the AppUpdaterUtils actions
*
* @param baatoSearchRequestListener the listener to be notified
* @return this
*/
public BaatoSearchService withListener(BaatoSearchRequestListener baatoSearchRequestListener) {
this.baatoSearchRequestListener = baatoSearchRequestListener;
return this;
}

public void doSearch() {
QueryAPI queryAPI = App.retrofit(accessToken).create(QueryAPI.class);
queryAPI.searchQuery(query).enqueue(new Callback<List<Place>>() {
@Override
public void onResponse(Call<List<Place>> call, Response<List<Place>> response) {
if (response.isSuccessful() && response.body() != null)
baatoSearchRequestListener.onSuccess(response.body());
else{
try {
baatoSearchRequestListener.onFailed(new Throwable(response.errorBody().string()));
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public void onFailure(Call<List<Place>> call, Throwable throwable) {
baatoSearchRequestListener.onFailed(throwable);
}
});
}
}
Loading

0 comments on commit b3b0243

Please sign in to comment.