相同的实现方式,放一个程序中正常读取加载,另一个程序读出来的全是空数据
异常的程序:
MapLayerManager包用来读文件
public String COMPath;
File loadfile = null;
Context context;
ShpMapListener shpMapListener;
//获取文件路径
public String getFilePath(String FileName){
if (COMPath == null) {
//COMPath = context.getFilesDir().getPath();
COMPath = Environment.getExternalStorageDirectory().getPath()+"/map";
}
return COMPath+"/"+FileName;
}
//加载文件
public void LoadFile(String filename){
String u_filetype[] = filename.split("\\.");
String filetype = u_filetype[u_filetype.length - 1];
switch (filetype){
case "shp": loadShpFile(filename); break;
case "tif": loadTifFile(filename); break;
}
}
//加载tif文件
private void loadTifFile(String filename) {
}
//加载shp文件
private void loadShpFile(String filename) {
String filepath = getFilePath(filename);
File file = new File(filepath);
if (file.exists()) {
ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(filepath);
shapefileFeatureTable.loadAsync();
shapefileFeatureTable.addDoneLoadingListener(new Runnable() {
@Override
public void run() {
Log.d("DZR", "开始加载地图");
shpMapListener.onGetFeatureTable(shapefileFeatureTable);
Log.d("DZR", "加载完成");
}
});
}
}
//加载shp文件的回调函数
public interface ShpMapListener{
public void onGetFeatureTable(ShapefileFeatureTable ashapefileFeatureTable);
}
//设置shp回调
public void setShpMapListener(ShpMapListener shpMapListener){
this.shpMapListener = shpMapListener;
}
HomeFragment extends Fragment对上文的调用:
private FragmentHomeBinding binding;
private MapView mapView;
Button button1;
FeatureLayer featureLayer1;
public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
//不知道干啥的
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
//对象初始化
mapView = binding.HOMEMAP1;
imageButton = binding.HOMEIMGBUT1;
button = binding.HOMEBUT1;
button1 = binding.HOMEBUT2;
textView1 = binding.HOMETEXT1;
dzReadFile.initContext(getContext());
//获取GPS定位信息
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
textView1.setText("获取GPS位置");
if (getAdress()) textView1.setText("获取位置完成");else textView1.setText("位置获取失败");
//绘制基础地图
ArcGISMap arcGISMap;
if (getAdress()) {
arcGISMap = new ArcGISMap(Basemap.Type.OPEN_STREET_MAP, latitude, longitude, 16);
}else {
arcGISMap = new ArcGISMap(Basemap.Type.OPEN_STREET_MAP, latitude, longitude, 10);
}
mapView.setAttributionTextVisible(false);//去水印
mapView.setMap(arcGISMap);
//设置位置显示
mapView.getLocationDisplay().addDataSourceStatusChangedListener(new LocationDisplay.DataSourceStatusChangedListener() {
@Override
public void onStatusChanged(LocationDisplay.DataSourceStatusChangedEvent dataSourceStatusChangedEvent) {
if (dataSourceStatusChangedEvent.isStarted()) {
return;
}
if (dataSourceStatusChangedEvent.getError() == null) {
return;
}
}
});
mapView.getLocationDisplay().startAsync();
}
//按钮按下开始读取shp文件并加载
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dzReadFile.setShpMapListener(new ShpMapListener() {
@Override
public void onGetFeatureTable(ShapefileFeatureTable shapefileFeatureTable) {
featureLayer1 = new FeatureLayer(shapefileFeatureTable);
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.DKGRAY, 1.0f);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#00000000"), lineSymbol);
SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
featureLayer1.setRenderer(renderer);
binding.HOMEMAP1.getMap().getOperationalLayers().add(featureLayer1);
//mapView.setViewpointAsync(new Viewpoint(featureLayer1.getFullExtent()));
Log.d("HF","townbount.shp已加载");
}
});
dzReadFile.initContext(getContext());
dzReadFile.LoadFile("townbount.shp");
}
});
return root;
}
正常程序的代码:
路径获取
public class MapFilePathManager {
public static final String dirName = "/map/";
public static final String townbount = getShpFile("townbount.shp");
public static final String disasters = getShpFile("disasters.shp");
public static final String Gangu_bount = getShpFile("Gangu_bount.shp");
public static final String all2020 = getShpFile("all2020.tif");
public static String getShpFile(String fileName) {
return getMapFileDIRPath() + fileName;
}
public static String getMapFileDIRPath() {
String filePath = Environment.getExternalStorageDirectory().getPath() + dirName;
// String filePath =
return filePath;
}
}
文件读取
public class MapLayerManager {
//成功加载地图文件的回调
public interface MapLayerListener {
public void onGetFeatureTable(ShapefileFeatureTable ashapefileFeatureTable);
}
//成功加载Feature的回调
public interface QueryFeaturesListener {
public void onGetFeatureQueryResult(FeatureQueryResult featureQueryResult);
}
//异步加载图层中的Feature
public static void queryFeaturesAsync(FeatureLayer featureLayer ,QueryParameters query , QueryFeaturesListener queryFeaturesListener ) {
FeatureTable mTable = featureLayer.getFeatureTable();//得到查询属性表
final ListenableFuture<FeatureQueryResult> featureQueryResult
= mTable.queryFeaturesAsync(query);
featureQueryResult.addDoneListener(new Runnable() {
@Override
public void run() {
try {
FeatureQueryResult featureResul = featureQueryResult.get();
queryFeaturesListener.onGetFeatureQueryResult(featureResul);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
//异步加载本地shp文件
public static void loadShpFile(AssetManager assetManager, String fileName ,MapLayerListener mapLayerListener ){
File shpFile = new File(fileName);
Log.d("PP",fileName);
if (shpFile.exists()){
ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(fileName);
shapefileFeatureTable.loadAsync();
shapefileFeatureTable.addDoneLoadingListener(new Runnable() {
@Override
public void run() {
mapLayerListener.onGetFeatureTable(shapefileFeatureTable);
}
});
}else {
Log.d("11111","no file!!!!!");
}
}
//加载tif文件
public static RasterLayer loadTifFile(String filePath ) {
File shpFile = new File(filePath);
if (shpFile.exists()){// 判断是否存储这个文件
Raster raster = new Raster(filePath);
RasterLayer rasterLayer = new RasterLayer(raster);
List<Integer> colors = new ArrayList<>();
for (int i = 0; i <= 6; i++) {
if (i == 0) {
colors.add(i, Color.RED);
} else if (i == 1 ) {
colors.add(i, Color.BLUE);
}else if (i == 2) {
colors.add(i, Color.BLACK);
}else if (i == 3) {
colors.add(i, Color.GREEN);
}else if (i == 4) {
colors.add(i, Color.WHITE);
} else {
colors.add(i, Color.YELLOW);
}
}
ColormapRenderer colormapRenderer = new ColormapRenderer(colors);
rasterLayer.setRasterRenderer(colormapRenderer);
return rasterLayer;
}else {
Log.d("11111","no file!!!!!");
}
return null;
}
}
调用
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private MapView mapView;
FeatureLayer featureLayer;
private double latitude = 0.0;
private double longitude = 0.0;
private LocationManager locationManager;
private TextView info;
Button nextButton;
TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapaView);
nextButton = findViewById(R.id.runButton);
text1 = findViewById(R.id.ACT_MAIN_TEXT1);
//获取GPS定位信息
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
toggleGPS();
text1.setText("纬度:" + latitude + "\n" + "经度:" + longitude );
ArcGISMap map = new ArcGISMap(Basemap.Type.OPEN_STREET_MAP, latitude, longitude, 17);
mapView.setMap(map);
loadMapLayer();
}
public void loadMapLayer() {
loadShpFile();
loadGangu_bount();
}
public void loadShpFile(){
AssetManager assetManager = getAssets();
MapLayerManager.loadShpFile(assetManager, MapFilePathManager.townbount, new MapLayerManager.MapLayerListener() {
@Override
public void onGetFeatureTable(ShapefileFeatureTable ashapefileFeatureTable) {
featureLayer = new FeatureLayer(ashapefileFeatureTable);
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.DKGRAY, 1.0f);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#00000000"), lineSymbol);
SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
featureLayer.setRenderer(renderer);
mapView.getMap().getOperationalLayers().add(featureLayer);
mapView.setViewpointAsync(new Viewpoint(featureLayer.getFullExtent()));
Log.d("HF","townbount.shp已加载"+featureLayer.getFullExtent().toString());
}
});
}
public void loadGangu_bount() {
AssetManager assetManager = getAssets();
MapLayerManager.loadShpFile(assetManager, MapFilePathManager.Gangu_bount, new MapLayerManager.MapLayerListener() {
@Override
public void onGetFeatureTable(ShapefileFeatureTable ashapefileFeatureTable) {
FeatureLayer featureLayer = new FeatureLayer(ashapefileFeatureTable);
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 5);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#00000000"), lineSymbol);
SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
featureLayer.setRenderer(renderer);
mapView.getMap().getOperationalLayers().add(featureLayer);
}
});
}
}