osmdroid做为一个开源的项目,在在线地图上仍是作的不错,离线地图上也能支持多种格式,例如:zip,sqlite,.mbtiles,gemf,map,gpkg等格式。就是不能直接加载shp文件大为苦恼。osmdroid没有能够设置坐标参考系的接口。只有地图坐标与屏幕坐标的转换绘制。接下来就讲讲osmdroid 在线加载谷歌瓦片。html
影像数据,加上地图标注java
影像数据,没有地图标注android
矢量数据地图sql
package com.googlemap.online; import org.osmdroid.tileprovider.MapTile; import org.osmdroid.tileprovider.tilesource.XYTileSource; public class GoogleTileSource extends XYTileSource { //影像的叠加层 lyrs=h static final String[] baseUrl_GoogleLabel = new String[]{ "http://mt1.google.cn/vt/imgtp=png32&lyrs=h@210000000&hl=en-US&gl=US&src=app&s=G", "http://mt2.google.cn/vt/imgtp=png32&lyrs=h@210000000&hl=en-US&gl=US&src=app&s=G", "http://mt3.google.cn/vt/imgtp=png32&lyrs=h@210000000&hl=en-US&gl=US&src=app&s=G" }; //矢量底图 lyrs=m lyrs=是指瓦片类型 有标注 在国内但有偏移,国外暂无测试 static final String[] baseUrl_GoogleRoad = new String[]{ "http://mt1.google.cn/vt/lyrs=m@209712068&hl=en-US&gl=US&src=app&s=G", "http://mt2.google.cn/vt/lyrs=m@209712068&hl=en-US&gl=US&src=app&s=G", "http://mt3.google.cn/vt/lyrs=m@209712068&hl=en-US&gl=US&src=app&s=G" }; //影像底图 lyrs=y 有标注 在国内但有偏移,国外暂无测试 static final String[] baseUrl_Google_cn = new String[]{ "http://mt0.google.cn/vt/lyrs=y@126&hl=zh-CN&gl=cn&src=app&s=G", "http://mt1.google.cn/vt/lyrs=y@126&hl=zh-CN&gl=cn&src=app&s=G", "http://mt2.google.cn/vt/lyrs=y@126&hl=zh-CN&gl=cn&src=app&s=G", "http://mt3.google.cn/vt/lyrs=y@126&hl=zh-CN&gl=cn&src=app&s=G" }; //影像底图 lyrs=s 没有标注 static final String[] baseUrl_GoogleSatellite = new String[]{ "http://mt0.google.cn/vt/lyrs=s@126&hl=en-US&gl=US&src=app&s=G", "http://mt1.google.cn/vt/lyrs=s@126&hl=en-US&gl=US&src=app&s=G", "http://mt2.google.cn/vt/lyrs=s@126&hl=en-US&gl=US&src=app&s=G", "http://mt3.google.cn/vt/lyrs=s@126&hl=en-US&gl=US&src=app&s=G" }; String urlXYZ = "&x={$x}&y={$y}&z={$z}"; public GoogleTileSource() { super("Google", 2, 22, 256, "png", baseUrl_GoogleRoad); } @Override public String getTileURLString(MapTile aTile) { String url = getBaseUrl() + urlXYZ .replace("{$x}", aTile.getX() + "") .replace("{$y}", aTile.getY() + "") .replace("{$z}", aTile.getZoomLevel() + ""); return url; } }
package com.googlemap.online; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; import org.osmdroid.tileprovider.MapTileProviderBasic; import org.osmdroid.tileprovider.tilesource.TileSourceFactory; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapView; import org.osmdroid.views.overlay.ScaleBarOverlay; import org.osmdroid.views.overlay.TilesOverlay; import org.osmdroid.views.overlay.compass.CompassOverlay; import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider; import org.osmdroid.views.overlay.gestures.RotationGestureOverlay; import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; public class MainActivity extends Activity implements View.OnClickListener { private MapView mapView; //et地图旋转 private RotationGestureOverlay mRotationGestureOverlay; //比例尺 private ScaleBarOverlay mScaleBarOverlay; //指南针方向 private CompassOverlay mCompassOverlay = null; //设置导航图标的位置 private MyLocationNewOverlay mLocationOverlay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { findViewById(R.id.button1).setOnClickListener(this); mapView = (MapView) findViewById(R.id.mymapview); mapView.setDrawingCacheEnabled(true); mapView.setMaxZoomLevel(22); mapView.setMinZoomLevel(6); mapView.getController().setZoom(12); //加载谷歌地图,设置地图数据源的形式 mapView.setTileSource(new GoogleTileSource()); mapView.setUseDataConnection(true); mapView.setMultiTouchControls(true);// 触控放大缩小 //是否显示地图数据源 mapView.getOverlayManager().getTilesOverlay().setEnabled(true); //地图自由旋转 mRotationGestureOverlay = new RotationGestureOverlay(mapView); mRotationGestureOverlay.setEnabled(true); mapView.getOverlays().add(this.mRotationGestureOverlay); //比例尺配置 final DisplayMetrics dm = getResources().getDisplayMetrics(); mScaleBarOverlay = new ScaleBarOverlay(mapView); mScaleBarOverlay.setCentred(true); mScaleBarOverlay.setAlignBottom(true); //底部显示 mScaleBarOverlay.setScaleBarOffset(dm.widthPixels / 5, 80); mapView.getOverlays().add(this.mScaleBarOverlay); //指南针方向 mCompassOverlay = new CompassOverlay(this, new InternalCompassOrientationProvider(this), mapView); mCompassOverlay.enableCompass(); mapView.getOverlays().add(this.mCompassOverlay); //设置导航图标 this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this), mapView); mapView.getOverlays().add(this.mLocationOverlay); mLocationOverlay.enableMyLocation(); //设置可视 } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: //定位当前的位置,并设置缩放级别 mapView.getController().setZoom(18); mapView.getController().setCenter(new GeoPoint(23.12648183, 113.365548756)); break; default: break; } } //增长图层的形式 public void initExtendMap() { MapTileProviderBasic mapTileProviderBasic = new MapTileProviderBasic(this, new GoogleTileSource()); TilesOverlay tilesOverlay = new TilesOverlay(mapTileProviderBasic, this); mapView.getOverlays().add(tilesOverlay); } @Override public void onPause() { super.onPause(); } @Override public void onResume() { super.onResume(); } }
还有网上下载瓦片数据须要存储的权限缓存
定位,联网的权限服务器
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />
在线瓦片加载后是有缓存,有时候比较缓慢。默认缓存是放在根目录下面的osmdroid文件夹。若是你一个app出现屡次测试不一样的在线资源,有可能出现叠加的状况。出现就删除缓存吧。还有测试时候最好定位一下你当前的位置,比较好确认。网络
博客源码下载:http://download.csdn.net/download/qq_16064871/10168741app