如何通过geoJson将多个标记放在android中的MapBox上?

问题描述 投票:0回答:1

我试图在大地区散布大约40个标记。我找到了这样做的代码,但是它会聚集附近的标记,但是我想让它显示所有标记,因为它们与缩放级别无关。

这些是我试过的代码。

  1. Mapbox github: - 它工作正常但它会产生附近标记的簇。
  2. 这段代码对我不起作用,因为android无法解析方法featureCollection.getFeatures()f.getGeometry()mapView.addMarkercoordinates.getLatitude()coordinates.getLongitude() public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { private MapView mapView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Mapbox.getInstance(this, getString(R.string.mapbox_access_token)); setContentView(R.layout.activity_main); mapView = findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); mapView.getMapAsync(this); } @Override public void onMapReady(MapboxMap mapboxMap) { this.mapboxMap = mapboxMap; try{ Uri uriMap = Uri.parse("https://dl.dropbox.com/s/9jln7v48lp3lb7e/data.geojson?dl=0"); String geoJsonString = getStringFromFile(uriMap,MainActivity.this); GeoJsonSource source = new GeoJsonSource("geojson", geoJsonString); mapboxMap.addSource(source); mapboxMap.addLayer(new LineLayer("geojson", "geojson")); FeatureCollection featureCollection = FeatureCollection.fromJson(geoJsonString); List<Feature> features = featureCollection.getFeatures(); for (Feature f : features) { if (f.getGeometry() instanceof Point) { Position coordinates = (Position) f.getGeometry().getCoordinates(); mapView.addMarker( new MarkerViewOptions().position(new LatLng(coordinates.getLatitude(), coordinates.getLongitude())) ); } } }catch(Exception e){ Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show(); } } private static String convertStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } public static String getStringFromFile(Uri fileUri, Context context) throws Exception { InputStream fin = context.getContentResolver().openInputStream(fileUri); String ret = convertStreamToString(fin); fin.close(); return ret; } }

如果我能在没有任何库的情况下完成它,那将是很棒的。

android mapbox mapbox-android mapbox-marker
1个回答
1
投票

您没有使用您创建的linelayer,它会在您调用source.setGeoJson(featureCollection)时直接呈现geojson。

要将标记直接绘制到地图上,您需要在地图实例上调用addmarker方法,而不是地图视图实例。在你的情况下mapboxMap.addMarker

对于未解决的方法,您可能无法导入正确的包:import com.mapbox.geojson.Feature; import com.mapbox.geojson.FeatureCollection;

© www.soinside.com 2019 - 2024. All rights reserved.