How do I calculate path between two map points in Android? - android

I am working on iOS/Android map application.
To find a path between two locations in iOS, I am using MKDirectionsRequest.
How do I do it in Android?
I found Directions API, which is a Web service. So that I will have to send HTTP requests.
Isn't there any Java interface to calculate directions in Android?

To set this as an answer.
You can use Android-GoogleDirectionLibrary
As such:
GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
.from(new LatLng(37.7681994, -122.444538))
.to(new LatLng(37.7749003,-122.4034934))
.avoid(AvoidType.FERRIES)
.avoid(AvoidType.HIGHWAYS)
.execute(new DirectionCallback() {
#Override
public void onDirectionSuccess(Direction direction, String rawBody) {
if(direction.isOK()) {
// Do something
} else {
// Do something
}
}
#Override
public void onDirectionFailure(Throwable t) {
// Do something
}
});

Related

Android get properties for polygon from GeoJson

Im adding GeoJsonLayer to Google map on my application. This json contains coordinates for polygon and properties for each polygon. What I need is to get those properties when polygon is clicked. This is the code Im using to achieve that.
GeoJsonLayer jsonLayer = new GeoJsonLayer(mMap, geoJSON);
jsonLayer.addLayerToMap();
jsonLayer.setOnFeatureClickListener(new GeoJsonLayer.GeoJsonOnFeatureClickListener() {
#Override
public void onFeatureClick(final com.google.maps.android.data.Feature feature) {
mMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
#Override
public void onPolygonClick(Polygon polygon) {
for (Object s : feature.getProperties()) {
Log.d("getProperties", "getProperties = " + s.toString());
}
}
});
}
});
The problem is that it returns all properties. I want it to return properties only for the polygon I clicked.
Also its possible to draw polygons with user input. When I click on those polygons it triggers onPolygonClick() inside jsonLayer.setOnFeatureClickListener(). How to check if clicked polygon is from jsonLayer?

Android: find fastest route between 2 or more points depeding on streets in GoogleMaps

Am adding markers in my app on GoogleMaps,my markers allways are on streets.For example I have moore than 2 markers on map, I need to draw optimal route between them depending on streets.Is there any service to help me, or I need to create my own algorithm? Until now I worked a little with Google Elevation API and Google Direction API, but now I need Google Roads API, to find optimal roads between points? It must be something simmilar to my needs.
Consider using following library: https://github.com/akexorcist/Android-GoogleDirectionLibrary
GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
.from(new LatLng(37.7681994, -122.444538))
.to(new LatLng(37.7749003,-122.4034934))
.avoid(AvoidType.FERRIES)
.avoid(AvoidType.HIGHWAYS)
.execute(new DirectionCallback() {
#Override
public void onDirectionSuccess(Direction direction, String rawBody) {
if(direction.isOK()) {
// Do something
} else {
// Do something
}
}
#Override
public void onDirectionFailure(Throwable t) {
// Do something
}
});
Gradle
compile 'com.akexorcist:googledirectionlibrary:1.0.5'

RxAnadroid concatenating streams

I'm making a simple weather app to learn RxAndroid and I'm faced with the following issue.
I first load cities I'm interested in and then ask for the weather of each one of them.
getCitiesUseCase returns an Observable<List<City>> that I load from the data base. I send that list of cities to my view to display them and then ask for the weather individually (flatmap) inside the subscriber.
Subscription subscription = getCitiesUseCase.execute().flatMap(new Func1<List<City>, Observable<City>>() {
#Override
public Observable<City> call(List<City> cities) {
citiesView.addCities(cities);
return Observable.from(cities);
}
}).subscribe(new Subscriber<City>() {
#Override
public void onCompleted() {
subscriptions.remove(this);
this.unsubscribe();
}
#Override
public void onError(Throwable e) {
Log.e(this.getClass().getSimpleName(), e.toString());
}
#Override
public void onNext(City city) {
getCityWeatherUseCase.setLatLon(city.getLat().toString(), city.getLon().toString(), city.getId());
getCityWeather(city);
}
});
subscriptions.add(subscription);
Now the getCityWeather() method looks like this:
private void getCityWeather(final City city) {
subscriptions.add(getCityWeatherUseCase.execute().subscribe(new Subscriber<CityWeather>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
Log.e("error", e.toString());
}
#Override
public void onNext(CityWeather cityWeather) {
city.setCityWeather(cityWeather);
citiesView.updateCity(city);
}
}));
}
Everything works fine and as expected, but the fact that I'm subscribing to an observer inside a subcriber doesnt feel right. I know rxJava lets you play around with subscribers to prevent this kind of things but I really dont know how to improve my code further. Keep in mind that I need a city in order to ask for its weather.
Merry chrismas!
One approach could be the following. (I'm using retrolambda - so wherever you see ->, just replace with a new anonymous inner class).
Note that I'm using flatMap to spin up the weather data requests, rather than Observable.concat like your question suggests. The reason for this is that your scheduler (e.g. io()) will handle these in parallel and send the results through when they are available. However, with Observable.concat, these requests would be serialized so they'd be forced to happen one at a time - nullifying the benefits of a thread pool like io().
private class City {
public String name;
public City(String name) {
this.name = name;
}
public void setWeather(Weather weather) { /*...*/ }
}
private class Weather {
public String status;
public Weather(String status) {
this.status = status;
}
}
private Observable<Weather> getWeather(City city) {
// call your weather API here..
return Observable.just(new Weather("Sunny"));
}
#Test
public void test() {
Observable<List<City>> citiesObs = Observable.create(new Observable.OnSubscribe<List<City>>() {
#Override
public void call(Subscriber<? super List<City>> subscriber) {
// do work
final List<City> cities = new ArrayList<>();
cities.add(new City("Paris"));
cities.add(new City("Tokyo"));
cities.add(new City("Oslo"));
// send results
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(cities);
subscriber.onCompleted();
}
}
});
Observable<City> obs = citiesObs
// inject a side effect
.doOnNext(list -> {
// pass `list` to your view here
})
// turn Observable<Iterable<T>> into Observable<T>
.flatMapIterable(list -> list)
// Map a city to an observable that fetches Weather data
// Your scheduler can take care of these at once.
.flatMap(city -> {
return getWeather(city)
// another side effect
.doOnNext(weather -> {
city.setWeather(weather);
})
// map baack to city, just for the heck of it
.map($ -> city);
});
TestSubscriber sub = TestSubscriber.create();
obs.subscribe(sub);
sub.awaitTerminalEvent();
sub.assertValueCount(3);
}
Also note that in order to take advantage of io(), you'd need to add a call to subscribeOn(Schedulers.io()) to tell the observable to begin doing work on the io thread pool. When you want to pass control to another thread, for example your view, you could insert a observeOn(AndroidSchedulers.mainThread()) before your side-effect (or mapping). If you want to bounce control back to the background thread(s) for your weather calls, you could then add another call to observeOn(Schedulers.io()) right before you flatMap to getWeather(City).

