【问题标题】:android google maps marker based on string parameter基于字符串参数的android谷歌地图标记
【发布时间】:2017-02-25 11:12:24
【问题描述】:

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

    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 google-maps-android-api-2


    【解决方案1】:

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

    getIdentifier

    从 android Resources 中通过名称获取图标的 id。

    类似这样的:

    getResources().getIdentifier("icon_name", "drawable", getPackageName());
    

    例如,如果您从活动中调用它。

    getResources() documentation

    更新:

    你试过了吗?

    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)) 但不起作用 .icon 行说它不知道 drawableResourceId,我在 mMap.addMarker 行上方定义。还是谢谢
    • 是的,它有效。谢谢。任何想法我怎样才能得到它,如果用户有 hdpi 屏幕或 xhdpi 屏幕然后根据那个选择图标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多