为了账号安全,请及时绑定邮箱和手机立即绑定

将 cartoDB 浏览器中的图层添加到 Android 应用程序

将 cartoDB 浏览器中的图层添加到 Android 应用程序

慕婉清6462132 2021-12-30 16:13:35
我在 cartoDB 浏览器中有一个带有三层的地图,但我无法在 android 应用程序中的地图中添加图层如何使用 java 代码将 CartoDB 浏览器中的 aKMZ 图层(不是底图)添加到 Android 应用程序以显示应用程序中的所有图层?谁能帮我?💔添加底图的代码protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    // Register the license so that CARTO online services can be used    MapView.registerLicense(LICENSE,getApplicationContext());    // Get 'mapView' object from the application layout    mapView = (MapView) this.findViewById(R.id.mapView);    // Add basemap layer to mapView    CartoOnlineVectorTileLayer baseLayer = new CartoOnlineVectorTileLayer(CartoBaseMapStyle.CARTO_BASEMAP_STYLE_VOYAGER);    mapView.getLayers().add(baseLayer);}
查看完整描述

2 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

我的项目是室内导航系统,所以我需要 cartoDB 地图中的图层来显示 Android App 中的建筑级别


查看完整回答
反对 回复 2021-12-30
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

建议的解决方案是使用一些工具预先将 KMZ 转换为 GeoJSON。SDK 本身并没有这样做。在 SDK 中,您可以使用 GeoJSONGeometryReader 来解析 GeoJSON 并将其添加到最后映射为 VectorLayer。我的通用功能来完成这一切:


// usage: the file tpr-men.geojson is in assets folder of project

polygonGeoJSON(mapView,"tpr-men.geojson",android.graphics.Color.LTGRAY);


//helper function to read polygons from GeoJSON file, sets styles


private void polygonGeoJSON(MapView mapView, final String fileName, int color) {


    final Thread thread;// Initialize a local vector data source

    final LocalVectorDataSource source = new LocalVectorDataSource(baseProjection);


    // Initialize a vector layer with the previous data source

    VectorLayer vectorLayer = new VectorLayer(source);


    vectorLayer.setVectorElementEventListener(listener);


    // Add the clustered vector layer to the map

    mapView.getLayers().add(vectorLayer);




    LineStyleBuilder lsb = new LineStyleBuilder();

    lsb.setColor(new Color(color));

    lsb.setWidth(0.5f);

    lsb.setLineJoinType(LineJoinType.LINE_JOIN_TYPE_NONE);

    lsb.setLineEndType(LineEndType.LINE_END_TYPE_NONE);


    PolygonStyleBuilder polygonBuilder = new PolygonStyleBuilder();

    polygonBuilder.setColor(new Color(color & 0xbbffffff)); // a bit transparent

   //polygonBuilder.setLineStyle(lsb.buildStyle());

    final PolygonStyle style = polygonBuilder.buildStyle();


    // As the file to load is rather large, we don't want to block our main thread

    thread = new Thread(new Runnable() {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)

        @Override

        public void run() {



            try {


                // Read GeoJSON, parse it using SDK GeoJSON parser

                GeoJSONGeometryReader reader = new GeoJSONGeometryReader();


                // Set target projection to base (mercator)

                reader.setTargetProjection(baseProjection);

                Log.d("log","Starting load from .geojson");


                // Read features from local asset

                String json = loadJSONFromAsset(fileName);

                Log.d("log","Finished load from .geojson");


                FeatureCollection features = reader.readFeatureCollection(json);

                Log.d("log","Finished parsing of geojson, loaded objects: "+features.getFeatureCount());


                source.addFeatureCollection(features,style);


            } catch (IOException e) {

                e.printStackTrace();

            }


            Log.d("log","Finished load from geojson");


        }

    });


    thread.start();



}


// general helper function to read file to String

private String loadJSONFromAsset(String fileName) throws IOException {


    InputStream is = getAssets().open(fileName);

    int size = is.available();

    byte[] buffer = new byte[size];

    is.read(buffer);

    is.close();


    return new String(buffer, "UTF-8");


}



查看完整回答
反对 回复 2021-12-30
  • 2 回答
  • 0 关注
  • 155 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号