Android ArcGis加载地图 下载瓦片实现离线地图

public class GaoDeLayer extends TiledServiceLayer {

    private final int GaoDeLayerType;
    private final String cachePath;// 缓存目录
    private boolean whetheCachedMap = false;// 是否缓存地图
    private String offLineMapPath;// 离线地图目录
    public GaoDeLayer(int layerType, boolean whetheCachedMap,
                      String cachePath,String offLineMapPath) {
        super(true);
        this.whetheCachedMap = whetheCachedMap;
        this.cachePath = cachePath;
        this.GaoDeLayerType = layerType;
        this.offLineMapPath = offLineMapPath;
        this.init();
    }

    private final double[] scales = new double[]{591657527.591555,
            295828763.79577702, 147914381.89788899, 73957190.948944002,
            36978595.474472001, 18489297.737236001, 9244648.8686180003,
            4622324.4343090001, 2311162.217155, 1155581.108577, 577790.554289,
            288895.277144, 144447.638572, 72223.819286, 36111.909643,
            18055.954822, 9027.9774109999998, 4513.9887049999998, 2256.994353,
            1128.4971760000001};
    private final double[] resolutions = new double[]{156543.03392800014,
            78271.516963999937, 39135.758482000092, 19567.879240999919,
            9783.9396204999593, 4891.9698102499797, 2445.9849051249898,
            1222.9924525624949, 611.49622628138, 305.748113140558,
            152.874056570411, 76.4370282850732, 38.2185141425366,
            19.1092570712683, 9.55462853563415, 4.7773142679493699,
            2.3886571339746849, 1.1943285668550503, 0.59716428355981721,
            0.29858214164761665};

    private final Point origin = new Point(-20037508.342787, 20037508.342787);

    private void init() {
        try {
            getServiceExecutor().submit(new Runnable() {
                public void run() {
                    GaoDeLayer.this.initLayer();
                }
            });
        } catch (RejectedExecutionException rejectedexecutionexception) {
            Log.e("GaoDe Map Layer", "initialization of the layer failed.",
                    rejectedexecutionexception);
        }
    }

    @Override
    protected byte[] getTile(int level, int col, int row) throws Exception {
        int maxLevel = 17;
        int minLevel = 0;
        if (level > maxLevel || level < minLevel) {
            return new byte[0];
        }

        byte[] tiles = new byte[0];
        //先从离线地图中读取
        switch (GaoDeLayerType) {

            case GaoDeLayerTypes.IMAGE_GAODE_MAP:
                tiles = TileFactory.readGaoDeOffLineMapTiles(offLineMapPath,level, col, row,1);
                break;
            case GaoDeLayerTypes.IMAGE_GAODE_MARKING_MAP:
                tiles = TileFactory.readGaoDeOffLineMapTiles(offLineMapPath,level, col, row,2);
                break;
        }
        if (RegexUtils.isNotNull(tiles)) {
            return tiles;
        }
        //再从缓存中读取
        tiles = TileFactory.readTiles(cachePath, level, col, row);
        if (RegexUtils.isNotNull(tiles)) {
            return tiles;
        }

        String s = "Galileo".substring(0, ((3 * col + row) % 8));
        String url = "";

        switch (GaoDeLayerType) {

            case GaoDeLayerTypes.IMAGE_GAODE_MAP:
                url = "https://wprd01.is.autonavi.com/appmaptile?x="+col+"&y="+row+"&z="+level+"&lang=zh_cn&size="+s+"&scl=1"+"&style=6";
                break;
            case GaoDeLayerTypes.IMAGE_GAODE_MARKING_MAP:
                url = "https://wprd01.is.autonavi.com/appmaptile?x="+col+"&y="+row+"&z="+level+"&lang=zh_cn&size="+s+"&scl=1"+"&style=8";
                break;
        }

        Map<String, String> map = null;
        tiles = com.esri.core.internal.io.handler.a.a(url, map);

        TileFactory.writeTiles(whetheCachedMap, cachePath, tiles, level, col,
                row);

        return tiles;
    }

    @Override
    protected void initLayer() {
        int dpi = 96;
        int tileWidth = 256;
        int tileHeight = 256;
        if (getID() == 0L) {
            nativeHandle = create();
            changeStatus(OnStatusChangedListener.STATUS
                    .fromInt(-1000));
        } else {
            this.setDefaultSpatialReference(SpatialReference.create(102113));
            this.setFullExtent(new Envelope(-22041257.773878,
                    -32673939.6727517, 22041257.773878, 20851350.0432886));

            this.setTileInfo(new TileInfo(origin, scales, resolutions,
                    scales.length, dpi, tileWidth, tileHeight));
            super.initLayer();

        }
    }
}

