当前位置:   article > 正文

Android地图应用新视界--mapbox的常用功能封装工具类_mapbox-gl-draw在android可以用吗

mapbox-gl-draw在android可以用吗


上一篇- Android地图应用新视界--mapbox的应用开发之初始集成篇-中介绍了全球应用的多平台地图框架mapbox在Android端的集成步骤,

以及Android的地图应用新视界--mapbox的应用开发之简单功能提取篇,如果要了解建议先看前两篇哦

此篇将延续上篇内容,主要提取常用功能封装工具类,可以直接当工具类使用

直接上干货

如下:



  1. public class MapBoxUtils {
  2. private MapboxMap mapboxMap;
  3. private Context context;
  4. //调用mapboxAPI的token令牌
  5. public static String MAPBOX_ACCESS_TOKEN = "pk.eyJ1IjoiamFja3l6IiwiYSI6ImNpb2w3OTlmdDAwNzd1Z20weG42MjF5dmMifQ.775C4o6elT5la-uuMjJe4w";
  6. public MapBoxUtils() {
  7. }
  8. public MapBoxUtils(MapboxMap mapboxMap, Context context) {
  9. this.mapboxMap = mapboxMap;
  10. this.context = context;
  11. mapboxMap.setAccessToken(MAPBOX_ACCESS_TOKEN);
  12. }
  13. /**
  14. * 在指定位置绘制指定图片
  15. *
  16. * @param latitude
  17. * @param longitude
  18. * @param drawableId 指定id的图片
  19. */
  20. public void DrawMarker(final double latitude, final double longitude, int drawableId) {
  21. // Create an Icon object for the marker to use
  22. IconFactory iconFactory = IconFactory.getInstance(context);
  23. Drawable iconDrawable = ContextCompat.getDrawable(context, drawableId);
  24. Icon icon = iconFactory.fromDrawable(iconDrawable);
  25. // Add the custom icon marker to the map
  26. mapboxMap.addMarker(new MarkerOptions()
  27. .position(new LatLng(latitude, longitude))
  28. .icon(icon)).getPosition();
  29. }
  30. /**
  31. * 绘制简单的默认标记
  32. *
  33. * @param latitude
  34. * @param longitude
  35. * @param title 标题
  36. * @param sinippet 说明
  37. */
  38. public void DrawSimpleMarker(final double latitude, final double longitude, String title, String sinippet) {
  39. // Add the custom icon marker to the map
  40. mapboxMap.addMarker(new MarkerOptions()
  41. .position(new LatLng(latitude, longitude))
  42. .title(title)
  43. .snippet(sinippet));
  44. }
  45. /**
  46. * 动画跳转到指定位置--指定方式
  47. *
  48. * @param latitude
  49. * @param longitude
  50. * @param zoom 指定变焦
  51. * @param bearing 指定相对原始旋转角度
  52. * @param tilt 指定视角倾斜度
  53. */
  54. public void jumpToLocation(final double latitude, final double longitude, final double zoom, final double bearing, final double tilt) {
  55. //设置目标点---点击回到当前位置
  56. CameraPosition cameraPosition = new CameraPosition.Builder()
  57. .target(new LatLng(latitude, longitude)) // Sets the center of the map to the specified location
  58. .zoom(zoom)
  59. .bearing(bearing)
  60. .tilt(tilt)
  61. .build();
  62. //set the user's viewpoint as specified in the cameraPosition object
  63. mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 3000);
  64. }
  65. /**
  66. * 动画跳转到指定位置---默认方式
  67. */
  68. public void jumpToLocationDefault(final double latitude, final double longitude) {
  69. //设置目标点---点击回到当前位置
  70. CameraPosition cameraPosition = new CameraPosition.Builder()
  71. .target(new LatLng(latitude, longitude)) // Sets the center of the map to the specified location
  72. .zoom(11)
  73. .bearing(0)
  74. .tilt(0)
  75. .build();
  76. //set the user's viewpoint as specified in the cameraPosition object
  77. mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 3000);
  78. }
  79. /**
  80. * 获取token令牌
  81. *
  82. * @param context
  83. * @return
  84. */
  85. public static String getMapboxAccessToken(@NonNull Context context) {
  86. try {
  87. // Read out AndroidManifest
  88. PackageManager packageManager = context.getPackageManager();
  89. ApplicationInfo appInfo = packageManager
  90. .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
  91. String token = appInfo.metaData.getString(MapboxConstants.KEY_META_DATA_MANIFEST);
  92. if (token == null || token.isEmpty()) {
  93. throw new IllegalArgumentException();
  94. }
  95. return token;
  96. } catch (Exception e) {
  97. // Use fallback on string resource, used for development
  98. int tokenResId = context.getResources()
  99. .getIdentifier("mapbox_access_token", "string", context.getPackageName());
  100. return tokenResId != 0 ? context.getString(tokenResId) : null;
  101. }
  102. }
  103. /**
  104. * 此方法待验证
  105. * <p/>
  106. * 给定坐标点绘制出参考路线
  107. *
  108. * @param point
  109. */
  110. public void drawMyRoute(LatLng point) {
  111. //删除所有之前的标记
  112. mapboxMap.removeAnnotations();
  113. // Set the origin waypoint to the devices location设置初始位置
  114. Waypoint origin = new Waypoint(mapboxMap.getMyLocation().getLongitude(), mapboxMap.getMyLocation().getLatitude());
  115. // 设置目的地路径--点击的位置点
  116. Waypoint destination = new Waypoint(point.getLongitude(), point.getLatitude());
  117. // Add marker to the destination waypoint
  118. mapboxMap.addMarker(new MarkerOptions()
  119. .position(new LatLng(point))
  120. .title("目的地")
  121. .snippet("My destination"));
  122. // Get route from API
  123. getRoute(origin, destination);
  124. }
  125. private DirectionsRoute currentRoute = null;
  126. private void getRoute(Waypoint origin, Waypoint destination) {
  127. MapboxDirections directions = new MapboxDirections.Builder()
  128. .setAccessToken(MAPBOX_ACCESS_TOKEN)
  129. .setOrigin(origin)
  130. .setDestination(destination)
  131. .setProfile(DirectionsCriteria.PROFILE_WALKING)
  132. .build();
  133. directions.enqueue(new Callback<DirectionsResponse>() {
  134. @Override
  135. public void onResponse(Response<DirectionsResponse> response, Retrofit retrofit) {
  136. // Print some info about the route
  137. currentRoute = response.body().getRoutes().get(0);
  138. showToastMessage(String.format("You are %d meters \nfrom your destination", currentRoute.getDistance()));
  139. // Draw the route on the map
  140. drawRoute(currentRoute);
  141. }
  142. @Override
  143. public void onFailure(Throwable t) {
  144. showToastMessage("Error: " + t.getMessage());
  145. }
  146. });
  147. }
  148. private void showToastMessage(String message) {
  149. Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
  150. }
  151. private void drawRoute(DirectionsRoute route) {
  152. // Convert List<Waypoint> into LatLng[]
  153. List<Waypoint> waypoints = route.getGeometry().getWaypoints();
  154. LatLng[] point = new LatLng[waypoints.size()];
  155. for (int i = 0; i < waypoints.size(); i++) {
  156. point[i] = new LatLng(
  157. waypoints.get(i).getLatitude(),
  158. waypoints.get(i).getLongitude());
  159. }
  160. // Draw Points on MapView
  161. mapboxMap.addPolyline(new PolylineOptions()
  162. .add(point)
  163. .color(Color.parseColor("#38afea"))
  164. .width(5));
  165. }
  166. /**
  167. * 获取地图数据,参数1代表获取当前中心点数据; 数据2 标示获取矩形区域点数据
  168. *
  169. * @param dataStyle
  170. * @return
  171. */
  172. public String getPOIDataJson(int dataStyle) {
  173. String data = null;
  174. String poiUrl = Constants.POI_URL; //请求地址
  175. String currentPoint = getMyLocationLatLon(); //当前坐标
  176. String areaCoordinates = getAreaCoordinates(); //矩形区域坐标
  177. String layerStyle = "try|cate|shop|hotel|msg"; //图层条件(暂写死)
  178. String smartRecommend = "1"; //智能开关--默认打开
  179. String currentTime = getMyTime(); //当前时间
  180. String zoneOffset = "28800000"; //时区便宜值(暂定)
  181. int currenRowNumber = 100; //查询页数最大值
  182. int pageSize = 100; //每页行数最大值
  183. String user_id = getMyUseId();
  184. if (dataStyle == 1) {
  185. data = "{currentPoint:" +currentPoint +
  186. ",layerStyle:" + layerStyle +
  187. ",smartRecommend:" + smartRecommend +
  188. ",currentTime:" + currentTime +
  189. ",zoneOffset:" + zoneOffset +
  190. ",currenRowNumber:" + currenRowNumber +
  191. ",pageSize:" + pageSize +
  192. ",user_id:" + user_id +
  193. "}";
  194. } else if (dataStyle == 2) {
  195. data = "{areaCoordinates:" + areaCoordinates +
  196. ",layerStyle:" + layerStyle +
  197. ",smartRecommend:" + smartRecommend +
  198. ",currentTime:" + currentTime +
  199. ",zoneOffset:" + zoneOffset +
  200. ",currenRowNumber:" + currenRowNumber +
  201. ",pageSize:" + pageSize +
  202. ",user_id:" + user_id +
  203. "}";
  204. }
  205. LogUtils.e("Tag", "请求地图poi数据的post_Json是:\n" + data);
  206. return data;
  207. }
  208. /**
  209. * 得到user_id
  210. *
  211. * @return
  212. */
  213. private String getMyUseId() {
  214. String user_id = SpUtils.getInitialize(context).getValue("user_id", "");
  215. return user_id;
  216. }
  217. /**
  218. * 获取当前位置的坐标--经度,维度
  219. *
  220. * @return
  221. */
  222. public String getMyLocationLatLon() {
  223. String currentPoint = mapboxMap.getMyLocation().getLongitude() + "," + mapboxMap.getMyLocation().getLatitude();
  224. return currentPoint;
  225. }
  226. /**
  227. * 获取可见区域四角坐标所围成的区域坐标--纬度,经度模式
  228. *
  229. * @return
  230. */
  231. public String getAreaCoordinates() {
  232. VisibleRegion bounds = mapboxMap.getProjection().getVisibleRegion();
  233. String topleft = bounds.farLeft.getLongitude() + "," + bounds.farLeft.getLatitude();
  234. String topright = bounds.farRight.getLongitude() + "," + bounds.farRight.getLatitude();
  235. String bottomleft = bounds.nearLeft.getLongitude() + "," + bounds.nearLeft.getLatitude();
  236. String bottomright = bounds.nearRight.getLongitude() + "," + bounds.nearRight.getLatitude();
  237. String areaCoordinates = topleft + "," + bottomleft + "," + bottomright + "," + topright + "," + topleft;
  238. return areaCoordinates;
  239. }
  240. /**
  241. * 得到设备号
  242. *
  243. * @return
  244. */
  245. public String device_id() {
  246. return Variables.device_id;
  247. }
  248. /**
  249. * 得到token令牌
  250. *
  251. * @return
  252. */
  253. public String token() {
  254. return Variables.device_id;
  255. }
  256. /**
  257. * //得到当前的时间,格式:15:30:30
  258. *
  259. * @return
  260. */
  261. private String getMyTime() {
  262. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  263. Date curDate = new Date(System.currentTimeMillis());//获取当前时间
  264. String strTime = formatter.format(curDate);
  265. return strTime;
  266. }
  267. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/623984
推荐阅读