Android ARCGIS Shape file labeling polylines

I have my ARCGIS android app displaying a polyline shapefile file - a roading network in this case.
The shape file has a field that contains the name of the road "ROAD_NAME' - the question i have is how do i create a layer to have the road name automatically displayed?
cheers
B
I also tried to display labels on the map.
By my experiment, there are 2 ways. I realized it.One way is that draw those labels programmatically on the map. but it needs a lot of effort. I did it, it is not pretty. I think so.
public class LabelLayer extends GraphicsLayer {
...
private void showLabel(final FeatureLayer fly, final QueryParameters query,final String labelField) {
new Thread(new Runnable() {
#Override
public void run() {
fly.getFeatureTable().queryFeatures(query, new CallbackListener<FeatureResult>() {
#Override
public void onCallback(FeatureResult objects) {
Iterator<Object> fet=objects.iterator();
while (fet.hasNext()){
Feature f=(Feature) fet.next();
Geometry geom= f.getGeometry();
Point p=null;
switch (geom.getType()){
case POINT:p=(Point)geom;break;
case POLYLINE: Polyline polyline=(Polyline)geom;break;
case POLYGON: p= GeometryEngine.getLabelPointForPolygon((Polygon)geom,mapView.getSpatialReference());break;
default:break;
}
txtSymbol.setFontWeight(FontWeight.BOLD);
txtSymbol.setColor(Color.DKGRAY);
txtSymbol.setText(f.getAttributeValue(labelField).toString());
Graphic gr = new Graphic(p, txtSymbol);
LabelLayer.this.addGraphic(gr);
}
}
#Override
public void onError(Throwable throwable) {
}
});
}
}).start();
}
...
}
this class should be implemented in onDraw() method.
the second is that use the tpk of ArcGIS.
I think it would be better much.

Login function with ASync networking request?

I'm using 'Retrofit' for making asynchronous network requests, how might i right a function for handling logins? For instance i've currently attempted:
public UserAuthResponse Login(String username, String password) {
try {
Callback<UserAuthResponse> getAuthCallback = new Callback<UserAuthResponse>() {
#Override
public void failure(RetrofitError arg0) {
if (arg0 != null) {
if (arg0.getMessage() != null
&& arg0.getMessage().length() > 0) {
Log.e("KFF-Retrofit", arg0.getMessage());
}
}
}
#Override
public void success(UserAuthResponse listItem,
retrofit.client.Response arg1) {
Log.e("dg", listItem.getUser().getFirstname());
}
};
service.authUser(username, MD5(password), getAuthCallback);
return response;
} catch (RetrofitError e) {
e.printStackTrace();
return null;
}
}
But this is flawed: there is no way of returning the 'UserAuthResponse' from the function? How can i pass back the result?
It seems like i need a synchronous call to the web service but then i'm hit with a 'NetworkOnMainThreadException'
What is the best practice for things like this? Sorry about the poor explanation, struggling to form the right words.
Well the things is that when you're using the Callback as your means of getting the results from Retrofit you automatically giving away the possibility of having the response returned inline. There's a few ways this can be solved. I suppose it's up to you to choose which one fits best with your design.
You could decide to not use the Callback approach and use the inline result from Retrofit but then you'd need to handle the scheduling yourself otherwise you'll hit the Exception of NetworkOnMainThreadException like you mentioned.
You could also pass in a listener to your login method. This listener could then be called by the result Callback. This could be useful if you're trying to hide Retrofit behind some sort of service layer and expose a simple login interface.
interface OnLoginListener {
onLoginSuccessful(UserAuthResponse response);
onLoginFailed(Throwable t);
}
public void Login(String username, String password, final OnLoginListener listener) {
Callback<UserAuthResponse> getAuthCallback = new Callback<UserAuthResponse>() {
#Override
public void failure(RetrofitError e) {
// You can handle Retrofit exception or simply pass them down to the listener as is
listener.onLoginFailed(e);
}
#Override
public void success(UserAuthResponse listItem,
retrofit.client.Response arg1) {
// handle successful case here and pass down the data to the listener
listener.onLoginSucessful(listItem);
}
};
service.authUser(username, MD5(password), getAuthCallback);
}
use this line i Manifest
<uses-permission android:name="android.permission.INTERNET"/>
or use this before network operation (not suggestible)
if (Build.VERSION.SDK_INT>= 10) {
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
}

Categories

Resources