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
|