Как использовать мой JSON массив для стиля с отсутствующим значком в mapbox - PullRequest
0 голосов
/ 04 апреля 2020

Я хочу использовать свой массив lat и long location с сервера и использовать стиль с отсутствующим значком в функции Mapbox, пожалуйста, помогите мне. Большое спасибо

public class TestMap extends AppCompatActivity {

private static final String ICON_SOURCE_ID = "ICON_SOURCE_ID";
private static String ICON_LAYER_ID = "ICON_LAYER_ID";
private static double LATITUDE;
private static double LONGITUDE ;
private static final String PROFILE_NAME = "PROFILE_NAME";
private static final String CARLOS = "Carlos";
private static final String ANTONY = "Antony";
private static final String MARIA = "Maria";
private static final String LUCIANA = "Luciana";
private MapView mapView;
private MapboxMap mapboxMap;
private RequestQueue mQueue;
private TextView mTextViewResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, "my token");
setContentView(R.layout.activity_test_map);

//    mTextViewResult = findViewById(R.id.text_view_result);

//    Button buttonParse = findViewById(R.id.button_parse);

mQueue = Volley.newRequestQueue(this);



mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
  @Override
  public void onMapReady(@NonNull final MapboxMap mapboxMap) {

// Add Features which represent the location of each profile photo SymbolLayer icon
    Feature carlosFeature = Feature.fromGeometry(Point.fromLngLat(51.3239335,
      35.7464124));
    carlosFeature.addStringProperty(PROFILE_NAME, CARLOS);

    Feature antonyFeature = Feature.fromGeometry(Point.fromLngLat(51.3215886,
      35.7474219));
    antonyFeature.addStringProperty(PROFILE_NAME, ANTONY);

    Feature mariaFeature = Feature.fromGeometry(Point.fromLngLat(51.3215924,
      35.7474052));
    mariaFeature.addStringProperty(PROFILE_NAME, MARIA);

    Feature lucianaFeature = Feature.fromGeometry(Point.fromLngLat(51.3217689,
      35.7477305));
    lucianaFeature.addStringProperty(PROFILE_NAME, LUCIANA);

  // Use a URL to build and add a Style object to the map. Then add a source to the Style.
    mapboxMap.setStyle(
      new Style.Builder().fromUri(Style.LIGHT)
        .withSource(new GeoJsonSource(ICON_SOURCE_ID,
          FeatureCollection.fromFeatures(new Feature[] {
            carlosFeature,
            antonyFeature,
            mariaFeature,
            lucianaFeature}))),
      new Style.OnStyleLoaded() {
        @Override
        public void onStyleLoaded(@NonNull Style style) {
          TestMap.this.mapboxMap = mapboxMap;

   // Add a SymbolLayer to the style. iconImage is set to a value that will
   // be used later in the addOnStyleImageMissingListener below
          style.addLayer(new SymbolLayer(ICON_LAYER_ID, ICON_SOURCE_ID).withProperties(
            iconImage(get(PROFILE_NAME)),
            iconIgnorePlacement(true),
            iconAllowOverlap(true),
            textField(get(PROFILE_NAME)),
            textIgnorePlacement(true),
            textAllowOverlap(true),
            textOffset(new Float[] {0f, 2f})
          ));
        }
      });
  }
});

// Use the listener to match the id with the appropriate person. The correct profile photo is
// given to the map during "runtime".
mapView.addOnStyleImageMissingListener(new MapView.OnStyleImageMissingListener() {
  @Override
  public void onStyleImageMissing(@NonNull String id) {
    switch (id) {
      case CARLOS:
        addImage(id, R.drawable.ic_add_location_black_24dp);
        break;
      case ANTONY:
        addImage(id, R.drawable.ic_remove_red_eye_black_24dp);
        break;
      case MARIA:
        addImage(id, R.drawable.ic_add_location_black_24dp);
        break;
      case LUCIANA:
        addImage(id, R.drawable.ic_remove_red_eye_black_24dp);
        break;
      default:
        addImage(id, R.drawable.ic_remove_red_eye_black_24dp);
        break;
    }
  }
});

}

my Json arry:

private void jsonParse() {

String url = "my url";

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null,
  new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {

      try {
        JSONArray jsonArray = response.getJSONArray("ucovid");
        //Log.i("console.logs", String.valueOf(jsonArray));

        for (int i = 0; i < jsonArray.length(); i++) {
          JSONObject employee = jsonArray.getJSONObject(i);
          //Log.i("console.logs", String.valueOf(employee));

          int a = employee.getInt("id");
          LATITUDE = employee.getDouble("latitude");
          LONGITUDE = employee.getDouble("longitude");
          mTextViewResult.append(LATITUDE + ", " + LONGITUDE + "\n\n");

        }
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
  }, new Response.ErrorListener() {
  @Override
  public void onErrorResponse(VolleyError error) {
    //Log.i("console.logs", String.valueOf(error));

    error.printStackTrace();
  }
});

mQueue.add(request);
}

Как использовать массив LATITUDE & LONGITUDE вместо: // Добавить компоненты, которые представляет местоположение каждой фотографии профиля значок SymbolLayer Feature carlosFeature = Feature.fromGeometry (Point.fromLngLat (51.3239335, 35.7464124)); carlosFeature.addStringProperty (PROFILE_NAME, CARLOS);

...