gpt4 book ai didi

android - 在 android 谷歌地图中显示大的 geojson 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:08:14 24 4
gpt4 key购买 nike

我正在尝试向 Google map 显示一个 geojson 图层。代码如下。 geojson 文件本地存储在我的原始文件夹中。该文件有 286 个特征(大约 15MB)。因此,读取此文件并显示它会消耗更多时间。最初,我遇到了内存不足错误,通过在 list 文件中将 large heap 设置为 true 来消除该错误。我怎样才能快速加载这个文件(目前,它需要一分钟或更长时间)?我想知道是否有其他有效的方法可以做到这一点。在此之后,我还将有其他任务,如获取功能和显示一些属性。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myanmar, 5.25f));
new MyAsyncTask().execute();
}

public class MyAsyncTask extends AsyncTask <Void, Void, Void> {

ProgressDialog pd;
GeoJsonLayer layer;

@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MapsActivity.this);
pd.setMessage("Loading Data");
pd.show();
}

@Override
protected Void doInBackground(Void... voids) {
try {
layer = new GeoJsonLayer(mMap, R.raw.myanmar, getApplicationContext());
} catch (Exception e) {
Log.d("Error is : ", e.toString());
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pd.cancel();
layer.addLayerToMap();
}
}

我的geojson文件的一小部分如下:

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "ID_3": 193, "NAME_3": "Chaung-U" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 95.348327636718807, 21.878610610961914 ], [ 95.297210693359432, 21.860000610351676 ], [ 95.286926269531307, 21.853612899780387 ], [ 95.276092529296989, 21.850000381469727 ], [ 95.265823364257926, 21.84832954406744 ], [ 95.257217407226676, 21.846940994262695 ] ] ] ] } },
{ "type": "Feature", "properties": { "ID_3": 199, "NAME_3": "Myaung" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 95.376281738281193, 21.708541870117301 ], [ 95.375259399414119, 21.703050613403434 ], [ 95.370529174804801, 21.681390762329102 ], [ 95.367752075195313, 21.664720535278434 ], [ 95.366699218750114, 21.658369064331055 ], [ 95.362762451171875, 21.649999618530273 ] ] ] ] } }
]}

我尝试将文件分成多个小文件单元并并行运行单独的异步任务,每个任务处理一个单独的文件:

for (int i = 1; i < 3; i++) {
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,i);
}

异步任务:

public class MyAsyncTask extends AsyncTask <Integer, Void, GeoJsonLayer> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected GeoJsonLayer doInBackground(Integer... integers) {
int i = integers[0];
try {
GeoJsonLayer layer = null;
switch (i) {
case 1:
layer = new GeoJsonLayer(mMap, R.raw.small_1,
getApplicationContext());
break;
case 2:
layer = new GeoJsonLayer(mMap, R.raw.small_2,
getApplicationContext());
break;
//other cases
}
for (GeoJsonFeature feature : layer.getFeatures()) {
geoJsonFeatures.put(Integer.parseInt(feature.getProperty("ID_3")), feature);
}
return layer;
} catch (Exception e) {
return null;
}
}

@Override
protected void onPostExecute(GeoJsonLayer layer) {
super.onPostExecute(layer);
layer.addLayerToMap();
}
}

这是推荐的吗?这样一来,时间比之前有所减少,但考虑到用户体验因素,还是不够快。

最佳答案

最后,通过减小 geojson 文件的大小解决了这个问题。二手http://mapshaper.org/ ,一种在线工具,可为您提供缩小尺寸但保持形状的选项。用它来获取简化文件(近300KB)和bang,加载在几秒钟内完成。希望这对面临同样问题的人有所帮助。

关于android - 在 android 谷歌地图中显示大的 geojson 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42266565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com