編輯:關於Android編程
上一篇-Android地圖應用新視界--mapbox的應用開發之初始集成篇-中介紹了全球應用的多平台地圖框架mapbox在Android端的集成步驟,
以及Android的地圖應用新視界--mapbox的應用開發之簡單功能提取篇,如果要了解建議先看前兩篇哦
此篇將延續上篇內容,主要提取常用功能封裝工具類,可以直接當工具類使用
直接上干貨
如下:
public class MapBoxUtils {
private MapboxMap mapboxMap;
private Context context;
//調用mapboxAPI的token令牌
public static String MAPBOX_ACCESS_TOKEN = "pk.eyJ1IjoiamFja3l6IiwiYSI6ImNpb2w3OTlmdDAwNzd1Z20weG42MjF5dmMifQ.775C4o6elT5la-uuMjJe4w";
public MapBoxUtils() {
}
public MapBoxUtils(MapboxMap mapboxMap, Context context) {
this.mapboxMap = mapboxMap;
this.context = context;
mapboxMap.setAccessToken(MAPBOX_ACCESS_TOKEN);
}
/**
* 在指定位置繪制指定圖片
*
* @param latitude
* @param longitude
* @param drawableId 指定id的圖片
*/
public void DrawMarker(final double latitude, final double longitude, int drawableId) {
// Create an Icon object for the marker to use
IconFactory iconFactory = IconFactory.getInstance(context);
Drawable iconDrawable = ContextCompat.getDrawable(context, drawableId);
Icon icon = iconFactory.fromDrawable(iconDrawable);
// Add the custom icon marker to the map
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.icon(icon)).getPosition();
}
/**
* 繪制簡單的默認標記
*
* @param latitude
* @param longitude
* @param title 標題
* @param sinippet 說明
*/
public void DrawSimpleMarker(final double latitude, final double longitude, String title, String sinippet) {
// Add the custom icon marker to the map
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(title)
.snippet(sinippet));
}
/**
* 動畫跳轉到指定位置--指定方式
*
* @param latitude
* @param longitude
* @param zoom 指定變焦
* @param bearing 指定相對原始旋轉角度
* @param tilt 指定視角傾斜度
*/
public void jumpToLocation(final double latitude, final double longitude, final double zoom, final double bearing, final double tilt) {
//設置目標點---點擊回到當前位置
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)) // Sets the center of the map to the specified location
.zoom(zoom)
.bearing(bearing)
.tilt(tilt)
.build();
//set the user's viewpoint as specified in the cameraPosition object
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 3000);
}
/**
* 動畫跳轉到指定位置---默認方式
*/
public void jumpToLocationDefault(final double latitude, final double longitude) {
//設置目標點---點擊回到當前位置
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)) // Sets the center of the map to the specified location
.zoom(11)
.bearing(0)
.tilt(0)
.build();
//set the user's viewpoint as specified in the cameraPosition object
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 3000);
}
/**
* 獲取token令牌
*
* @param context
* @return
*/
public static String getMapboxAccessToken(@NonNull Context context) {
try {
// Read out AndroidManifest
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = packageManager
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
String token = appInfo.metaData.getString(MapboxConstants.KEY_META_DATA_MANIFEST);
if (token == null || token.isEmpty()) {
throw new IllegalArgumentException();
}
return token;
} catch (Exception e) {
// Use fallback on string resource, used for development
int tokenResId = context.getResources()
.getIdentifier("mapbox_access_token", "string", context.getPackageName());
return tokenResId != 0 ? context.getString(tokenResId) : null;
}
}
/**
* 此方法待驗證
*
* 給定坐標點繪制出參考路線 * * @param point */ public void drawMyRoute(LatLng point) { //刪除所有之前的標記 mapboxMap.removeAnnotations(); // Set the origin waypoint to the devices location設置初始位置 Waypoint origin = new Waypoint(mapboxMap.getMyLocation().getLongitude(), mapboxMap.getMyLocation().getLatitude()); // 設置目的地路徑--點擊的位置點 Waypoint destination = new Waypoint(point.getLongitude(), point.getLatitude()); // Add marker to the destination waypoint mapboxMap.addMarker(new MarkerOptions() .position(new LatLng(point)) .title("目的地") .snippet("My destination")); // Get route from API getRoute(origin, destination); } private DirectionsRoute currentRoute = null; private void getRoute(Waypoint origin, Waypoint destination) { MapboxDirections directions = new MapboxDirections.Builder() .setAccessToken(MAPBOX_ACCESS_TOKEN) .setOrigin(origin) .setDestination(destination) .setProfile(DirectionsCriteria.PROFILE_WALKING) .build(); directions.enqueue(new Callback
BLE藍牙在Android開發中的應用
最近一段時間在寫支持BLE藍牙的Android應用。是時候總結一下了。1、什麼是BLE。(總得先知道BLE是什麼吧~~~)Bluetooth Low Energy(低功耗
Android適配安卓6.0藍牙通訊實現過程
事先說明:安卓藍牙需要定位權限申請,在安卓6.0需要用戶手動確認權限後才能使用,各位可以自行查詢資料實現,如果嫌麻煩,可以用第三方Bmob集成好的工具類進行實現,詳細可以
android TCP 和 UDP總結
之前寫過一些關於TCP和UDP數據傳輸的代碼,比如使用TCP傳輸音視頻數據包,P2P打洞中使用UDP等。寫好之後就直接丟下了,沒有總結下都。最近准備找工作,再拿來溫習下。
Android中Handler的使用
在Android開發中,我們經常會遇到這樣一種情況:在UI界面上進行某項操作後要執行一段很耗時的代碼,比如我們在界面上點擊了一個”下載“按鈕,那麼