如何通过geoJson在Android的MapBox上放置多个标记?

时间:2018-09-07 08:58:41

标签: android mapbox mapbox-android mapbox-marker

我正在尝试在散布在大面积区域的地图上放置约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;
      }
    }
    

P.S,如果我不用任何库就可以做到。

1 个答案:

答案 0 :(得分:1)

您没有使用创建的线层,而是在调用source.setGeoJson(featureCollection)时直接渲染geojson。

要直接在地图上绘制标记,您需要在地图实例(而不是地图视图实例)上调用addmarker方法。就您而言,mapboxMap.addMarker

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