【AndroidでSkobblerを使う】SKMapViewHolderとSKMapSurfaceView
Skobblerで地図を表示するにはクラス:SKMapFragmentを使うと簡単に表示ができます。
このSKMapFragmentは地図にアクセスするクラス:SKMapViewHolderと地図を表示するクラス:SKMapSurfaceViewを持っています。
しかし、SKMapViewHolderとSKMapSurfaceViewは初期化に少し時間が必要になります、そのため初期化が完了したことを検知する必要があります。
そこでSKMapFragment#setMapSurfaceListenerを使うことで地図に関連するイベントを検知できるようになります。
上記のメソッドはSKMapSurfaceListenerを引数に取ります。
SKMapSurfaceListenerはたくさんのイベントを検知することができます、少しずつ説明しています。
SKMapSurfaceListener#onSurfaceCreatedはSKMapViewHolderの初期化が完成すると呼び出されます。
引数のSKMapViewHolderからSKMapSurfaceViewを取得することができます。
また、いくつか重要なことを列挙します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | private SKMapSurfaceView mapView; private SKMapViewHolder mapHolder; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); SKMapFragment mapFragment = new SKMapFragment(); mapFragment.setMapSurfaceListener( this ); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.container, mapFragment).commit(); } @Override public void onSurfaceCreated(SKMapViewHolder skMapViewHolder) { mapHolder = skMapViewHolder; mapView = skMapViewHolder.getMapSurfaceView(); } @Override protected void onPause() { super .onPause(); if (mapHolder != null ) { mapHolder.onPause(); } } @Override protected void onResume() { super .onResume(); if (mapHolder != null ) { mapHolder.onResume(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | private void settingScaleView() { //縮尺の表示を有効にする。 mapHolder.setScaleViewEnabled( true ); //縮尺を右下に配置する。 mapHolder.setScaleViewPosition( 0 , 80 , RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.ALIGN_PARENT_BOTTOM); //SKMapScaleViewのインスタンスを取得する。 SKMapScaleView scaleView = mapHolder.getScaleView(); //枠線の色を指定する。 scaleView.setBorderColor(Color.RED); //SKMapScaleViewのバーは明るい色と黒い色の二色で塗り分けられます。 //暗い色を指定します。 scaleView.setDarkerColor(Color.BLUE); //明るい色を指定します。 scaleView.setLighterColor(Color.GREEN); //テキストの色を指定します。 scaleView.setTextColor(Color.CYAN); //縮尺の単位を指定します。キロメーター、マイルフィート、マイルヤードから選択できます。 scaleView.setDistanceUnit(SKMaps.SKDistanceUnitType.DISTANCE_UNIT_KILOMETER_METERS); //各単位のラベルを指定する。 scaleView.setDistanceUnitLabels( "kmのラベル" , "mのラベル" , "miのラベル" , "ftのラベル" , "ydのラベル" ); //SKMapScaleViewの自動フェイドアウトアニメーションの有効無効を設定する。true:フェイドアウトする/false:フェイドアウトしない scaleView.setFadeOutEnabled( false ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | public class MainActivity extends AppCompatActivity implements SKPrepareMapTextureListener, SKMapSurfaceListener { private SKCoordinate tokyo = new SKCoordinate( 139.766084 , 35.681382 ); private SKMapSurfaceView mapView; private SKMapViewHolder mapHolder; String mapResourcesDirPath; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); SKMapSurfaceView.preserveGLContext = false ; setContentView(R.layout.activity_main); mapResourcesDirPath = getFilesDir().getPath() + "/" + "SKMaps/" ; if (! new File(mapResourcesDirPath).exists()) { new SKPrepareMapTextureThread( this , mapResourcesDirPath, "SKMaps.zip" , this ).start(); } else { initMapSetting(); } } @Override public void onMapTexturesPrepared( boolean b) { if (b) { initMapSetting(); } } 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( this ); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.container, mapFragment).commit(); } @Override protected void onPause() { super .onPause(); if (mapHolder != null ) { mapHolder.onPause(); } } @Override protected void onResume() { super .onResume(); if (mapHolder != null ) { mapHolder.onResume(); } } @Override public void onSurfaceCreated(SKMapViewHolder skMapViewHolder) { mapHolder = skMapViewHolder; settingScaleView(); mapView = skMapViewHolder.getMapSurfaceView(); mapView.centerMapOnPosition( new SKCoordinate( 139.766084 , 35.681382 )); //initMapSettings(); } private void settingScaleView() { //縮尺の表示を有効にする。 mapHolder.setScaleViewEnabled( true ); //縮尺を右下に配置する。 mapHolder.setScaleViewPosition( 0 , 80 , RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.ALIGN_PARENT_BOTTOM); //SKMapScaleViewのインスタンスを取得する。 SKMapScaleView scaleView = mapHolder.getScaleView(); //枠線の色を指定する。 scaleView.setBorderColor(Color.RED); //SKMapScaleViewのバーは明るい色と黒い色の二色で塗り分けられます。 //暗い色を指定します。 scaleView.setDarkerColor(Color.BLUE); //明るい色を指定します。 scaleView.setLighterColor(Color.GREEN); //テキストの色を指定します。 scaleView.setTextColor(Color.CYAN); //縮尺の単位を指定します。キロメーター、マイルフィート、マイルヤードから選択できます。 scaleView.setDistanceUnit(SKMaps.SKDistanceUnitType.DISTANCE_UNIT_KILOMETER_METERS); //各単位のラベルを指定する。 scaleView.setDistanceUnitLabels( "kmのラベル" , "mのラベル" , "miのラベル" , "ftのラベル" , "ydのラベル" ); //SKMapScaleViewの自動フェイドアウトアニメーションの有効無効を設定する。true:フェイドアウトする/false:フェイドアウトしない scaleView.setFadeOutEnabled( false ); } @Override public void onActionPan() {} @Override public void onActionZoom() {} @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) {} @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 initMapSettings() { //地図描画フレームレートの最大値を設定する。ただし、ユーザー操作は含まれない。 mapView.getMapSettings().setFrameRate( 60 ); //地図の表示領域が何を追尾モードを指定する。 //例えば下記のようにするとユーザーの位置が地図の中心に来るように追尾します。 mapView.getMapSettings().setFollowerMode(SKMapSettings.SKMapFollowerMode.NONE); //地図の言語設定に使用する。 SKMapInternationalizationSettings skMapInternationalizationSettings = new SKMapInternationalizationSettings(); skMapInternationalizationSettings.setPrimaryLanguage(SKMaps.SKLanguage.LANGUAGE_RU); mapView.getMapSettings().setMapInternationalizationSettings(skMapInternationalizationSettings); settingsUserAction(); settingsMapObject(); settingsPoi(); settingsStyle(); settings3D(); //以下は使い方がわからない調査中メソッド //mapView.getMapSettings().setDrawingOrder(); //mapView.getMapSettings().setTerrainEnabled(false); //mapView.getMapSettings().setTerrainDisabledIfNoElevation(false); //mapView.getMapSettings().setOrientationIndicatorType(); //mapView.getMapSettings().setGeneratedPoisShown(false); //mapView.getMapSettings().setImportantPoisShown(true); } private void settings3D() { //地図を3D表示したときのカメラを設定する。 SK3DCameraSettings sk3DCameraSettings = new SK3DCameraSettings(); sk3DCameraSettings.setTilt( 60 ); sk3DCameraSettings.setCenter( 0 .9f); sk3DCameraSettings.setDistance( 10 ); mapView.getMapSettings().setCameraSettings(sk3DCameraSettings); //地図を拡大した表示を2Dと3Dのどちらかを指定することができます。 mapView.getMapSettings().setMapDisplayMode(SKMapSettings.SKMapDisplayMode.MODE_3D); //通りの名前をポップアップ風に表示する。true:ポップアップ表示/false:地図上に書き込み mapView.getMapSettings().setStreetNamePopupsShown( false ); } private void settingsStyle() { // 地図を描画する時に使うスタイルファイルを設定する。 //SKMaps.zip内にはデフォルトで下記の4styleが存在します。 String daystyle = "daystyle" ; String grayscalestyle = "grayscalestyle" ; String nightstyle = "nightstyle" ; String outdoorstyle = "outdoorstyle" ; String style = nightstyle; //コンストラクタの引数はstyleファイルが存在するフォルダとstyleファイルの名前です。 SKMapViewStyle skMapViewStyle = new SKMapViewStyle(mapResourcesDirPath + style + "/" , style + ".json" ); mapView.getMapSettings().setMapStyle(skMapViewStyle); } private void settingsPoi() { //POIはランドマークのことです。 //POIの表示非表示を設定する true:表示/false:非表示 mapView.getMapSettings().setMapPoiIconsShown( true ); //地図上の都市情報の表示非表示を設定する true:表示/false:非表示 //country:国名/state:州名/city:都市名/town:街名/village:村名などなど mapView.getMapSettings().setCityPoisShown( true ); } private void settingsMapObject() { //自転車専用レーンの表示非表示を設定する true:表示/false:非表示 mapView.getMapSettings().setShowBicycleLanes( true ); //一方通行の表示非表示を設定する true:表示/false:非表示 mapView.getMapSettings().setOneWayArrows( true ); //同一建物内の店舗数の表示非表示を設定する true:表示/false:非表示 mapView.getMapSettings().setHouseNumbersShown( true ); //自分の位置の表示非表示を設定する。true:表示/false:非表示 //初期表示位置はなぜか北極点 mapView.getMapSettings().setCurrentPositionShown( false ); //コンパスの表示-true:表示/false:非表示 mapView.getMapSettings().setCompassShown( true ); //コンパスの位置を指定する。右上を原点とする。 mapView.getMapSettings().setCompassPosition( new SKScreenPoint( 10 , 10 )); } private void settingsUserAction() { //地図の移動の有効無効を設定する。true:有効/false:無効 mapView.getMapSettings().setMapPanningEnabled( false ); //地図の回転の有効無効を設定する true:有効/false:無効 mapView.getMapSettings().setMapRotationEnabled( false ); //Zoomの有効無効を設定する。true:有効/false:無効 mapView.getMapSettings().setMapZoomingEnabled( true ); //地図の移動に慣性力を付加する。 true:有効/false:無効 mapView.getMapSettings().setInertiaPanningEnabled( false ); //地図の回転に慣性力を付加する。 true:有効/false:無効 mapView.getMapSettings().setInertiaRotatingEnabled( false ); //地図のズームに慣性力を付加する。 true:有効/false:無効 mapView.getMapSettings().setInertiaZoomingEnabled( false ); //アンカーポイントを中心にZoomをするかを設定する。 // true:アンカー(地図中心)にZoomする。/false:ピンチしている指の間を中心にZoomする。 mapView.getMapSettings().setZoomWithAnchorEnabled( false ); //Zoomの上下限を設定するメソッド //しかし、どんな値を設定しても特に変化がない。 mapView.getMapSettings().setZoomLimits(5f, 19f); //リファレンスに説明が無く、値を変更しても反応しない。 //動作しないメソッドなのか? mapView.getMapSettings().setMinimumZoomForTapping( 0 ); } } |
![]() |
![]() |
![]() |
![]() |
![]() |
||||||||||
![]() | ||||||||||||||