2017-02-25 27 views
0

我需要一些關於Android上的谷歌地圖的幫助。我想解決的問題是如何從可繪製文件夾中選擇一個標記圖標,並根據通過ajax調用收到的參數在Google地圖上繪製它。以下是我已經嘗試android谷歌地圖標記基於字符串參數

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)) 
    ); 

回答

1

我不知道我是否準確地理解你的問題,但你可以使用

getIdentifier

從Android的資源通過讓你的圖標的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)) 
    ); 
+0

一個新的線程調用函數畫出標記,我想這INT drawableResourceId = this.getResources()則getIdentifier(img_name, 「繪製」,this.getPackageName())。和.icon(BitmapDescriptorFactory.fromResource(R.id.drawableResourceId)),但不起作用.icon行說它不知道drawableResourceId,我已經定義在mMap.addMarker行之上。謝謝反正 – burgur

+0

我已經更新了我的答案 – krlos77

+0

謝謝,我現在正在嘗試。 – burgur