目標Achievements
這個Class是我們App的導入點,我稱這種類別為Main類別,那我們在這個類別中我們就需要去做整個程式大方向的規劃.
程式碼Code
package lien.ching.maptracker;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.MapPosition;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.android.util.AndroidUtil;
import org.mapsforge.map.android.view.MapView;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.datastore.MultiMapDataStore;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;
import java.io.File;
import lien.ching.maptracker.api.EnvCheck;
import lien.ching.maptracker.overlay.NowLocationLayout;
/**
* Created by lienching on 11/27/15.
*/
public class MainActivity extends Activity {
private MapView mapView;
private MapDataStore taiwanMap,worldMap;
private MultiMapDataStore multiMapDataStore;
private TileCache tileCache;
private TileRendererLayer tileRendererLayer;
private EnvCheck envCheck;
private NowLocationLayout nowLocationLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidGraphicFactory.createInstance(this.getApplication()); //Initiation Mapsforge Library
mapView = new MapView(this);
mapView.setClickable(true);
mapView.getMapScaleBar().setVisible(true);
mapView.setBuiltInZoomControls(true);
mapView.getMapZoomControls().setZoomLevelMin((byte) 5);
mapView.getMapZoomControls().setZoomLevelMax((byte) 20);
mapView.getModel().mapViewPosition.setMapPosition(new MapPosition(new LatLong(23.6, 121), (byte) 7));
//Disable hardware acceleration at mapview, for more detail about this line of code please check (https://goo.gl/v5weg9)
mapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
setContentView(mapView);
nowLocationLayout = new NowLocationLayout(this,mapView.getModel().mapViewPosition,mapView);
envCheck = new EnvCheck(mapView,nowLocationLayout,this.getApplicationContext(),this);
//To keep screen on(http://developer.android.com/training/scheduling/wakelock.html)
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
protected void onStart() {
super.onStart();
envCheck.CheckAndDownload("world", "world-lowres-0-7.map"); //Check if the map source exist
envCheck.CheckAndDownload("asia", "taiwan.map");
mapView.getLayerManager().getLayers().add(nowLocationLayout);//Add Location Layer
nowLocationLayout.startTrack();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
this.mapView.destroyAll();
}
}
說明Explanation
第41行:這一行因為Mapsforge所用的androidsvg必須要做一些初始化,所以我們在第一行就將他呼叫避免後續問題
第50行:用來關閉硬體加速( For Detail
) 第55行:因為這個範例是要用來紀錄使用者的移動軌跡,所以需要使用這行(For Detail)
第63~64行:檢查檔案是否存在或有沒有新版本,(詳細運作請參考EnvCheck)
第65~66行:因為前兩行需要花時間但也必須要讓使用者知道程式有在運作,所以先讓定位開始運作