首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在mapbox中加载MultiLineString geojson

无法在mapbox中加载MultiLineString geojson
EN

Stack Overflow用户
提问于 2018-01-12 12:15:15
回答 3查看 753关注 0票数 0

我正在使用mapbox的教程,我可以让geojson文件显示得很好。但是当我尝试使用MultiLine geojson时,我一直得到这个异常,它没有显示在我的地图上。

代码语言:javascript
复制
 Exception Loading GeoJSON: org.json.JSONException: Value [-84.38482011299999,44.24712923700008,0] at 1 of type org.json.JSONArray cannot be converted to double

我试着改变了这句话:

代码语言:javascript
复制
  if (!TextUtils.isEmpty(type) && type.equalsIgnoreCase("LineString")) {

我将"LineString“改为"MultiLineString”,但仍然得到了相同的例外。我的代码中的其他内容和现在的教程是一样的。

我目前使用osmdroid,在那里加载geojson没有问题,所以我不认为这是我的文件的问题。

试图切换到mapbox。

代码语言:javascript
复制
 public class MainActivity extends Activity implements OnMapReadyCallback
 {
     private static final String TAG = "DrawGeojsonLineActivity";

private MapView mapView;
private MapboxMap mapboxMap;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Mapbox access token is configured here. This needs to be called either in your application
    // object or in the same activity which contains the mapview.
    Mapbox.getInstance(this, "pk.eyJ1IjoiamViMTkyMDA0IiwiYSI6ImNpbWNyODZyaDAwMmZ1MWx2dHdzcHQ5M2EifQ.IZsMnB3wOYFIaX1A5sy7Mw");

    // This contains the MapView in XML and needs to be called after the access token is configured.
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
}

@Override
public void onMapReady(MapboxMap mapboxMap) {
    this.mapboxMap = mapboxMap;

    // Load and Draw the GeoJSON
    new DrawGeoJson().execute();
}

@Override
public void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
protected void onStart() {
    super.onStart();
    mapView.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    mapView.onStop();
}

@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

private class DrawGeoJson extends AsyncTask<Void, Void, List<LatLng>> {
    @Override
    protected List<LatLng> doInBackground(Void... voids) {

        List<LatLng> points = new ArrayList<>();

        try {
            // Load GeoJSON file
            InputStream inputStream = getAssets().open("st_helen_trail.geojson");
            BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            StringBuilder sb = new StringBuilder();
            int cp;
            while ((cp = rd.read()) != -1) {
                sb.append((char) cp);
            }

            inputStream.close();

            // Parse JSON
            JSONObject json = new JSONObject(sb.toString());
            JSONArray features = json.getJSONArray("features");
            JSONObject feature = features.getJSONObject(0);
            JSONObject geometry = feature.getJSONObject("geometry");
            if (geometry != null) {
                String type = geometry.getString("type");

                // Our GeoJSON only has one feature: a line string
                if (!TextUtils.isEmpty(type) && type.equalsIgnoreCase("MultiLineString")) {

                    // Get the Coordinates
                    JSONArray coords = geometry.getJSONArray("coordinates");
                    for (int lc = 0; lc < coords.length(); lc++) {
                        JSONArray coord = coords.getJSONArray(lc);
                        LatLng latLng = new LatLng(coord.getDouble(1), coord.getDouble(0));
                        points.add(latLng);
                    }
                }
            }
        } catch (Exception exception) {
            Log.e(TAG, "Exception Loading GeoJSON: " + exception.toString());
        }

        return points;
    }

    @Override
    protected void onPostExecute(List<LatLng> points) {
        super.onPostExecute(points);

        if (points.size() > 0) {

            // Draw polyline on map
            mapboxMap.addPolyline(new PolylineOptions()
                                  .addAll(points)
                                  .color(Color.parseColor("#3bb2d0"))
                                  .width(2));
        }
    }
}

}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-16 16:08:09

我大概一周前就想出来了。这就是我要做的

我把这个去掉了:

