Android 利用OSMdroid开发GIS 添加天地图

部署看这个:Android 利用OSMdroid开发GIS-优快云博客

使用天地图需要开启明文网络,http请求

直接上源码

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
 
   <org.osmdroid.views.MapView
       android:id="@+id/mapView"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
 
   <Button
       android:id="@+id/btnLocation"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="定位"
       android:layout_margin="10dp"
       android:layout_alignParentTop="true"/>
 
</RelativeLayout>

 MainActivity.java:

package com.chy.osmdroid;


import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.chy.layers.LayerTileSources;
import com.chy.permission.PermissionUtils;
import org.osmdroid.api.IMapController;
import org.osmdroid.tileprovider.MapTileProviderBasic;
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.util.MapTileIndex;
import org.osmdroid.views.CustomZoomButtonsController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.MinimapOverlay;
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 AppCompatActivity {
    private static final int REQUEST_PERMISSION_CODE = 0;// 权限所用
    // 动态申请权限
    private String[] permissions = {
            Manifest.permission.INTERNET,// 网络权限
            Manifest.permission.ACCESS_COARSE_LOCATION,// 精细定位
            Manifest.permission.ACCESS_FINE_LOCATION,// 粗定位
            Manifest.permission.ACCESS_WIFI_STATE,// 定位权限
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private MapView mapView;
    private LocationManager locationManager;// 定位管理器
    private Button btnLocation;// 定位按钮
    private boolean isLocation = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getPermission();// 获取权限

        initControls();
    }

    /**
     * 权限
     * */
    private void getPermission(){
        if (PermissionUtils.hasPermissions(MainActivity.this, permissions)) {
            initMap();// 调用初始化地图
        } else {
            PermissionUtils.requestPermissions(MainActivity.this, REQUEST_PERMISSION_CODE, permissions);
            Toast.makeText(getApplicationContext(), "地图加载失败!", Toast.LENGTH_SHORT).show();
        }
    }

    // 地图初始化
    private void initMap(){
        // 获取mapView实例
        mapView = findViewById(R.id.mapView);
        mapView.setUseDataConnection(true);
        // 加载在线地图-高德地图
        //mapView.setTileSource(LayerTileSources.AutoNaviVector);
        // 加载天地图影像图
        mapView.setTileSource(LayerTileSources.TDTIMG_W);
        // 加载天地图标注图
        TilesOverlay tilesOverlay = new TilesOverlay(new MapTileProviderBasic(this,LayerTileSources.TDTCIA_W),this);
        mapView.getOverlayManager().add(tilesOverlay);




        // 设置最小缩放比例
        mapView.setMinZoomLevel(3.0);
        // 设置最大缩放比例
        mapView.setMaxZoomLevel(18.0);
        // 让瓦片适应不同像素密度:默认地图显示的字体小,图片像素高,可设置以下代码,使地图适应不同像素密度,更美观
        //mapView.setTilesScaledToDpi(true);

        IMapController mapController = mapView.getController();
        // 设置地图初始级别
        mapController.setZoom(11.0);

        // 设置初始中心点
        GeoPoint centerPoint = new GeoPoint(43.90, 125.33);
        mapController.setCenter(centerPoint);

        //启用缩放及滑动手势
        //mapView.setBuiltInZoomControls(true);// 废弃得方法,被下面方法所替代
        mapView.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER);
        mapView.setMultiTouchControls(true);
        mapView.getOverlayManager().getTilesOverlay().setEnabled(true);
        mapView.setSelected(true);
        
    }

    // 控件初始化
    private void initControls(){
        btnLocation = findViewById(R.id.btnLocation);
        // 点击事件
        btnLocation.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("MissingPermission")
            @Override
            public void onClick(View v) {
                //创建位置管理器实例
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                if (!isLocation){
                    // 注册位置监听器
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                    isLocation = !isLocation;
                }else {
                    // 停止位置更新
                    locationManager.removeUpdates(locationListener);
                    isLocation = !isLocation;
                    Toast.makeText(getApplicationContext(),"停止位置更新",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }


    /**
     * 定位监听
     * */
    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // 处理位置变化
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            Toast.makeText(getApplicationContext(),"lat:"+latitude+"lon:"+longitude,Toast.LENGTH_SHORT).show();
            // 在地图上显示当前位置

            // 定位
            MyLocationNewOverlay mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(getApplicationContext()), mapView);
            mLocationOverlay.enableMyLocation();
            // osmdroid获取的坐标点为空
            // GeoPoint myLocation = mLocationOverlay.getMyLocation();

            // 使用本机获取的经纬度-设置经纬度
            GeoPoint myLocation = new GeoPoint(latitude,longitude);
            mapView.getController().setZoom(18.0);
            mapView.getController().setCenter(myLocation);
            mapView.getOverlays().add(mLocationOverlay);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(getApplicationContext(),"onStatusChanged",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(getApplicationContext(),"onProviderEnabled",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(),"onProviderDisabled",Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止位置更新
        if (locationManager != null){
            locationManager.removeUpdates(locationListener);
        }

    }
}

地图工具类:

LayerTileSources.java
package com.chy.layers;

import android.util.Log;

import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
import org.osmdroid.util.MapTileIndex;

/**
 * 谷歌、高德等瓦片地图
 *
 * @author jiang zhu on 2019/10/18
 */
public class LayerTileSources extends TileSourceFactory {

    //谷歌卫星混合
    public static final OnlineTileSourceBase GoogleHybrid = new XYTileSource("Google-Hybrid",
            0, 19, 512, ".png", new String[]{
            "http://mt0.google.cn",
            "http://mt1.google.cn",
            "http://mt2.google.cn",
            "http://mt3.google.cn",

    }) {
        @Override
        public String getTileURLString(long pMapTileIndex) {
            Log.d("url", getBaseUrl() + "/vt/lyrs=y&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex));
            return getBaseUrl() + "/vt/lyrs=y&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
        }
    };

    //谷歌卫星
    public static final OnlineTileSourceBase GoogleSat = new XYTileSource("Google-Sat",
            0, 19, 512, ".png", new String[]{
            "http://mt0.google.cn",
            "http://mt1.google.cn",
            "http://mt2.google.cn",
            "http://mt3.google.cn",

    }) {
        @Override
        public String getTileURLString(long pMapTileIndex) {

            return getBaseUrl() + "/vt/lyrs=s&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);

        }
    };
    //谷歌地图
    public static final OnlineTileSourceBase GoogleRoads = new XYTileSource("Google-Roads",
            0, 18, 512, ".png", new String[]{
            "http://mt0.google.cn",
            "http://mt1.google.cn",
            "http://mt2.google.cn",
            "http://mt3.google.cn",

    }) {
        @Override
        public String getTileURLString(long pMapTileIndex) {
            return getBaseUrl() + "/vt/lyrs=m&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
        }
    };
    //谷歌地形
    public static final OnlineTileSourceBase GoogleTerrain = new XYTileSource("Google-Terrain",
            0, 16, 512, ".png", new String[]{
            "http://mt0.google.cn",
            "http://mt1.google.cn",
            "http://mt2.google.cn",
            "http://mt3.google.cn",

    }) {
        @Override
        public String getTileURLString(long pMapTileIndex) {
            return getBaseUrl() + "/vt/lyrs=t&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
        }
    };
    //谷歌地形带标注
    public static final OnlineTileSourceBase GoogleTerrainHybrid = new XYTileSource("Google-Terrain-Hybrid",
            0, 16, 512, ".png", new String[]{
            "http://mt0.google.cn",
            "http://mt1.google.cn",
            "http://mt2.google.cn",
            "http://mt3.google.cn",

    }) {
        @Override
        public String getTileURLString(long pMapTileIndex) {
            return getBaseUrl() + "/vt/lyrs=p&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
        }
    };

    /*******【上面的谷歌地图无法使用】*********/

    //高德地图
    public static final OnlineTileSourceBase AutoNaviVector = new XYTileSource("AutoNavi-Vector",
            0, 20, 256, ".png", new String[]{
            "https://wprd01.is.autonavi.com/appmaptile?",
            "https://wprd02.is.autonavi.com/appmaptile?",
            "https://wprd03.is.autonavi.com/appmaptile?",
            "https://wprd04.is.autonavi.com/appmaptile?",

    }) {
        @Override
        public String getTileURLString(long pMapTileIndex) {
            Log.d("url", getBaseUrl() + "x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z="
                    + MapTileIndex.getZoom(pMapTileIndex) + "&lang=zh_cn&size=1&scl=1&style=7&ltype=7");
            return getBaseUrl() + "x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z="
                    + MapTileIndex.getZoom(pMapTileIndex) + "&lang=zh_cn&size=1&scl=1&style=7&ltype=7";
        }
    };

    // 天地图申请的tk值
    private static String TK = "&tk=352d4b1313777a8643542046a28d17e5";
    //天地图-有标注电子地图 _W是墨卡托投影  _c是国家2000的坐标系
    public static OnlineTileSourceBase TDTCIA_W = new XYTileSource("Tian Di Tu CIA",
            0, 20, 256, "",
            new String[]{"http://t0.tianditu.com/DataServer?T=cia_w" + TK,
                    "http://t1.tianditu.com/DataServer?T=cia_w" + TK,
                    "http://t2.tianditu.com/DataServer?T=cia_w" + TK,
                    "http://t3.tianditu.com/DataServer?T=cia_w" + TK,
                    "http://t4.tianditu.com/DataServer?T=cia_w" + TK,
                    "http://t5.tianditu.com/DataServer?T=cia_w" + TK,
                    "http://t6.tianditu.com/DataServer?T=cia_w" + TK,
                    "http://t7.tianditu.com/DataServer?T=cia_w" + TK
            }) {
        @Override
        public String getTileURLString(final long pMapTileIndex) {
            Log.d("url", getBaseUrl() + "&X=" + MapTileIndex.getX(pMapTileIndex) + "&Y=" + MapTileIndex.getY(pMapTileIndex)
                    + "&L=" + MapTileIndex.getZoom(pMapTileIndex));
            return getBaseUrl() + "&X=" + MapTileIndex.getX(pMapTileIndex) + "&Y=" + MapTileIndex.getY(pMapTileIndex)
                    + "&L=" + MapTileIndex.getZoom(pMapTileIndex);
        }
    };


    // 天地图-影像地图 _W墨卡托投影 _c国家2000的坐标系
    public static final OnlineTileSourceBase TDTIMG_W = new XYTileSource("Tian Di Tu IMG",
            0,20,256,"",
        new String[]{"http://t0.tianditu.com/DataServer?T=img_w"+TK,
                "http://t1.tianditu.com/DataServer?T=img_w"+TK,
                "http://t2.tianditu.com/DataServer?T=img_w"+TK,
                "http://t3.tianditu.com/DataServer?T=img_w"+TK,
                "http://t4.tianditu.com/DataServer?T=img_w"+TK,
                "http://t5.tianditu.com/DataServer?T=img_w"+TK,
                "http://t6.tianditu.com/DataServer?T=img_w"+TK,
                "http://t7.tianditu.com/DataServer?T=img_w"+TK}) {

       @Override
        public String getTileURLString(long pMapTileIndex) {
           return getBaseUrl() + "&X=" + MapTileIndex.getX(pMapTileIndex) + "&Y=" + MapTileIndex.getY(pMapTileIndex)
                   + "&L=" + MapTileIndex.getZoom(pMapTileIndex);
        }

    };


    // 天地图-电子地图 _W墨卡托投影 _c国家2000的坐标系
    public static final OnlineTileSourceBase TDTVEC_W = new XYTileSource("Tian Di Tu VEC",
            0,20,256,"",
            new String[]{"http://t0.tianditu.com/DataServer?T=vec_w"+TK,
                    "http://t1.tianditu.com/DataServer?T=vec_w"+TK,
                    "http://t2.tianditu.com/DataServer?T=vec_w"+TK,
                    "http://t3.tianditu.com/DataServer?T=vec_w"+TK,
                    "http://t4.tianditu.com/DataServer?T=vec_w"+TK,
                    "http://t5.tianditu.com/DataServer?T=vec_w"+TK,
                    "http://t6.tianditu.com/DataServer?T=vec_w"+TK,
                    "http://t7.tianditu.com/DataServer?T=vec_w"+TK}) {

        @Override
        public String getTileURLString(long pMapTileIndex) {
            return getBaseUrl() + "&X=" + MapTileIndex.getX(pMapTileIndex) + "&Y=" + MapTileIndex.getY(pMapTileIndex)
                    + "&L=" + MapTileIndex.getZoom(pMapTileIndex);
        }

    };

    // 天地图-地形图 _W墨卡托投影 _c国家2000的坐标系
    public static final OnlineTileSourceBase TDTTER_W = new XYTileSource("Tian Di Tu TER",
            0,20,256,"",
            new String[]{"http://t0.tianditu.com/DataServer?T=ter_w"+TK,
                    "http://t1.tianditu.com/DataServer?T=ter_w"+TK,
                    "http://t2.tianditu.com/DataServer?T=ter_w"+TK,
                    "http://t3.tianditu.com/DataServer?T=ter_w"+TK,
                    "http://t4.tianditu.com/DataServer?T=ter_w"+TK,
                    "http://t5.tianditu.com/DataServer?T=ter_w"+TK,
                    "http://t6.tianditu.com/DataServer?T=ter_w"+TK,
                    "http://t7.tianditu.com/DataServer?T=ter_w"+TK}) {

        @Override
        public String getTileURLString(long pMapTileIndex) {
            return getBaseUrl() + "&X=" + MapTileIndex.getX(pMapTileIndex) + "&Y=" + MapTileIndex.getY(pMapTileIndex)
                    + "&L=" + MapTileIndex.getZoom(pMapTileIndex);
        }

    };

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值