diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ece8636..040e3b2 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -52,6 +52,10 @@ android:name=".CustomLayerActivity" android:configChanges="orientation|screenSize" android:theme="@android:style/Theme.NoTitleBar" /> + diff --git a/src/main/java/com/yandex/mapkitdemo/MasstransitRoutingActivity.java b/src/main/java/com/yandex/mapkitdemo/MasstransitRoutingActivity.java new file mode 100644 index 0000000..61b4972 --- /dev/null +++ b/src/main/java/com/yandex/mapkitdemo/MasstransitRoutingActivity.java @@ -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(), new ArrayList(), + // 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 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 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 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; + } +} diff --git a/src/main/res/layout/masstransit_routing.xml b/src/main/res/layout/masstransit_routing.xml new file mode 100644 index 0000000..a9d8141 --- /dev/null +++ b/src/main/res/layout/masstransit_routing.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 714248d..3a51373 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -8,6 +8,7 @@ Suggest Panorama Custom Layer + Masstransit routing Not found Network error diff --git a/src/main/res/xml/main.xml b/src/main/res/xml/main.xml index bcfe77b..8d1b140 100644 --- a/src/main/res/xml/main.xml +++ b/src/main/res/xml/main.xml @@ -66,5 +66,14 @@ android:targetPackage="@string/package_name" android:targetClass="com.yandex.mapkitdemo.CustomLayerActivity" /> + + + +