今回は経路表示を行います。
プログラムの内容は下記のとおりです。
- 東京駅を経路の開始地点にする
- 3箇所を中継点に設定する
- タップしたところを終了地点にする
public class MainActivity extends AppCompatActivity {
private SKMapSurfaceView skMapSurfaceView;
private SKMapViewHolder mapHolder;
String mapResourcesDirPath;
private SKCoordinate startSkCoordinate = new SKCoordinate(139.766084, 35.681382);
SKRouteListener skRouteListener = new SKRouteListener() {
@Override
public void onRouteCalculationCompleted(SKRouteInfo skRouteInfo) {
}
@Override
public void onRouteCalculationFailed(SKRoutingErrorCode skRoutingErrorCode) {
}
@Override
public void onAllRoutesCompleted() {
}
@Override
public void onServerLikeRouteCalculationCompleted(SKRouteJsonAnswer skRouteJsonAnswer) {
}
@Override
public void onOnlineRouteComputationHanging(int i) {
}
};
SKMapSurfaceListener skMapSurfaceListener = new SKMapSurfaceListener() {
@Override
public void onActionPan() {
}
@Override
public void onActionZoom() {
}
@Override
public void onSurfaceCreated(SKMapViewHolder skMapViewHolder) {
mapHolder = skMapViewHolder;
skMapSurfaceView = skMapViewHolder.getMapSurfaceView();
skMapSurfaceView.centerMapOnPosition(startSkCoordinate);
}
@Override
public void onMapRegionChanged(SKCoordinateRegion skCoordinateRegion) {
}
@Override
public void onMapRegionChangeStarted(SKCoordinateRegion skCoordinateRegion) {
}
@Override
public void onMapRegionChangeEnded(SKCoordinateRegion skCoordinateRegion) {
}
@Override
public void onDoubleTap(SKScreenPoint skScreenPoint) {
}
@Override
public void onSingleTap(SKScreenPoint skScreenPoint) {
SKCoordinate endSKSkCoordinate = skMapSurfaceView.pointToCoordinate(skScreenPoint);
launchRouteCalculation(startSkCoordinate, endSKSkCoordinate);
}
@Override
public void onRotateMap() {
}
@Override
public void onLongPress(SKScreenPoint skScreenPoint) {
}
@Override
public void onInternetConnectionNeeded() {
}
@Override
public void onMapActionDown(SKScreenPoint skScreenPoint) {
}
@Override
public void onMapActionUp(SKScreenPoint skScreenPoint) {
}
@Override
public void onPOIClusterSelected(SKPOICluster skpoiCluster) {
}
@Override
public void onMapPOISelected(SKMapPOI skMapPOI) {
}
@Override
public void onAnnotationSelected(SKAnnotation skAnnotation) {
}
@Override
public void onCustomPOISelected(SKMapCustomPOI skMapCustomPOI) {
}
@Override
public void onCompassSelected() {
}
@Override
public void onCurrentPositionSelected() {
}
@Override
public void onObjectSelected(int i) {
}
@Override
public void onInternationalisationCalled(int i) {
}
@Override
public void onBoundingBoxImageRendered(int i) {
}
@Override
public void onGLInitializationError(String s) {
}
@Override
public void onScreenshotReady(Bitmap bitmap) {
}
};
private void launchRouteCalculation(SKCoordinate startPoint, SKCoordinate destinationPoint) {
//経路情報のキャッシュをクリアする
SKRouteManager.getInstance().clearAllRoutesFromCache();
//経路計算のリスナーを追加する。
SKRouteManager.getInstance().setRouteListener(skRouteListener);
//経路計算の設定する。
SKRouteSettings skRouteSettings = new SKRouteSettings();
//開始位置を設定する
skRouteSettings.setStartCoordinate(startPoint);
//終了位置を設定する
skRouteSettings.setDestinationCoordinate(destinationPoint);
//最大計算経路数を設定する
skRouteSettings.setNoOfRoutes(5);
//移動手段の設定
//SKRouteSettings.SKRouteMode.BICYCLE_FASTEST→自転車最短時間
//SKRouteSettings.SKRouteMode.BICYCLE_QUIETEST→自転車ゆっくり?
//SKRouteSettings.SKRouteMode.BICYCLE_SHORTEST→自転車最短距離
//SKRouteSettings.SKRouteMode.BUS_FASTEST→バス最短時間
//SKRouteSettings.SKRouteMode.CAR_FASTEST→車最短時間
//SKRouteSettings.SKRouteMode.CAR_SHORTEST→車最短時間
//SKRouteSettings.SKRouteMode.EFFICIENT→???
//SKRouteSettings.SKRouteMode.PEDESTRIAN→歩き
skRouteSettings.setRouteMode(SKRouteSettings.SKRouteMode.PEDESTRIAN);
//接続モードを設定する
//SKRouteSettings.SKRouteConnectionMode.HYBRID→オフライン・オンラインの両方
//SKRouteSettings.SKRouteConnectionMode.OFFLINE→オフライン
//SKRouteSettings.SKRouteConnectionMode.ONLINE→オンライン
skRouteSettings.setRouteConnectionMode(SKRouteSettings.SKRouteConnectionMode.HYBRID);
//有料道路を避けるかどうか。避ける→true/避けない→false
skRouteSettings.setTollRoadsAvoided(true);
//高速道路を避けるかどうか。避ける→true/避けない→false
skRouteSettings.setHighWaysAvoided(true);
//フェリーを避けるかどうか。避ける→true/避けない→false
skRouteSettings.setAvoidFerries(true);
//経路情報だけを取得する。true:情報だけを取得し、地図上にルートを表示しない
skRouteSettings.setRouteExposed(true);
//自転車を運ぶのを避けるかどうか。
skRouteSettings.setBicycleCarryAvoided(false);
//自転車を降りて歩くのを避けるかどうか。
skRouteSettings.setBicycleWalkAvoided(false);
//経路上に中継点を複数設置する。
SKViaPoint skViaPoint1 = new SKViaPoint(0, new SKCoordinate(startPoint.getLongitude(), startPoint.getLatitude() + 0.1f));
SKViaPoint skViaPoint2 = new SKViaPoint(1, new SKCoordinate(startPoint.getLongitude() + 0.1, startPoint.getLatitude() + 0.1f));
SKViaPoint skViaPoint3 = new SKViaPoint(2, new SKCoordinate(destinationPoint.getLongitude(), destinationPoint.getLatitude()));
List<SKViaPoint> skViaPointList = new ArrayList<>();
skViaPointList.add(skViaPoint1);
skViaPointList.add(skViaPoint2);
skViaPointList.add(skViaPoint3);
skRouteSettings.setViaPoints(skViaPointList);
//経路計算を開始する。結果は成功失敗は各コールバックメソッドで返却する
SKRouteManager.getInstance().calculateRoute(skRouteSettings);
//わからない
//skRouteSettings.setUseRoadSlopes();
//skRouteSettings.setFilterAlternatives();
//skRouteSettings.setRequestAdvices();
//skRouteSettings.setExtendedPointsReturned();
//skRouteSettings.setCountryCodesReturned();
//skRouteSettings.setUseRoadSlopes();
//skRouteSettings.setAlternativeRouteModes();
//skRouteSettings.setDownloadRouteCorridor(true);
//skRouteSettings.setRouteCorridorWidthInMeters(100);
//skRouteSettings.setWaitForCorridorDownload(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapResourcesDirPath = getFilesDir().getPath() + "/" + "SKMaps/";
if (!new File(mapResourcesDirPath).exists()) {
new SKPrepareMapTextureThread(this, mapResourcesDirPath, "SKMaps.zip", new SKPrepareMapTextureListener() {
@Override
public void onMapTexturesPrepared(boolean b) {
if (b) {
initMapSetting();
}
}
}).start();
} else {
initMapSetting();
}
}
@Override
protected void onPause() {
super.onPause();
if (mapHolder != null) {
mapHolder.onPause();
}
}
@Override
protected void onResume() {
super.onResume();
if (mapHolder != null) {
mapHolder.onResume();
}
}
private void initMapSetting() {
SKMapsInitSettings initMapSettings = new SKMapsInitSettings();
initMapSettings.setMapResourcesPaths(mapResourcesDirPath,
new SKMapViewStyle(mapResourcesDirPath + "daystyle/", "daystyle.json"));
final SKAdvisorSettings advisorSettings = initMapSettings.getAdvisorSettings();
advisorSettings.setAdvisorConfigPath(mapResourcesDirPath + "Advisor");
advisorSettings.setResourcePath(mapResourcesDirPath + "Advisor/Languages");
advisorSettings.setLanguage(SKAdvisorSettings.SKAdvisorLanguage.LANGUAGE_EN);
advisorSettings.setAdvisorVoice("en");
initMapSettings.setAdvisorSettings(advisorSettings);
SKMaps.getInstance().initializeSKMaps(this, initMapSettings);
SKMapFragment mapFragment = new SKMapFragment();
mapFragment.setMapSurfaceListener(skMapSurfaceListener);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, mapFragment).commit();
}
}