IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android 加载天地图 -> 正文阅读

[移动开发]Android 加载天地图

Android? 通过ArcGis 来加载天地图,研究了一个星期 整理成了一个小demo,附上部分源码,底部附上源码demo,

package com.luao.arcgisdemo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.admin.SystemUpdateInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.layers.WebTiledLayer;
import com.esri.arcgisruntime.location.LocationDataSource;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luao.arcgisdemo.entity.SysUnitInfo;
import com.luao.arcgisdemo.gisutils.ArcGisUtil;
import com.luao.arcgisdemo.gisutils.PermissionUtils;
import com.luao.arcgisdemo.gisutils.SecondActivity;
import com.luao.arcgisdemo.gisutils.TianDiMapUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity  implements PermissionUtils.PermissionCallbacks{
    private MapView mMapView;


    private ArcGISMap map;

    private WebTiledLayer imageBaseMap;
    private WebTiledLayer imageBaseMapAnno;

    private WebTiledLayer vectorBaseMap;
    private WebTiledLayer vectorBaseMapAnno;

    private WebTiledLayer terrainBaseMap;
    private WebTiledLayer terrainBaseMapAnno;

    private Point center;
    /**
     * 坐标系
     */
    final SpatialReference wgs84 = SpatialReference.create(4326);
    /**
     * 加载的图层
     */
    private ArcGISTiledLayer tiledLayerBaseMap;
    /**
     * 图层切换按钮
     */
    private TextView layerChangeBtn;

    /**
     * 当前图层
     */
    private int currentLayer;
    /**
     * 泵站信息list
     */
    private List<Graphic> graphicListPump;
    /**
     * 水厂图层list
     */
    private List<Graphic> graphicListWaterStation;
    /**
     * 设备列表
     */
    private List<SysUnitInfo> sysUnitInfos;
    /**
     * 泵站图层
     */
    private GraphicsOverlay graphicsOverlayPump;
    /**
     * 水清图层
     */
    private GraphicsOverlay graphicsOverlayWaterStation;
    /**
     * 功能根据
     */
    private ArcGisUtil arcGisUtil;
    /**
     * 点击弹窗
     */
    private TextView calloutContent;

    private LocationDisplay mLocationDisplay;
    @RequiresApi(api = Build.VERSION_CODES.M)
    private static final int REQUEST_PERMISSION_CODE = 0;
    private String[] permissions = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    };

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = findViewById(R.id.gis_map);
        layerChangeBtn = findViewById(R.id.layerChangeBtn);


        arcGisUtil = new ArcGisUtil(this, mMapView);

        final SpatialReference SRID_MERCATOR = SpatialReference.create(102100);
        map = new ArcGISMap(SRID_MERCATOR);
        center = new Point(106.4360552458, 38.2487400748, SpatialReference.create(4326));

        imageBaseMap = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_IMAGEBASEMAP);
        imageBaseMapAnno = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_IMAGEBASEMAP_ANNOTATION);

        terrainBaseMap = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_TERRAINBASEMAP);
        terrainBaseMapAnno = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_TERRAINBASEMAP_ANNOTATION);

        vectorBaseMap = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_VECTORBASEMAP);
        vectorBaseMapAnno = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_VECTORBASEMAP_ANNOTATION);

        final List<WebTiledLayer> imageBaseMapLayerList = new ArrayList<>();
        imageBaseMapLayerList.add(imageBaseMap);
        imageBaseMapLayerList.add(imageBaseMapAnno);
        final List<WebTiledLayer> terrainBaseMapLayerList = new ArrayList<>();
        terrainBaseMapLayerList.add(terrainBaseMap);
        terrainBaseMapLayerList.add(terrainBaseMapAnno);
        final List<WebTiledLayer> vectorBaseMapLayerList = new ArrayList<>();
        vectorBaseMapLayerList.add(vectorBaseMap);
        vectorBaseMapLayerList.add(vectorBaseMapAnno);

        map.setMaxScale(TianDiMapUtils.getSCALES()[TianDiMapUtils.getSCALES().length - 1]);

        mMapView.setMap(map);

        map.getOperationalLayers().addAll(imageBaseMapLayerList);

        imageBaseMap.loadAsync();
        imageBaseMapAnno.loadAsync();

        terrainBaseMap.loadAsync();
        terrainBaseMapAnno.loadAsync();

        vectorBaseMap.loadAsync();
        vectorBaseMapAnno.loadAsync();


        if (PermissionUtils.hasPermissions(this, permissions)) {
            startLocation();
        } else {
            PermissionUtils.requestPermissions(this, REQUEST_PERMISSION_CODE, permissions);
        }

        layerChangeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                map.getOperationalLayers().clear();
                currentLayer++;
                switch (currentLayer % 3) {
                    case 0:
                        map.getOperationalLayers().addAll(imageBaseMapLayerList);
                        break;
                    case 1:
                        map.getOperationalLayers().addAll(terrainBaseMapLayerList);
                        break;
                    case 2:
                        map.getOperationalLayers().addAll(vectorBaseMapLayerList);
                        break;
                    default:
                        map.getOperationalLayers().addAll(imageBaseMapLayerList);
                        break;
                }

            }
        });

        calloutContent = new TextView(this);
        calloutContent.setTextColor(Color.BLACK);
        calloutContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

        arcGisUtil.setOnClickMapListener(new ArcGisUtil.OnClickMapListener() {
            @Override
            public void onClickMap(Graphic graphicsFromLayer, Point point) {
                Object name = graphicsFromLayer.getAttributes().get("name");
                if (name != null) {
                    calloutContent.setText(name.toString());
                    arcGisUtil.showPop(calloutContent, point);
                }
            }
        });
        loadUnitMarkerData();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        PermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsAllGranted(int requestCode, List<String> perms, boolean isAllGranted) {
        if (isAllGranted) {
            startLocation();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
    }

    private void startLocation() {

        mLocationDisplay = mMapView.getLocationDisplay();
        mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.NAVIGATION);
        //当我们执行LocationDisplay.startAsync()方法时候,会在地图上显示出我们当前位置
        mLocationDisplay.startAsync();


        //获取的点是基于当前地图坐标系的点
        Point point = mLocationDisplay.getMapLocation();
        Log.d("xyh1", "point: " + point.toString());
        //获取基于GPS的位置信息
        LocationDataSource.Location location = mLocationDisplay.getLocation();
        //基于WGS84的经纬度坐标。
        Point point1 = location.getPosition();
        if (point1 != null) {
            Log.d("xyh2", "point1: " + point1.toString());
        }


//        //如果要在LocationDisplay里进行位置信息的自动监听,方法也很简单,只需要LocationDisplay.addLocationChangedListener即可
//        mLocationDisplay.addLocationChangedListener(new LocationDisplay.LocationChangedListener() {
//            @Override
//            public void onLocationChanged(LocationDisplay.LocationChangedEvent locationChangedEvent) {
//                LocationDataSource.Location location = locationChangedEvent.getLocation();
//                Log.d("xyh", "onLocationChanged: " + location.getPosition().toString());
//            }
//        });
    }


    public static String getOriginalFundData(Context context) {
        InputStream input = null;
        try {
            input = context.getAssets().open("unit.json");
            String json = convertStreamToString(input);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String convertStreamToString(java.io.InputStream is) {
        String s = null;
        try {
            Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A");
            if (scanner.hasNext()) {
                s = scanner.next();
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }


    /**
     * 加载地图marker元素数据
     */
    private void loadUnitMarkerData() {
        String unitDataJson = getOriginalFundData(this);
        List<SysUnitInfo> sysUnitInfoList = new Gson().fromJson(unitDataJson, new TypeToken<List<SysUnitInfo>>() {
        }.getType());

        //添加点
        graphicsOverlayPump = arcGisUtil.addGraphicsOverlay();
        graphicsOverlayWaterStation = arcGisUtil.addGraphicsOverlay();
        PictureMarkerSymbol iconPump = new PictureMarkerSymbol(new BitmapDrawable(changeSize(R.drawable.gis_ic_bengzhan)));
        PictureMarkerSymbol iconWaterStation = new PictureMarkerSymbol(new BitmapDrawable(changeSize(R.drawable.gis_ic_shuichang)));

        graphicListPump = new ArrayList<>();
        graphicListWaterStation = new ArrayList<>();

        for (SysUnitInfo sysUnitInfo : sysUnitInfoList) {
            Point point = new Point(sysUnitInfo.getLgtd(), sysUnitInfo.getLttd(), wgs84);
            HashMap<String, Object> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("name", sysUnitInfo.getName());
            if (sysUnitInfo.getKey1().equals("1")) {
                graphicListPump.add(new Graphic(point, stringStringHashMap, iconPump));
            } else if (sysUnitInfo.getKey1().equals("5")) {
                graphicListWaterStation.add(new Graphic(point, stringStringHashMap, iconWaterStation));
            }
        }
        graphicsOverlayPump.getGraphics().addAll(graphicListPump);
        graphicsOverlayWaterStation.getGraphics().addAll(graphicListWaterStation);

    }

    /**
     * 移动地图中心
     *
     * @param point 中心点
     */
    private void moveMapCenter(Point point) {
        mMapView.setViewpointCenterAsync(point, TianDiMapUtils.getSCALES()[11]);
    }


    /**
     * 图片资源大小不契合,需要缩放
     *
     * @param drawable 图片资源
     * @return 新的图片资源
     */
    private Bitmap changeSize(@DrawableRes int drawable) {
        Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), drawable);
        int originalWidth = originalBitmap.getWidth();
        int originalHeight = originalBitmap.getHeight();
        int newWidth = 30;
        int newHeight = 70; // 自定义 高度 暂时没用

        float scale = ((float) newHeight) / originalHeight;
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        return Bitmap.createBitmap(originalBitmap, 0, 0,
                originalWidth, originalHeight, matrix, true);//你的 ImageView
    }
}

demo下载地址:Android通过Arcgis加载天地图-Android文档类资源-CSDN下载通过Arcgis加载天地图,并且定位到当前,同时可以生成maker,并添加点击事件,,如果地图加载更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/t747515714/48945799

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-11-26 08:57:51  更:2021-11-26 08:59:11 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 5:46:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码