Ishan hrx
Ishan hrx

Reputation: 423

how to put multiple markers on MapBox in android via geoJson?

I am trying to put about 40 markers on the map scattered across the large area. I found the code to do so but it clusters the nearby markers, but I want it to show all the markers as they are regardless of zoom levels.

These are the codes I tried.

  1. Mapbox github :- It works fine but it makes clusters of nearby markers.

  2. This code doesn't work for me as android can't resolve methods featureCollection.getFeatures(),f.getGeometry(),mapView.addMarker,coordinates.getLatitude() and 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 it would be great if I can do it without any libraries.

Upvotes: 0

Views: 1877

Answers (1)

bradm6s
bradm6s

Reputation: 102

You are not using the linelayer that you create, it renders geojson directly when you call source.setGeoJson(featureCollection).

To draw markers directly onto the map you need to call the addmarker method on the map instance, not the map view instance. In your case mapboxMap.addMarker

As for unresolved methods, you are likely not importing the correct packages: import com.mapbox.geojson.Feature; import com.mapbox.geojson.FeatureCollection;

Upvotes: 1

Related Questions