2017-02-26 22 views
2

我有一個ListView,當我點擊單個項目時,它會轉到單個項目視圖。在每個單個項目查看頁面中都有一個位置按鈕。當點擊位置按鈕時,我需要進入谷歌地圖。android:geo使用intent的位置

長和緯度像這樣保存在數據庫中。 我想要動態地調用此列中的每個單元 ,數據被存儲這樣的,


的數據被存儲爲解析數據庫內的對象,而不是作爲字符串

{「lat」:25.1250241,「lng」:55.3752069}

任何人都知道請告訴我如何從中獲取數據?

  btn = (Button) findViewById(R.id.button5) ; 
      btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 


        btn = (Button) findViewById(R.id.button56) ; 
        Intent intent = new Intent(); 
        intent.setAction(Intent.ACTION_VIEW); 
        String data = String.format("geo:%s,%s", latitude, longitude); 

        intent.setData(Uri.parse(data)); 
        startActivity(intent); 
       } 
      }); 
+0

意味着你需要從中獲取數據的代碼 「{」 LAT 「:25.1250241,」lng「:55.3752069}」this String。 –

+0

是的,這是一個單元格內的數據@Chetan –

+1

看到我的回答在 –

回答

0

終於找到辦法..

這是我使用只得到座標

showCorporate = (Button) findViewById(R.id.individualEmail); 
     showCorporate.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String[] location = corporateLocation.split(","); 
       double lat, lng; 
       try{ 
        lat = fetchDouble(location[0], true); 
        lng = fetchDouble(location[1], false); 
        Log.d("Co-Ordinates",""+lat+", "+lng); 
        Intent intent = new Intent(); 
        intent.setAction(Intent.ACTION_VIEW); 
        String data = String.format("geo:%s,%s", lat, lng); 
        intent.setData(Uri.parse(data)); 
        startActivity(intent); 
       }catch (NullPointerException npe){ 
        Toast.makeText(SingleCorporate.this, "Sorry No Location Available!", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
1

使用下面的方法從String中解析數據,當我傳遞字符串時需要在下面的方法中傳遞一個paramaeter。

private void parseJsonData(){ 
     try { 
      String jsonString = "{\"lat\":25.1250241,\"lng\":55.3752069}"; 
      Object object = new JSONTokener(jsonString).nextValue(); 
      if (object instanceof JSONObject) { 
       JSONObject jsonObject = (JSONObject) object; 
       double lat = Double.parseDouble(jsonObject.optString("lat")); 
       double lng = Double.parseDouble(jsonObject.optString("lat")); 

       Log.i(getClass().getSimpleName(),"lat="+lat); 
       Log.i(getClass().getSimpleName(),"lng="+lng); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 
+0

以下嗨,謝謝你,但這不是靜態數據,數據結構就像上面那樣,但是數據庫是類似於你回答的電話號碼的動態數據庫昨天.. @Chetan –

+0

是的,你需要傳遞你的字符串來代替jsonString。 –

+0

哦謝謝..我可以使用這個代碼裏面的點擊事件@Chetan –