地图展示后勾画地块

勾画的地块获取各个Point点(List<Point>)

拼接思路 获取最大最小xy点 组成四边形 遍历拼接url

if (null != project && !project.isEmpty()) {
                Map<Integer, int[]> levelToBounds = new HashMap<>();
                for (int level = 0; level <= 18; level++) {
                    int minX = Integer.MAX_VALUE;
                    int minY = Integer.MAX_VALUE;
                    int maxX = Integer.MIN_VALUE;
                    int maxY = Integer.MIN_VALUE;

                    // 遍历所有点,计算每个层级的边界
                    for (Point projectedPoint : project) {
                        int[] tileCoordinates = getTileNumber(projectedPoint.getX(), projectedPoint.getY(), level);
                        minX = Math.min(minX, tileCoordinates[0]);
                        minY = Math.min(minY, tileCoordinates[1]);
                        maxX = Math.max(maxX, tileCoordinates[0]);
                        maxY = Math.max(maxY, tileCoordinates[1]);
                    }
                    // 存储层级与边界信息
                    levelToBounds.put(level, new int[]{minX, minY, maxX, maxY});
                }
                List<String> urlList = new ArrayList<>();
                for (Map.Entry<Integer, int[]> entry : levelToBounds.entrySet()) {
                    int level = entry.getKey();
                    int[] bounds = entry.getValue();
                    for (int x = bounds[0]; x <= bounds[2]; x++) {
                        for (int y = bounds[1]; y <= bounds[3]; y++) {
                            // 拼接瓦片请求的URL
                            String tileUrl = "https://wprd01.is.autonavi.com/appmaptile?x=" + x + "&y=" + y + "&z=" + level + "&lang=zh_cn&size=Galil&scl=1&style=6";
                            urlList.add(tileUrl);
                        }
                    }
                }
            }
public static int[] getTileNumber(double longitude, double latitude, int zoomLevel) {
        double longitudeRad = longitude * Math.PI / 180;
        double latitudeRad = latitude * Math.PI / 180;

        double y = Math.log(Math.tan((Math.PI / 4) + (latitudeRad / 2)));

        int tileX = (int) Math.floor((longitudeRad + Math.PI) / (2 * Math.PI) * Math.pow(2, zoomLevel));
        int tileY = (int) Math.floor((1 - (y / Math.PI)) / 2 * Math.pow(2, zoomLevel));

        return new int[]{tileX, tileY};
    }

获取到url就可以去下载了

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Leaflet是一个轻量级的开源JavaScript库,用于创建移动友好的交互式地图。而ArcGIS则是一个强大的地理信息系统(GIS)平台,提供各种类型的地图和数据。要将ArcGIS瓦片地图加载到Leaflet中,你可以按照以下步骤进行操作: 1. 安装依赖:在项目中安装Leaflet和ArcGIS的JavaScript库。你可以使用npm或yarn等包管理器来安装所需的依赖项。 2. 创建地图容器:使用Leaflet创建一个地图容器,并设置其id或其他自定义属性以便于后续引用。 3. 加载瓦片图层:使用Leaflet的TileLayer类加载ArcGIS瓦片图层。你需要提供ArcGIS服务器的主机名、瓦片图层的名称和瓦片图层的URL。 ```javascript var tileLayer = L.tileLayer('http://<ArcGIS服务器主机名>/arcgis/rest/services/<瓦片图层名称>/MapServer/tile/{z}/{y}/{x}', { attribution: '© <ArcGIS版权信息>' }); ``` 确保将`<ArcGIS服务器主机名>`替换为实际的ArcGIS服务器主机名,并将`<瓦片图层名称>`替换为实际的瓦片图层名称。 4. 将图层添加到地图:将加载瓦片图层添加到地图容器中。 ```javascript map.addLayer(TileLayer); ``` 5. 配置地图选项:根据需要配置地图选项,如缩放级别、坐标系等。 6. 显示地图:将配置好的地图显示在页面上。 这样,你就可以在Leaflet中加载和使用ArcGIS瓦片地图了。请确保你已正确配置ArcGIS服务器和相关权限,以便能够访问所需的瓦片图层。此外,你可能还需要根据需要调整代码以适应特定的项目要求和样式。 请注意,此过程假设你已经了解了基本的Leaflet和JavaScript知识,并且已经正确安装和配置了所需的依赖项。如果你不熟悉这些技术,建议参考相关的文档和教程以获得更深入的了解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值