OpenStreetMap開發文檔

前言

OpenStreetMap社區是一個由地圖製作愛好者組成的社區,這些愛好者提供並維護世界各地關於道路、小道、咖啡館、鐵路車站等各種各樣的數據。OpenStreetMap開源項目可以讓程序開發更加靈活,圖源更加豐富,例如可以使用谷歌地圖,以解決國內無法使用谷歌服務的尷尬。國內戶外導航軟件,例如:

行者戶外幫小狼信標都使用了OpenStreetMap。

Android版OpenStreetMap的github地址:osmdroid

5.2地圖緩存的是瓦片,5.4之後地圖緩存到數據庫

一、環境配置

1、Gradle中添加依賴

<code>compile 'org.osmdroid:osmdroid-android:5.2@aar'/<code>

2、權限配置

<code><uses-permission>
<uses-permission>
<uses-permission>/<code>

提示:編譯版本為23或以上版本,要注意動態獲取讀寫存儲空間權限,否則地圖可能不顯示

二、基礎MapView使用

1、使用內置地圖源

(1)、在佈局文件中添加MapView控件,在代碼中找到控件並設置圖源

<code><org.osmdroid.views.mapview>     android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>/<org.osmdroid.views.mapview>/<code>
<code>MapView mMapView= (MapView) findViewById(R.id.mapView);
mMapView.setTileSource(TileSourceFactory.CYCLEMAP);//(OCM等高)若不設置,則默認使用的是MAPNIK(OSM街道)/<code>

(2)、直接在代碼中new MapView,然後設置圖源,並將MapView添加到父佈局中

<code>MapView mMapView=new MapView(this);
mMapView.setTileSource(TileSourceFactory.MAPNIK);/<code>

2、設置其他地圖源

(1)、谷歌圖源

a、新建一個谷歌圖源類,繼承OnlineTileSourceBase

<code>public class GoogleMapsTileSource extends OnlineTileSourceBase {
/**
* @param aName a human-friendly name for this tile source 自定圖源義名字,會在手機外部存儲中新建以該名字命名的文件夾,瓦片存儲在其中
* @param aZoomMinLevel the minimum zoom level this tile source can provide 最小縮放級別
* @param aZoomMaxLevel the maximum zoom level this tile source can provide 最大縮放級別
* @param aTileSizePixels the tile size in pixels this tile source provides 瓦片質量 (256)
* @param aImageFilenameEnding the file name extension used when constructing the filename 瓦片格式(jpg[有損壓縮率高、不透明]、png[無損、透明])
* @param aBaseUrl the base url(s) of the tile server used when constructing the url to download the tiles 下載瓦片的鏈接(前綴)
*/
public GoogleMapsTileSource(String aName, int aZoomMinLevel, int aZoomMaxLevel, int aTileSizePixels, String aImageFilenameEnding, String[] aBaseUrl) {
super(aName, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding, aBaseUrl);
}

@Override
public String getTileURLString(MapTile aTile) {
return getBaseUrl() + "&x=" + aTile.getX() + "&y=" + aTile.getY() + "&z=" + aTile.getZoomLevel();
}
}/<code>

b、new一個谷歌圖源對象,並設置MapView圖源

<code>String str1 = "http://mt0.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
String str2 = "http://mt1.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";

String str3 = "http://mt2.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
String str4 = "http://mt3.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
GoogleMapsTileSource googleMapsTileSource = new GoogleMapsTileSource("GoogleNormal", 2, 19, 256, ".png", new String[]{str1, str2, str3, str4});
mMapView.setTileSource(googleMapsTileSource);/<code>

(2)、必應等圖源,使用方法類似於谷歌圖源

參考文檔:http://blog.csdn.net/youngkingyj/article/details/23365849

3、讓瓦片適應不同像素密度

默認地圖顯示的字體小,圖片像素高,可設置以下代碼,使地圖適應不同像素密度,更美觀

<code>mMapView.setTilesScaledToDpi(true);/<code>

4、添加指南針

<code>CompassOverlay mCompassOverlay = new CompassOverlay(MainActivity.this, new InternalCompassOrientationProvider(MainActivity.this), mMapView);
mMapView.getOverlays().add(mCompassOverlay);
mCompassOverlay.enableCompass();/<code>