代码语言:javascript
复制
 new DrawGeoJson().execute();

这是:

代码语言:javascript
复制
 private class DrawGeoJson extends AsyncTask<Void, Void, List<LatLng>> { @Override protected List<LatLng> doInBackground(Void... voids) { List<LatLng> points = new ArrayList<>(); try { // Load GeoJSON file InputStream inputStream = getAssets().open("st_helen_trail.geojson"); BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } inputStream.close(); // Parse JSON JSONObject json = new JSONObject(sb.toString()); JSONArray features = json.getJSONArray("features"); JSONObject feature = features.getJSONObject(0); JSONObject geometry = feature.getJSONObject("geometry"); if (geometry != null) { String type = geometry.getString("type"); // Our GeoJSON only has one feature: a line string if (!TextUtils.isEmpty(type) && type.equalsIgnoreCase("MultiLineString")) { // Get the Coordinates JSONArray coords = geometry.getJSONArray("coordinates"); for (int lc = 0; lc < coords.length(); lc++) { JSONArray coord = coords.getJSONArray(lc); LatLng latLng = new LatLng(coord.getDouble(1), coord.getDouble(0)); points.add(latLng); } } } } catch (Exception exception) { Log.e(TAG, "Exception Loading GeoJSON: " + exception.toString()); } return points; } @Override protected void onPostExecute(List<LatLng> points) { super.onPostExecute(points); if (points.size() > 0) { // Draw polyline on map mapboxMap.addPolyline(new PolylineOptions() .addAll(points) .color(Color.parseColor("#3bb2d0")) .width(2)); } }

然后在"onMapReady“中添加下面的代码

代码语言:javascript
复制
 final AssetManager assetManager = getAssets();
                try {

                    String[] imgPath = assetManager.list("mi_atv");
                    for (int i = 0; i< imgPath.length; i++) {
                        InputStream is1 = assetManager.open("mi_atv/" + imgPath[i]);
                        Log.d(TAG, imgPath[i]);
                        int size = is1.available();
                        byte[] buffer = new byte[size];
                        is1.read(buffer);
                        is1.close();

                        String json1 = new String(buffer, "UTF-8");

                        GeoJsonSource source = new GeoJsonSource(imgPath[i], json1);
                        map.addSource(source);

                        atv = new LineLayer(imgPath[i], imgPath[i]);
                        atv.setProperties(
                            lineColor(Color.parseColor("#7fff00")),

                            lineWidth(1.0f)
                        );
                    map.addLayer(main.atv);
                    }
                }
                 catch (Exception exception) {
                    Log.e(TAG, "Exception Loading GeoJSON: " + exception.toString());
                }
票数 0
EN

Stack Overflow用户

发布于 2018-01-12 12:51:54

onPostExecute()方法中添加此代码。

代码语言:javascript
复制
@Override
    protected void onPostExecute(List<LatLng> points) {
        super.onPostExecute(points);

        if (points.size() > 0) {
      LatLng[] dataOfArray = points.toArray(new LatLng[points.size()]);
            // Draw polyline on map
            mapboxMap.addPolyline(new PolylineOptions()
                                  .addAll(dataOfArray)
                                  .color(Color.parseColor("#3bb2d0"))
                                  .width(2));
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2018-04-04 09:34:46

问题是,在多行字符串的情况下,您需要获得一个额外的数组。

多个例子:

坐标:[ 7.38588715,44.17675579,7.385923192,44.176754868,.

LINESTRING示例:

坐标:[ 7.38588715,44.17675579,7.385923192,44.176754868,.

因此:

代码语言:javascript
复制
//replace linestring with multilinestring:
if (!TextUtils.isEmpty(type) && type.equalsIgnoreCase("MultiLineString")) {
    // get extra array adding .getJSONArray(0)
    JSONArray coords = geometry.getJSONArray("coordinates").getJSONArray(0);
    for (int lc = 0; lc < coords.length(); lc++) {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48226113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档