0

我试图在地图上放置大约 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;
      }
    }
    

PS如果我可以在没有任何库的情况下做到这一点,那就太好了。

4

1 回答 1

1

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

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

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

于 2018-09-09T12:01:56.627 回答