Osmdroid 使用之載入google影像地圖
阿新 • • 發佈:2018-12-24
Osmdroid 使用之載入google影像
Osmdroid 是開源的GIS載入框架。
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& [email protected]&hl=en-US&gl=US&src=app&s=G",
"http://mt2.google.cn/vt/imgtp=png32&[email protected]&hl=en-US&gl=US&src=app&s=G",
"http://mt3.google.cn/vt/imgtp=png32&[email protected]&hl=en-US&gl=US&src=app&s=G"
};
//向量底圖 lyrs=m lyrs=是指瓦片型別 有標註 在國內但有偏移,國外暫無測試
static final String[] baseUrl_GoogleRoad = new String[]{
"http://mt1.google.cn/vt/ [email protected]&hl=en-US&gl=US&src=app&s=G",
"http://mt2.google.cn/vt/[email protected]&hl=en-US&gl=US&src=app&s=G",
"http://mt3.google.cn/vt/[email protected]&hl=en-US&gl=US&src=app&s=G"
};
//影像底圖 lyrs=y 有標註 在國內但有偏移,國外暫無測試
static final String[] baseUrl_Google_cn = new String[]{
"http://mt0.google.cn/vt/ [email protected]&hl=zh-CN&gl=cn&src=app&s=G",
"http://mt1.google.cn/vt/[email protected]&hl=zh-CN&gl=cn&src=app&s=G",
"http://mt2.google.cn/vt/[email protected]&hl=zh-CN&gl=cn&src=app&s=G",
"http://mt3.google.cn/vt/[email protected]&hl=zh-CN&gl=cn&src=app&s=G"
};
//影像底圖 lyrs=s 沒有標註
static final String[] baseUrl_GoogleSatellite = new String[]{
"http://mt0.google.cn/vt/[email protected]&hl=en-US&gl=US&src=app&s=G",
"http://mt1.google.cn/vt/[email protected]&hl=en-US&gl=US&src=app&s=G",
"http://mt2.google.cn/vt/[email protected]&hl=en-US&gl=US&src=app&s=G",
"http://mt3.google.cn/vt/[email protected]&hl=en-US&gl=US&src=app&s=G"
};
String urlXYZ = "&x={$x}&y={$y}&z={$z}";
public GoogleTileSource(String[] urls) {
super("Google", 2, 22, 256, ".png", urls);
}
@Override
public String getTileURLString(long pMapTileIndex) {
String url = getBaseUrl() + urlXYZ
.replace("{$x}", MapTileIndex.getX(pMapTileIndex) + "")
.replace("{$y}", MapTileIndex.getY(pMapTileIndex) + "")
.replace("{$z}", MapTileIndex.getZoom(pMapTileIndex) + "");
return url;
}
}
Activity載入地圖
public class GoogleImgActivity extends Activity {
private static final int PERMISSION_REQUEST_CODE = 0x001;
private static final String TAG = GoogleImgActivity.class.getName();
private MapView mapView = null;
private static final String FILE_PATH = "/osmdroid/google/Satellite/";
private static final String SP_NAME = "osmdroid_google_Satellite";
private RotationGestureOverlay mRotationGestureOverlay;
private ScaleBarOverlay mScaleBarOverlay;
private CompassOverlay mCompassOverlay;
private MyLocationNewOverlay mLocationOverlay;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
return;
}
init(true);
}
private void init(boolean setFile) {
if (setFile) {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), FILE_PATH);
if (!file.exists() && !file.mkdirs()) {
Log.d(TAG, "onCreate: " + file.getAbsolutePath() + " mkdirs fail ");
}
Configuration.getInstance().setOsmdroidBasePath(new File(path + FILE_PATH));
Configuration.getInstance().setOsmdroidTileCache(new File(path + FILE_PATH));
}
Configuration.getInstance().load(getApplicationContext(), getSharedPreferences(SP_NAME, MODE_PRIVATE));
//TODO check permissions
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setDrawingCacheEnabled(true);
mapView.setMultiTouchControls(true);// 觸控放大縮小
mapView.setMaxZoomLevel(22.0);
mapView.setMinZoomLevel(6.0);
mapView.getController().setZoom(12.0);
//mMapView
mapView.setUseDataConnection(true);
mapView.setMultiTouchControls(true);// 觸控放大縮小
//是否顯示地圖資料來源
mapView.getOverlayManager().getTilesOverlay().setEnabled(true);
//載入谷歌地圖,設定地圖資料來源的形式
mapView.setTileSource(new GoogleTileSource(GoogleTileSource.baseUrl_GoogleSatellite));
mapView.getController().setZoom(18.0);
mapView.getController().setCenter(new GeoPoint(31.16590625, 121.28804862));
//地圖自由旋轉
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);
this.mLocationOverlay.setDirectionArrow(((BitmapDrawable)
mapView.getContext().getResources().getDrawable(org.osmdroid.library.R.drawable.direction_arrow)).getBitmap(),
((BitmapDrawable)
mapView.getContext().getResources().getDrawable(org.osmdroid.library.R.drawable.person)).getBitmap());
mapView.getOverlays().add(this.mLocationOverlay);
mLocationOverlay.enableMyLocation(); //設定可視
}
// 請求許可權相容低版本
private void requestPermissions(String... permissions) {
ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(permissions, grantResults)) {
Log.d(TAG, "onRequestPermissionsResult: true");
init(true);
} else {
Log.d(TAG, "onRequestPermissionsResult: false");
init(false);
}
}
// 含有全部的許可權
private boolean hasAllPermissionsGranted(@NonNull String[] permissions, @NonNull int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
@Override
public void onResume() {
super.onResume();
// Configuration.getInstance().load(getApplicationContext(), PreferenceManager.getDefaultSharedPreferences(getApplicationContext()));
if (mapView != null)
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
Configuration.getInstance().save(getApplicationContext(), getSharedPreferences(SP_NAME, MODE_PRIVATE));
if (mapView != null)
mapView.onPause();
}
}