我正在尝试实现一种机制,其中在 Mapbox 地图上标记 geojson 文件中描述的一组峰值。一旦用户点击这些标记,它们应该显示额外的信息,显示山峰的名称、海拔、纬度和经度。
我很难实现这种机制,因为几乎所有在线可用的示例都是在 Mapbox SDK v9 中制作的,而我正在使用 SDK v10 - 并且这两者之间的变化很大。事实上 SDK v10 只有 Kotlin 文档,而我用 Java 编写,这也无济于事。
我将在网上找到的一些此类实现的示例结合在一起,但我无法运行该应用程序。我遇到的问题是我无法读取 geojson 文件,并且无法将包含 geojson 数据的图层添加到地图中。对于下面的方法,我在“样式”中收到“无法解析方法“...”
style.addSource(geoJsonSource); style.addLayer(symbolLayer);
如果有人能指出我正确的方向,我将不胜感激。 Mapbox 文档对我来说并不清楚这个主题。
这是我的尝试:
公共类 MapyActivity 扩展 AppCompatActivity {
private MapView mapView;
FloatingActionButton floatingActionButton;
private MapboxMap mapboxMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapy);
mapView = findViewById(R.id.mapView);
floatingActionButton = findViewById(R.id.focusLocation);
floatingActionButton.hide();
//adding map annotations
if (ActivityCompat.checkSelfPermission(MapyActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
activityResultLauncher.launch((Manifest.permission.ACCESS_FINE_LOCATION));
}
mapView.getMapboxMap().loadStyleUri("mapbox://styles/hikegreg/clormq18j00q201qm3jbaewmr", new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
addMarkersFromGeoJson(style);
mapView.getMapboxMap().setCamera(new CameraOptions.Builder().zoom(5.0).build());
LocationComponentPlugin locationComponentPlugin = getLocationComponent(mapView);
locationComponentPlugin.setEnabled(true);
LocationPuck2D locationPuck2D = new LocationPuck2D();
locationPuck2D.setBearingImage(AppCompatResources.getDrawable(MapyActivity.this, R.drawable.baseline_location_on_24));
locationComponentPlugin.setLocationPuck(locationPuck2D);
locationComponentPlugin.addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener);
locationComponentPlugin.addOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener);
getGestures(mapView).addOnMoveListener(onMoveListener);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationComponentPlugin.addOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener);
locationComponentPlugin.addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener);
getGestures(mapView).addOnMoveListener(onMoveListener);
floatingActionButton.hide();
}
});
}
});
}
private void addMarkersFromGeoJson(Style style) {
try {
//load geojson first
InputStream inputStream = getAssets().open("tatry_wierzcholki.geojson");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String geoJson = new String(buffer, "UTF-8");
GeoJsonSource source = null;
//make geojson source with geojson data
GeoJsonSource geoJsonSource = new GeoJsonSource.Builder("geojson-source")
.data(geoJson)
.build();
style.addSource(geoJsonSource);
//symbol layer creation
SymbolLayer symbolLayer = new SymbolLayer("symbol-layer", "geojson-source")
.iconImage("marker-icon")
.iconAllowOverlap(true);
style.addLayer(symbolLayer);
//add marker click listener
GesturesPlugin gesturesPlugin = getGestures(mapView);
gesturesPlugin.addOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapClick(@NonNull Point point) {
ScreenCoordinate screenCoordinate = mapView.getMapboxMap().pixelForCoordinate(point);
List<QueriedFeature> features = mapView.getMapboxMap().queryRenderedFeatures(screenCoordinate, "symbol-layer");
if(!features.isEmpty()){
QueriedFeature feature = features.get(0);
Feature geoJsonFeature = feature.getFeature();
String name = geoJsonFeature.getStringProperty("name");
Double elevation = (Double) geoJsonFeature.getNumberProperty("elevation");
Point featurePoint = (Point) geoJsonFeature.geometry();
double latitude = featurePoint.latitude();
double longitude = featurePoint.longitude();
showMarkerDetails(name, elevation, latitude, longitude);
}
return true;
}
private void showMarkerDetails(String name, Double elevation, double latitude, double longitude){
String message = "Name: " + name + "\n" +
"Elevation: " + elevation + "m\n" +
"Latitude: " + latitude + "\n" +
"Longitude: " + longitude;
new AlertDialog.Builder(this)
.setTitle("Marker Details")
.setMessage(message)
.setPositiveButton("Close",null)
.show();
}
});
//handle IOExceptions
} catch (IOException e) {
e.printStackTrace();
//handle potential json parsing errros
} catch (JSONException e){
e.printStackTrace();
//handle any other unexpected errors
} catch (Exception e) {
e.printStackTrace();
}
}
更换:
style.addSource(geoJsonSource);
style.addLayer(symbolLayer);
与:
style.addSource(geoJsonSource);
style.addLayerAbove(symbolLayer, "com.mapbox.mapboxsdk.annotations.points");