在android google maps中显示大型geojson文件 [英] Show large geojson file in android google maps

查看:425
本文介绍了在android google maps中显示大型geojson文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向Google地图显示一个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图层;

@Override
保护void onPreExecute(){
super.onPreExecute();
pd =新的ProgressDialog(MapsActivity.this);
pd.setMessage(加载数据);
pd.show();
}

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

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


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



$ p $ {
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.446940994262695]]]]]},
{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.664720535278 434],[95.366699218750114,21.658369064331055],[95.362762451171875,21.649999618530273]]]]}}
]}

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

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

AsyncTask:

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

@Override
保护无效onPreExecute(){
super.onPreExecute();
}

@Override
protected GeoJsonLayer doInBackground(Integer ...整数){
int i =整数[0];
尝试{
GeoJsonLayer layer = null;
switch(i){
case 1:
layer = new GeoJsonLayer(mMap,R.raw.small_1,
getApplicationContext());
休息;
案例2:
layer = new GeoJsonLayer(mMap,R.raw.small_2,
getApplicationContext());
休息; (GeoJsonFeature特性:layer.getFeatures()){
geoJsonFeatures.put(Integer.parseInt(feature.getProperty(ID_3))的
//其他情况
}
) ,功能);
}
返回图层;
} catch(Exception e){
return null;


$ b $ @覆盖
保护无效onPostExecute(GeoJsonLayer图层){
super.onPostExecute(图层);
layer.addLayerToMap();
}
}

这是否建议?这样做的时间与之前相比有所减少,但仍然没有考虑到用户体验的因素。

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


I am trying to show a geojson layer to Google Map. The code is as follows. The geojson file is stored locally in my raw folder. The file has 286 features (around 15MB). Hence, reading this file and displaying it is consuming more time. Initially, I was getting out of memory error, which is removed by setting large heap as true in the manifest file. How can I load this file quickly (currently, its taking a minute or more)? I am wondering if there is some other efficient method to do this. After this, I will also have other tasks of getting features and displaying some properties.

@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();
     }
}

A very small portion of my geojson file is as follows:

{
 "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 ] ] ] ] } }
]}

I try to divide the file into multiple small file units and run separate async tasks in parallel, each processing a separate file:

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

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();
    }
}

Is this suggested? Doing so, the time is reduced as compared to earlier, but still not yet fast considering the user experience factor.

解决方案

Finally, solved the issue by reducing the size of the geojson file. Used http://mapshaper.org/ , an online tool, that provides you with the options to reduce the size, yet preserving the shape. Used it to obtain simplified file (nearly 300KB) and bang, the loading completes within few seconds. Hope this helps someone facing the same issue.

这篇关于在android google maps中显示大型geojson文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