今回は経路計算のコールバックについてです。
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) {
//経路計算が終わる毎に呼ばれる。
Log.d("skRouteListener" ,"onRouteCalculationCompleted:" + skRouteInfo.toString());
int routeID = skRouteInfo.getRouteID();
Log.d("skRouteListener" ,"ルートID:" + routeID);
int distance = skRouteInfo.getDistance();
Log.d("skRouteListener" ,"距離(m):" + distance);
int estimatedTime = skRouteInfo.getEstimatedTime();
Log.d("skRouteListener" ,"時間(秒):" + estimatedTime);
int estimatedTimeWithoutTraffic = skRouteInfo.getEstimatedTimeWithoutTraffic();
Log.d("skRouteListener" ,"交通状況を考慮しない時間(秒):" + estimatedTimeWithoutTraffic);
int mode = skRouteInfo.getMode();
Log.d("skRouteListener" ,"ルート検索モード:" + mode);
String[] routeSummaryArray = skRouteInfo.getRouteSummary();
for (String routeSummary : routeSummaryArray) {
Log.d("skRouteListener" ,"ルートサマリー:" + routeSummary);
}
ArrayList<SKViaPointInfo> viaPointsInfoArrayList = skRouteInfo.getViaPointsInfo();
Log.d("skRouteListener" ,"中継点情報");
for (SKViaPointInfo skViaPointInfo : viaPointsInfoArrayList) {
Log.d("skRouteListener" ,"ID:" + skViaPointInfo.getUniqueId());
Log.d("skRouteListener" ,"中継点から目的地への距離(m):" + skViaPointInfo.getDistanceToDestination());
Log.d("skRouteListener" ,"中継点から目的地への時間(秒):" + skViaPointInfo.getEstimatedTimeOfArrival());
Log.d("skRouteListener" ,"近接道路上の緯度経度?:" + skViaPointInfo.getMatchedPosition());
Log.d("skRouteListener" ,"緯度経度:" + skViaPointInfo.getPosition().toString());
}
}
@Override
public void onRouteCalculationFailed(SKRoutingErrorCode skRoutingErrorCode) {
//経路計算が終わったときに呼ばれる。
//SKRoutingErrorCode.APP_OFFLINE → オフラインモードの時にオンライン検索をした。
//SKRoutingErrorCode.INTERNAL_ERROR → 内部エラー
//SKRoutingErrorCode.INVALID_DESTINATION → 目的地が不正
//SKRoutingErrorCode.INVALID_START → 開始位置が不正
//SKRoutingErrorCode.INTERNET_IS_TURNED_OFF → インターネットがオフ
//SKRoutingErrorCode.INVALID_VIA_POINT → 不正な中継点
//SKRoutingErrorCode.NO_RESULTS_FOUND → 経路が見つからないかった
//SKRoutingErrorCode.ROUTE_CANNOT_BE_CALCULATED → 経路が計算できなかった
//SKRoutingErrorCode.SAME_START_AND_DESTINATION → 開始位置と終了位置が同じ
Log.d("skRouteListener" , "onRouteCalculationFailed:" + skRoutingErrorCode.toString());
}
@Override
public void onAllRoutesCompleted() {
//すべての経路計算が終わったときに呼ばれる。
Log.d("skRouteListener" , "onAllRoutesCompleted");
}
@Override
public void onServerLikeRouteCalculationCompleted(SKRouteJsonAnswer skRouteJsonAnswer) {
//???
Log.d("skRouteListener" ,"onServerLikeRouteCalculationCompleted:" + skRouteJsonAnswer.getJsonAnswer());
}
@Override
public void onOnlineRouteComputationHanging(int i) {
//???
Log.d("skRouteListener" , "onOnlineRouteComputationHanging:" + 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();
}
}