Suj*_*jal 6 android google-maps geojson
我正在尝试向Google Map显示geojson图层.代码如下.geojson文件存储在我的原始文件夹中.该文件有286个功能(约15MB).因此,读取此文件并显示它会消耗更多时间.最初,我出现内存不足错误,通过在清单文件中将大堆设置为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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的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 ] ] ] ] } }
]}
Run Code Online (Sandbox Code Playgroud)
我尝试将文件分成多个小文件单元并并行运行单独的异步任务,每个处理一个单独的文件:
for (int i = 1; i < 3; i++) {
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,i);
}
Run Code Online (Sandbox Code Playgroud)
的AsyncTask:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是建议的吗?这样做,与早期相比,时间减少了,但考虑到用户体验因素,仍然没有快速.
最后,通过减小geojson文件的大小解决了该问题。使用http://mapshaper.org/,一个在线工具,它为您提供了减小尺寸但保留形状的选项。用它来获取简化文件(近300KB),嘭的一声,几秒内加载完成。希望这可以帮助面临同样问题的人。
| 归档时间: |
|
| 查看次数: |
2255 次 |
| 最近记录: |