我的活动中只有一个空格。我在我的清单中添加了写入外部存储权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我的活动:
@Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_osm_map);
if(isStoragePermissionGranted()){
initMap();
}
}
private void initMap(){
MapView map = (MapView) findViewById(R.id.osm_map);
final float scale = getBaseContext().getResources().getDisplayMetrics().density;
final int newScale = (int) (256 * scale);
String[] OSMSource = new String[1];
OSMSource[0] = "https://gps.4u.uz/";
map.setTileSource(new OnlineTileSourceBase("USGS Topo", null, 1, 18, 256, "", OSMSource) {
@Override
public String getTileURLString(MapTile aTile) {
return getBaseUrl() + aTile.getZoomLevel() + "/" + aTile.getY() + "/" + aTile.getX()
+ mImageFilenameEnding;
}
});
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
MapController mapController = (MapController) map.getController();
mapController.setZoom(13);
GeoPoint geoPoint = new LatLonPoint(41.63, 64.59);
OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", geoPoint);
Drawable myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.ic_marker);
myLocationOverlayItem.setMarker(myCurrentLocationMarker);
final ArrayList<OverlayItem> items = new ArrayList<>();
items.add(myLocationOverlayItem);
CustomOverLay overlays = new CustomOverLay(myCurrentLocationMarker, map, this);
OverlayItem overlayItem = new OverlayItem("Title", "Snippet", geoPoint);
overlays.addOverlayItem(overlayItem);
map.getOverlays().add(overlays);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
map.setBottom(height);
map.setRight(width);
mapController.setCenter(geoPoint);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.d(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
initMap();
//resume tasks needing this permission
}else{
Log.d(TAG, "permission dined after requesting");
}
}
private static int REQUEST_CODE = 1;
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
Log.d(TAG, "Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
return false;
}
} else {
Log.d(TAG,"Permission is granted");
return true;
}
}
当我发现运行时的权限请求解决了问题时,我决定添加它。但它没有解决问题。
有趣的是osmdroid文件位于内部存储中,而不是外部存储。我只有cache.db和cache.db-journal文件。正如我所读,osmdroid需要外部存储来存储缓存。我试过了OpenStreetMapTileProviderConstants.setCachePath(this.getFilesDir().getAbsolutePath());
。但是没有这样的方法。这就是我删除这一行的原因。我无法使用this pitch
我正在使用osmdroid 4.2版本
答案 0 :(得分:1)
您需要在加载地图之前申请权限。如果你之后再这样做,那就太晚了。在您的示例中,将在布局膨胀时创建地图。
那就是说,我没有测试这个工作,但基本上在设置了权限之后,你可以使用以下内容重置tile提供者的地图:
mMapView.setTileProvider(new MapTileProviderBasic(getContext()));
这可能会解决您的问题,否则,您必须在创建地图视图之前请求许可。
哦,那个示例代码,可能需要osmdroid 4.4