我试图在地图上放置大约 40 个标记,这些标记散布在大片区域。我找到了这样做的代码,但它聚集了附近的标记,但我希望它显示所有标记,无论缩放级别如何。
这些是我尝试过的代码。
Mapbox github :- 它工作正常,但它使附近的标记集群。
此代码对我不起作用,因为 android 无法解析方法
featureCollection.getFeatures()
、、、和f.getGeometry()
mapView.addMarker
coordinates.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; } }
PS如果我可以在没有任何库的情况下做到这一点,那就太好了。