Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add masstransit routing activity #3

Merged
merged 3 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
android:name=".CustomLayerActivity"
android:configChanges="orientation|screenSize"
android:theme="@android:style/Theme.NoTitleBar" />
<activity
android:name=".MasstransitRoutingActivity"
android:configChanges="orientation|screenSize"
android:theme="@android:style/Theme.NoTitleBar" />
</application>

</manifest>
187 changes: 187 additions & 0 deletions src/main/java/com/yandex/mapkitdemo/MasstransitRoutingActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package com.yandex.mapkitdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.yandex.mapkit.Animation;
import com.yandex.mapkit.MapKitFactory;
import com.yandex.mapkit.geometry.Point;
import com.yandex.mapkit.geometry.Polyline;
import com.yandex.mapkit.geometry.SubpolylineHelper;
import com.yandex.mapkit.map.CameraPosition;
import com.yandex.mapkit.map.MapObjectCollection;
import com.yandex.mapkit.map.PolylineMapObject;
import com.yandex.mapkit.mapview.MapView;
import com.yandex.mapkit.masstransit.MasstransitOptions;
import com.yandex.mapkit.masstransit.MasstransitRouter;
import com.yandex.mapkit.masstransit.Route;
import com.yandex.mapkit.masstransit.Section;
import com.yandex.mapkit.masstransit.SectionMetadata;
import com.yandex.mapkit.masstransit.Session;
import com.yandex.mapkit.masstransit.TimeOptions;
import com.yandex.mapkit.masstransit.Transport;
import com.yandex.runtime.Error;
import com.yandex.runtime.network.NetworkError;
import com.yandex.runtime.network.RemoteError;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
* This example shows how to build public transport routes between two points,
* and how to handle route sections and vehicle types lists
* Note: Masstransit routing API calls count towards MapKit daily usage limits. Learn more at
* https://tech.yandex.ru/mapkit/doc/3.x/concepts/conditions-docpage/#conditions__limits
*/
public class MasstransitRoutingActivity extends Activity
implements Session.RouteListener {
/**
* Replace "your_api_key" with a valid developer key.
* You can get it at the https://developer.tech.yandex.ru/ website.
*/
private final String MAPKIT_API_KEY = "your_api_key";
private final Point TARGET_LOCATION = new Point(55.752078, 37.592664);

private final Point ROUTE_START_LOCATION = new Point(55.699671, 37.567286);
private final Point ROUTE_END_LOCATION = new Point(55.790621, 37.558571);

private MapView mapView;
private MapObjectCollection mapObjects;
private MasstransitRouter mtRouter;

@Override
protected void onCreate(Bundle savedInstanceState) {
MapKitFactory.setApiKey(MAPKIT_API_KEY);
MapKitFactory.initialize(this);
setContentView(R.layout.map);
super.onCreate(savedInstanceState);
mapView = (MapView)findViewById(R.id.mapview);

// And to show what can be done with it, we move the camera to the center of Saint Petersburg.
mapView.getMap().move(
new CameraPosition(TARGET_LOCATION, 12.0f, 0.0f, 0.0f),
new Animation(Animation.Type.SMOOTH, 5),
null);

mapObjects = mapView.getMap().getMapObjects().addCollection();

mtRouter = MapKitFactory.getInstance().createMasstransitRouter();
mtRouter.requestRoutes(ROUTE_START_LOCATION, ROUTE_END_LOCATION,
new MasstransitOptions(new ArrayList<String>(), new ArrayList<String>(),
// Specify departure time or arrival time here
new TimeOptions()),
this);
}

@Override
protected void onStop() {
mapView.onStop();
MapKitFactory.getInstance().onStop();
super.onStop();
}

@Override
protected void onStart() {
super.onStart();
MapKitFactory.getInstance().onStart();
mapView.onStart();
}

@Override
public void onMasstransitRoutes(List<Route> routes) {
// In this example we consider first alternative only
if (routes.size() > 0) {
for (Section section : routes.get(0).getSections()) {
drawSection(
section.getMetadata().getData(),
SubpolylineHelper.subpolyline(
routes.get(0).getGeometry(), section.getGeometry()));
}
}
}

@Override
public void onMasstransitRoutesError(Error error) {
String errorMessage = getString(R.string.unknown_error_message);
if (error instanceof RemoteError) {
errorMessage = getString(R.string.remote_error_message);
} else if (error instanceof NetworkError) {
errorMessage = getString(R.string.network_error_message);
}

Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
}

private void drawSection(SectionMetadata.SectionData data,
Polyline geometry) {
// Draw a section polyline on a map
// Set its color depending on the information which the section contains
PolylineMapObject polylineMapObject = mapObjects.addPolyline(geometry);
// Masstransit route section defines exactly one on the following
// 1. Wait until public transport unit arrives
// 2. Walk
// 3. Transfer to a nearby stop (typically transfer to a connected
// underground station)
// 4. Ride on a public transport
// Check the corresponding object for null to get to know which
// kind of section it is
if (data.getTransports() != null) {
// A ride on a public transport section contains information about
// all known public transport lines which can be used to travel from
// the start of the section to the end of the section without transfers
// along a similar geometry
for (Transport transport : data.getTransports()) {
// Some public transport lines may have a color associated with them
// Typically this is the case of underground lines
if (transport.getLine().getStyle() != null) {
polylineMapObject.setStrokeColor(
// The color is in RRGGBB 24-bit format
// Convert it to AARRGGBB 32-bit format, set alpha to 255 (opaque)
transport.getLine().getStyle().getColor() | 0xFF000000
);
return;
}
}
// Let us draw bus lines in green and tramway lines in red
// Draw any other public transport lines in blue
HashSet<String> knownVehicleTypes = new HashSet<>();
knownVehicleTypes.add("bus");
knownVehicleTypes.add("tramway");
for (Transport transport : data.getTransports()) {
String sectionVehicleType = getVehicleType(transport, knownVehicleTypes);
if (sectionVehicleType.equals("bus")) {
polylineMapObject.setStrokeColor(0xFF00FF00); // Green
return;
} else if (sectionVehicleType.equals("tramway")) {
polylineMapObject.setStrokeColor(0xFFFF0000); // Red
return;
}
}
polylineMapObject.setStrokeColor(0xFF0000FF); // Blue
} else {
// This is not a public transport ride section
// In this example let us draw it in black
polylineMapObject.setStrokeColor(0xFF000000); // Black
}
}

private String getVehicleType(Transport transport, HashSet<String> knownVehicleTypes) {
// A public transport line may have a few 'vehicle types' associated with it
// These vehicle types are sorted from more specific (say, 'histroic_tram')
// to more common (say, 'tramway').
// Your application does not know the list of all vehicle types that occur in the data
// (because this list is expanding over time), therefore to get the vehicle type of
// a public line you should iterate from the more specific ones to more common ones
// until you get a vehicle type which you can process
// Some examples of vehicle types:
// "bus", "minibus", "trolleybus", "tramway", "underground", "railway"
for (String type : transport.getLine().getVehicleTypes()) {
if (knownVehicleTypes.contains(type)) {
return type;
}
}
return null;
}
}
10 changes: 10 additions & 0 deletions src/main/res/layout/masstransit_routing.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.yandex.mapkit.mapview.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<string name="suggest">Suggest</string>
<string name="panorama">Panorama</string>
<string name="custom_layer">Custom Layer</string>
<string name="masstransit_routing">Masstransit routing</string>

<string name="not_found_error_message">Not found</string>
<string name="network_error_message">Network error</string>
Expand Down
9 changes: 9 additions & 0 deletions src/main/res/xml/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,14 @@
android:targetPackage="@string/package_name"
android:targetClass="com.yandex.mapkitdemo.CustomLayerActivity" />
</Preference>
<Preference
android:key="masstransit_routing_test"
android:title="@string/masstransit_routing">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="@string/package_name"
android:targetClass="com.yandex.mapkitdemo.MasstransitRoutingActivity" />
</Preference>

</PreferenceCategory>
</PreferenceScreen>