如何在R.raw.myVar中使用变量

时间:2018-12-09 16:04:09

标签: android api google-maps

我想使用API​​ GoogleMaps为国家/地区着色。该代码有效,但我使用的是搜索栏,用户可以在其中输入其他国家/地区名称。

private void colorCountry(String searchString){
    String myVar = searchString + "_geojson";
    Toast.makeText(this, searchString , Toast.LENGTH_SHORT).show();
    try {
        GeoJsonLayer layer = new GeoJsonLayer(nMap, R.raw.france_geojson, getApplicationContext());
        GeoJsonPolygonStyle style = layer.getDefaultPolygonStyle();
        style.setFillColor(Color.BLUE);
        style.setStrokeColor(Color.BLUE);
        style.setStrokeWidth(1F);
        layer.addLayerToMap();
    } catch (IOException ex) {
        Log.e("IOException", ex.getLocalizedMessage());
    } catch (JSONException ex) {
        Log.e("JSONException", ex.getLocalizedMessage());
    }
}

如何在GeoJsonLayer中用变量“ myVar”替换“ france_geojson”?

GeoJsonLayer layer = new GeoJsonLayer(nMap, R.raw.france_geojson, getApplicationContext());

1 个答案:

答案 0 :(得分:0)

使用此:

int rawId = getApplicationContext()
            .getResources()
            .getIdentifier(myVar, "raw", getApplicationContext().getPackageName());

获取myVar的ID,然后:

GeoJsonLayer layer = new GeoJsonLayer(nMap, rawId, getApplicationContext());