使用 MapBox 将多个 GeoJSON 源添加到同一层
Using MapBox to add multiple GeoJSON sources to the same layer
我正在尝试使用来自 多个 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 数据从 一个 文件添加到地图:
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。
编辑:添加了下面的代码。它可以用不同的方式清理,但下面的代码以您想要的方式工作。我调整了 https://docs.mapbox.com/android/maps/examples/create-a-simple-map-view/ 并使用了应用程序 assets
文件夹中已有的 GeoJSON 文件。
在 https://imgur.com/a/kLgkfgE 查看最终结果 GIF。您会看到黄色图钉图标来自一个 geojson 数据源,红色图钉来自另一个来源。
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);
}
}
我正在尝试使用来自 多个 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 数据从 一个 文件添加到地图:
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。
编辑:添加了下面的代码。它可以用不同的方式清理,但下面的代码以您想要的方式工作。我调整了 https://docs.mapbox.com/android/maps/examples/create-a-simple-map-view/ 并使用了应用程序 assets
文件夹中已有的 GeoJSON 文件。
在 https://imgur.com/a/kLgkfgE 查看最终结果 GIF。您会看到黄色图钉图标来自一个 geojson 数据源,红色图钉来自另一个来源。
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);
}
}