Добавить слои из браузера cartoDB в приложение Android - PullRequest
0 голосов
/ 05 ноября 2018

У меня есть amap с тремя слоями в браузере cartoDB, и я не могу добавить слои на моей карте в приложении для Android. Как я могу добавить слои kMZ (не базовую карту) в браузере CartoDB для приложения Android, используя код Java, чтобы отобразить все слои в приложении ?? Может кто-нибудь мне помочь? ?

Код для добавления базовой карты

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 ]

0 голосов
/ 05 ноября 2018

Предлагаемое решение заключается в том, чтобы заранее преобразовать 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");

}
0 голосов
/ 05 ноября 2018

Мой проект - это внутренняя навигационная система, поэтому мне нужны слои на моей карте в cartoDB, чтобы показать уровень строительства в приложении для Android

...