如何在Mapbox Maps Android的图层上分配颜色

时间:2019-02-18 10:39:03

标签: json gson layer mapbox-android

有没有一种方法可以将JSON对象(功能)传递到下面代码中的函数中,并在图层上分配颜色。

我试图通过制作JSON对象来解决它,实际上我 也使用了GSon API,但这些方法均无效。每当我尝试调试应用程序时,它都会向我显示所有键及其值。

这是键及其值;

{"type":"FeatureCollection","features":[
{"type":"Feature","id":"AFG","properties":

{"name":"Afghanistan"},"geometry":{"type":"Polygon","coordinates":`
[[[61.210817,35.650072],[62.230651,35.270664],[62.984662,35.404041],
[63.193538,35.857166],[63.982896,36.007957],[64.546479,36.312073],
[64.746105,37.111818],[65.588948,37.305217]}

private void addGeoJsonSourceToMap(@NonNull Style loadedMapStyle) {
            String geoJsonString = "";
            ArrayList<String> data = new ArrayList<>();

            try {
//            // Add GeoJsonSource to map
//            InputStream input = getAssets().open("countries_geo.json");

            InputStream inputStream = getResources().openRawResource(R.raw.countries_geo);
//
//            // int size = input.available();
//
//            //byte[] buffer = new byte[size];
//
//            input.read();
//            input.close();
//            String json = new String(buffer,"UTF-8");


            Scanner scanner = new Scanner(inputStream);

            StringBuilder builder = new StringBuilder();

            while (scanner.hasNextLine()) {
                builder.append(scanner.nextLine());
            }

            parseJSON(builder.toString());

GeoJsonSource source = new GeoJsonSource("geojson", (Geometry) Geojson.toJSON());
                loadedMapStyle.addSource(source);

}

 private void parseJSON(String toString) {

        Toast.makeText(this,"" + toString,Toast.LENGTH_SHORT).show();
        Log.i("The Information is: ","" + toString);

        StringBuilder builder = new StringBuilder();
        try {

            JSONObject root = new JSONObject(toString);
            JSONArray features = root.getJSONArray("features");

            Log.i("The country is: " , features.toString());




            for(int i = 0; i < features.length(); i++){

                JSONObject further_features = features.getJSONObject(i);

                // It will the result of the properities and its element's information....


                JSONObject properities = further_features.getJSONObject("properties");

                JSONObject geometry = further_features.getJSONObject("geometry");


                JSONArray coordinates = geometry.getJSONArray("coordinates");

                for(int j = 0; j< coordinates.length(); j++){
                    Log.i("TAG",coordinates.toString());
                }


                //JSONArray coordinates = geometry.getJSONArray("coordinates");


                Log.i("Properities: ", further_features.toString() );
                Log.i("Properities: ", properities.toString() );
                Log.i("geometry: ", geometry.toString() );
                Log.i("type: ", geometry.getString("type") );
               // Log.i("coordinates: ", coordinates.toString() );

                int y = 0;

            }

        }
        catch (JSONException e) {
            e.printStackTrace();
        }


    }

如果有人能帮助我,我将不胜感激。

0 个答案:

没有答案