按此方法添加指南針之後,部分手機仍不顯示指南針,原因未知

5、添加比例尺ScaleBarOverlay mScaleBarOverlay = new ScaleBarOverlay(mMapView);mMapView.getOverlays().add(mScaleBarOverlay);

添加上面代碼後,比例尺顯示在左上角,而且不美觀,可以繼續添加下面代碼,使比例尺顯示在左下角

<code>mScaleBarOverlay.setAlignBottom(true);
mScaleBarOverlay.setLineWidth(1 * (getResources().getDisplayMetrics()).density);
mScaleBarOverlay.setMaxLength(0.85f);/<code>

6、設置地圖中心

<code>GeoPoint geopoint = new GeoPoint(39.986250, 116.400025);
MapController mMapController= (MapController) mMapView.getController();//獲取MapView控制器
mMapController.setCenter(geopoint);//設置地圖中心/<code>

7、其他設置

(1)、設置縮放界別

<code>mMapController.setZoom(15);//設置縮放級/<code>

(2)、設置縮放按鈕可見

<code>mMapView.setBuiltInZoomControls(true);//設置縮放按鈕可見/<code>

(3)、設置多指觸控可用

<code>mMapView.setMultiTouchControls(true);//設置多指觸控可用/<code>

(4)、關閉硬件加速

<code>mMapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);//關閉硬件加速(繪製軌跡時需要)/<code>

(5)、地圖可旋轉

<code>RotationGestureOverlay mRotationGestureOverlay = new RotationGestureOverlay(this, mMapView);
mRotationGestureOverlay.setEnabled(true);
mMapView.getOverlays().add(mRotationGestureOverlay);/<code>

三、進階使用

1、自定義瓦片緩存位置

默認會在外部存儲中新建名為somdroid的文件夾,瓦片就存儲在其中。

自定義緩存位置就是在外部存儲中創建一個文件夾,然後設置為瓦片的緩存位置,放在MapView初始化之前 例如:

<code>File dir = new File(Environment.getExternalStorageDirectory(), "AAA");//新建文件夾
if (dir.exists()) {
File nomedia = new File(dir.getAbsoluteFile() + "/.nomedia");
if (!nomedia.exists()) {
try {
nomedia.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
dir.mkdirs();
File file = dir.getAbsoluteFile();
try {
new File(file + "/.nomedia").createNewFile();
} catch (Exception ex) {
android.util.Log.e(IMapView.LOGTAG, "unable to create a nomedia file. downloaded tiles may be visible to the gallery.", ex);
}
}
OpenStreetMapTileProviderConstants.setCachePath(dir + "");//設置MapView的緩存路徑/<code>

2、添加Marker

<code>Marker marker = new Marker(mMapView);
marker.setIcon(getResources().getDrawable(R.mipmap.ic_launcher));//設置圖標
marker.setPosition(geopoint);//設置位置
marker.setAnchor(0.5f, 0.5f);//設置偏移量
marker.setTitle("我是Titile");//設置標題
marker.setSubDescription("我是SubDescription");//設置說明
mMapView.getOverlays().add(marker);//添加marker到MapView/<code>

點擊Marker之後會出現氣泡,顯示title和subDescription

3、連線

<code>PathOverlay pathOverlay = new PathOverlay(Color.BLUE, 10, this);
pathOverlay.addPoint(new GeoPoint(39.986250, 116.400025));
pathOverlay.addPoint(new GeoPoint(39.886250, 116.300025));
mMapView.getOverlays().add(pathOverlay);/<code>

連線時務必關閉硬件加速,否則可能顯示不出來連的線

4、離線地圖下載

<code>CacheManager cacheManager = new CacheManager(mMapView);//獲取下載器
BoundingBoxE6 boundingBoxE6 = mMapView.getBoundingBox();//獲取當前區域
int tileNum = cacheManager.possibleTilesInArea(boundingBoxE6, 8, 16);//計算當前區域8-16級的瓦片數量
cacheManager.downloadAreaAsync(this, boundingBoxE6, 8, 16, new CacheManager.CacheManagerCallback() {//下載
@Override
public void onTaskComplete() {
//下載完成後的回調
}
});/<code>

下載時會有進度框,若點擊進度框以外的區域會取消下載,若想修改邏輯可參考CacheManager,自定義一個CacheManage


分享到:


相關文章: