GIS的学习(三十三)osmdroid OSMBonusPack实现Route,Bubble
程序员文章站
2022-04-04 15:53:29
...
使用开源项目OSMBonusPack实现线路和气泡的功能:
项目结构如下:
具体代码如下:
package com.osmbonus.simple; import java.util.ArrayList; import org.osmdroid.bonuspack.overlays.ExtendedOverlayItem; import org.osmdroid.bonuspack.overlays.ItemizedOverlayWithBubble; import org.osmdroid.bonuspack.routing.OSRMRoadManager; import org.osmdroid.bonuspack.routing.Road; import org.osmdroid.bonuspack.routing.RoadManager; import org.osmdroid.bonuspack.routing.RoadNode; import org.osmdroid.tileprovider.tilesource.TileSourceFactory; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapController; import org.osmdroid.views.MapView; import org.osmdroid.views.overlay.OverlayItem; import org.osmdroid.views.overlay.PathOverlay; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; /** * 备注:需要的注意事项: * 1.针对OSMBonusPack v2.x jars 是基于 osmdroid v3.0.8. 所以版本必须对应否则可能出现莫名其妙的问题。 * 2.以下是集成必须的资源信息 * •For a white bubble: * ◦drawable-mpi/bonuspack_bubble.9.png * ◦layout/bonuspack_bubble.xml * •For a dark-grey bubble: * ◦drawable-mpi/bonuspack_bubble_black.9.png * ◦layout/bonuspack_bubble_black.xml * •For the "more info" button: * ◦drawable/btn_moreinfo.xml * ◦drawable-mpi/moreinfo_arrow.png * ◦drawable-mpi/moreinfo_arrow_pressed.png * 3.地图使用的权限总结: * <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> * <uses-permission android:name="android.permission.INTERNET" /> * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> * *针对OSMBonusPack中几个重要的特性: * 1.根据地点名称查询POI * 2.查询附近的POI * 3.根据起始点规划线路 * 4.marker气泡重写和定制 * 5.地图poi点的响应处理 * * * * @author longgangbai * * 以下代码均来自官方Tutorial_1 整理 * */ public class OSMBonusPackSimpleActivity extends Activity { private MapView map; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取地图对象 map = (MapView) findViewById(R.id.map); //设置地图图源 map.setTileSource(TileSourceFactory.MAPNIK); //设置起始点和中心 GeoPoint startPoint = new GeoPoint(48.13, -1.63); MapController mapController = map.getController(); mapController.setCenter(startPoint); mapController.setZoom(9); //创建RoadManager管理器 RoadManager roadManager = new OSRMRoadManager(); //如果想需要自行车线路采用如下方式 //RoadManager roadManager = new MapQuestRoadManager(); //roadManager.addRequestOption("routeType=bicycle"); //设置线路的起始点位置 ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>(); waypoints.add(startPoint); waypoints.add(new GeoPoint(48.4, -1.9)); //end point //获取相关的线路对象 Road road = roadManager.getRoad(waypoints); //创建线路图层 PathOverlay roadOverlay = RoadManager.buildRoadOverlay(road, map.getContext()); //添加线路图层 map.getOverlays().add(roadOverlay); //设置线路的上的标志 final ArrayList<ExtendedOverlayItem> roadItems = new ArrayList<ExtendedOverlayItem>(); Drawable marker = getResources().getDrawable(R.drawable.marker_node); for (int i=0; i<road.mNodes.size(); i++){ RoadNode node = road.mNodes.get(i); ExtendedOverlayItem nodeMarker = new ExtendedOverlayItem("Step "+i, "", node.mLocation, this); nodeMarker.setMarkerHotspot(OverlayItem.HotspotPlace.CENTER); nodeMarker.setMarker(marker); roadItems.add(nodeMarker); } //创建气泡 ItemizedOverlayWithBubble<ExtendedOverlayItem> roadNodes = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(this, roadItems, map); //添加线路上气泡信息 map.getOverlays().add(roadNodes); //刷新地图 map.invalidate(); } }