我正在尝试实现一种机制,在MapboxMap上标记geojson文件中描述的一组峰值。这些标记应该在用户点击它们时显示额外的信息,显示峰值的名称,海拔,纬度和经度。
我很难实现这个机制,因为几乎所有的在线示例都是在Mapbox SDK v9中创建的,而我使用的是SDK v10 -这两者之间的变化很大。SDK v10只有Kotlin的文档,而我用Java编写的事实也没有帮助。
我在网上找到了一些这样的实现示例,但我无法运行应用程序。我遇到的问题是我无法读取geojson文件,也无法将带有geojson数据的图层添加到Map中。对于下面的方法,我在'Style'中获得了“cannot resolve method“.”
styl.addSource(geoJsonSource); styl.addLayer(symbolLayer);
如果有人能给我指出正确的方向,我将不胜感激。Mapbox文档对我来说在这个主题上不清楚。
下面是我的尝试:
public class MapyActivity extends 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();
}
}
字符串
1条答案
按热度按时间pxq42qpu1#
替换:
字符串
使用:
型