Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用MapBox将多个GeoJSON源添加到同一层_Java_Android_Mapbox_Geojson_Mapbox Android - Fatal编程技术网

Java 使用MapBox将多个GeoJSON源添加到同一层

Java 使用MapBox将多个GeoJSON源添加到同一层,java,android,mapbox,geojson,mapbox-android,Java,Android,Mapbox,Geojson,Mapbox Android,我正试图用多个GeoJSON文件中的数据填充地图。在下面的代码中,我创建了方法GeoJSONToMap,调用该方法时,会向地图添加标记 GeoJSONToMap: public void GeoJSONToMap(@NonNull Style loadedMapStyle, String asset_id) { GeoJsonSource source = null; try { source = new GeoJsonSource("geojson-sou

我正试图用多个GeoJSON文件中的数据填充地图。在下面的代码中,我创建了方法
GeoJSONToMap
,调用该方法时,会向地图添加标记

GeoJSONToMap

 public void GeoJSONToMap(@NonNull Style loadedMapStyle, String asset_id) {

    GeoJsonSource source = null;

    try {
        source = new GeoJsonSource("geojson-source", new URI(asset_id));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    loadedMapStyle.addSource(source);

    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
    loadedMapStyle.addImage("marker", icon);

    SymbolLayer symbolLayer = new SymbolLayer("layer-id", "geojson-source");
    symbolLayer.setProperties(iconImage("marker"));

    loadedMapStyle.addLayer(symbolLayer);


}
成功地将GeoJSON数据从one文件添加到地图:

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

    mapboxMap.setStyle(Style.MAPBOX_STREETS,
            new Style.OnStyleLoaded() {
                @Override
                public void onStyleLoaded(@NonNull Style style) {
                    GeoJSONToMap(style, "asset://the_GeoJSON_file");

当我尝试使用多个
GeoJSON文件填充地图时,应用程序不会打开。我假设这个错误与图层创建有关,但是我没有找到答案

GeoJSONToMap(style, "asset://the_GeoJSON_file");
GeoJSONToMap(style, "asset://another_GeoJSON_file");

每次都使用相同的图层id,因为它是在
GeoJSONToMap
方法中硬编码的。每个层都需要有一个唯一的层id

每次都使用相同的源id,因为它是在
GeoJSONToMap
方法中硬编码的。每个源都需要有一个唯一的源id

编辑:在下面添加代码。可以用不同的方式清理它,但是下面的代码按照您想要的方式工作。我调整并使用了应用程序的
assets
文件夹中已有的GeoJSON文件

请参见GIF中的最终结果。您将看到黄色的pin图标来自一个geojson数据源,红色的pin来自另一个数据源

package com.mapbox.mapboxandroiddemo.examples.basics;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import java.net.URI;
import java.net.URISyntaxException;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import static com.mapbox.mapboxsdk.style.layers.Property.ICON_ANCHOR_BOTTOM;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAnchor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;


/**
 * The most basic example of adding a map to an activity.
 */
public class SimpleMapViewActivity extends AppCompatActivity {

  private MapView mapView;
  private MapboxMap mapboxMap;

  @Override
  protected 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, getString(R.string.access_token));

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

    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
      @Override
      public void onMapReady(@NonNull MapboxMap mapboxMap) {
        mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
          @Override
          public void onStyleLoaded(@NonNull Style style) {
            SimpleMapViewActivity.this.mapboxMap = mapboxMap;

            // Map is set up and the style has loaded. Now you can add data or make other map adjustments.

            GeoJSONToMap("source-id1", "first-layer-id", "asset://spinning_icon.geojson");

            GeoJSONToMap("source-id2", "second-layer-id", "asset://la_heatmap_styling_points.geojson");

          }
        });
      }
    });
  }

  public void GeoJSONToMap(String sourceId, String layerId, String asset_id) {
    mapboxMap.getStyle(new Style.OnStyleLoaded() {
      @Override
      public void onStyleLoaded(@NonNull Style style) {

        try {
          GeoJsonSource source = new GeoJsonSource(sourceId, new URI(asset_id));

          style.addSource(source);

          Bitmap icon;

          if (layerId.equals("first-layer-id")) {
            icon = BitmapFactory.decodeResource(getResources(), R.drawable.red_marker);
          } else {
            icon = BitmapFactory.decodeResource(getResources(), R.drawable.yellow_marker);
          }

          style.addImage(layerId + " marker", icon);

          SymbolLayer symbolLayer = new SymbolLayer(layerId, sourceId);

          symbolLayer.setProperties(
              iconImage(layerId + " marker"),
              iconAllowOverlap(true),
              iconAnchor(ICON_ANCHOR_BOTTOM), // You should use this if you're using a pin-like icon image
              iconIgnorePlacement(true)
          );

          style.addLayer(symbolLayer);

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

  // Add the mapView lifecycle to the activity's lifecycle methods
  @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 onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
  }

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

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

谢谢请你举个例子,你太棒了!非常感谢你,你说得非常清楚。希望你有一个愉快的一天。:五彩纸屑:很高兴你发现它很有用:)