osmdroid 在线加载谷歌瓦片(谷歌地图瓦片地址解释)

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;
    }
}


我在网上找到以下解释,比较详细的解释
地图瓦片地址:http://mt2.google.cn/vt/lyrs=m@167000000&hl=zh-CN&gl=cn&x=420&y=193&z=9&s=Galil
详细解释下

mt2.google.cn :Google瓦片服务服务器,能够尝试mt1.google.cn依然有效
。Google提供多台瓦片服务器,减轻服务器负载,提升网络访问效率。
x瓦片的横向索引,起始位置为最左边,数值为0,向右+1递增。
y瓦片的纵向索引,起始位置为最上面,数值为0,向下+1递增。
z地图的级别Zoom,最上一级为0,向下依次递增。(0~22)

​s             
(3*x + y)%8 = s
x , y , z 组合后与 8 求余便可获得 s 的值。​

其它几个参数的含义

1)mt ( 0—3)
Google 地图使用了四个服务地址,
即 http://mt ( 0—3).google.cn/......,均可以用。
地图:http://mt2.google.cn/vt/lyrs=m@&hl=zh-CN&gl=cn&x=...
影像底图:http://mt3.google.cn/vt/lyrs=s@110&hl=zh-CN&gl=cn&x=...
影像的叠加:http://mt1.google.cn/vt/imgtp=png32&lyrs=h&hl=zh-CN&gl=cn&x=...

2)lyrs=...
表示的是图层类型,即瓦片类型,
具体含义以下:
m:路线图
t:地形图
​p:带标签的地形图
​s:卫星图
y:带标签的卫星图
​h:标签层(路名、地名等)

3)&s=...
暂时未发现 "&s=..." 的意义,url有无此后缀都不影响瓦片地址的访问。
计算瓦片URL
​要想出图就必须知道地图控件可视范围起始点瓦片索引、末尾瓦片索引,中间区域的瓦片索引循环遍历便可得出。

三、osmdroid调用

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