基于字符串参数的android谷歌地图标记

基于字符串参数的android谷歌地图标记,android,google-maps-android-api-2,Android,Google Maps Android Api 2,我需要一些关于android上谷歌地图的帮助。我想解决的问题是如何从drawable文件夹中选择一个标记图标,并根据通过ajax调用接收的参数在google maps上绘制它。这是我试过的 String img_name = jsonObj.getString("icon_name")+"_"+jsonObj.getString("icon_state") == "0" ? "on" : "off"; // img_name will get a value thing like

我需要一些关于android上谷歌地图的帮助。我想解决的问题是如何从drawable文件夹中选择一个标记图标,并根据通过ajax调用接收的参数在google maps上绘制它。这是我试过的

    String img_name = jsonObj.getString("icon_name")+"_"+jsonObj.getString("icon_state") == "0" ? "on" : "off";
    // img_name will get a value thing like 'cat_on', I have the icon in drawable folder

    // how do I use this img_name in the .icon line below


    mMap.addMarker(new MarkerOptions()
        .title(jsonObj.getString("header"))
        .snippet(jsonObj.getString("title").concat(jsonObj.getString("description")))
        .position(new LatLng(
            Double.parseDouble(jsonObj.getString("lat")),
            Double.parseDouble(jsonObj.getString("lng"))
        ))
        .icon(BitmapDescriptorFactory.fromResource(R.id.resourceId))
    );

我不知道我是否完全理解你的问题,但你可以用

从android资源中获取图标的id(按名称)

大概是这样的:

getResources().getIdentifier("icon_name", "drawable", getPackageName());
例如,如果您从活动中调用它

更新:

你试过这个吗

String img_name = jsonObj.getString("icon_name")+"_"+jsonObj.getString("icon_state") == "0" ? "on" : "off";
    // img_name will get a value thing like 'cat_on', I have the icon in drawable folder

    // how do I use this img_name in the .icon line below
int resourceId = getResources().getIdentifier(img_name, "drawable", getPackageName());

    mMap.addMarker(new MarkerOptions()
        .title(jsonObj.getString("header"))
        .snippet(jsonObj.getString("title").concat(jsonObj.getString("description")))
        .position(new LatLng(
            Double.parseDouble(jsonObj.getString("lat")),
            Double.parseDouble(jsonObj.getString("lng"))
        ))
        .icon(BitmapDescriptorFactory.fromResource(resourceId))
    );

一个新线程正在调用函数来绘制标记,我尝试了这个int-drawableResourceId=this.getResources().getIdentifier(img_name,“drawable”,this.getPackageName());和.icon(BitmapDescriptorFactory.fromResource(R.id.drawableResourceId))但不起作用。图标行表示它不知道drawableResourceId,我在mMap.addMarker行的正上方定义了它。不管怎样,谢谢你,它管用。谢谢任何想法,我如何才能得到它,如果用户有hdpi屏幕或xhdpi屏幕,然后选择图标的基础上,不介意在这里找到它