2011-04-08 175 views
1

嗨,我是黑莓應用程序開發的新手。我想獲得當前的gps位置細節。我已經成功地獲得了經緯度,但我不知道如何獲得當前的地址。任何人都可以給我一個樣本?請感謝提前。blackberry gps獲取當前位置地址?

回答

3

你在找什麼叫做「反向地理編碼」。 RIM在BB平臺上有一個example of exactly how to do this(好吧,無論如何要做到這一點)。

+0

我該如何檢查值?在可運行的線程內沒有字段允許像(labelfield,textfield)如何檢查addrinfo值?請回復我 – 2011-04-08 13:33:09

+0

這聽起來像你應該做一些調查在BB應用中使用線程。例如,您可能會看到Blackberry UI線程 - 非常基礎:http://www.thinkingblackberry.com/archives/182 – 2011-04-08 16:02:16

3

您可以使用Google地圖解決您的問題,如果您請求經度和緯度,Google地圖將根據您的選擇返回JSON(或XML)。

的網址是:http://maps.google.com/maps/geo?json&ll="+_lattitude+","+_longitude

這將返回JSON格式的給定的緯度和經度的所有細節,而且你必須解析由谷歌地圖返回的JSON。

您可以使用下面的代碼:

private void getLocationFromGoogleMaps() { 
    try { 
     StreamConnection s = null; 
     InputStream iStream = null;   
     s=(StreamConnection)javax.microedition.io.Connector.open("http://maps.google.com/maps/geo?json&ll="+_lattitude+","+_longitude+getConnectionStringForGoogleMap());//&deviceside=false&ConnectionType=mds-public" 

     HttpConnection con = (HttpConnection)s; 
     con.setRequestMethod(HttpConnection.GET); 
     con.setRequestProperty("Content-Type", "//text"); 
     int status = con.getResponseCode(); 
     if (status == HttpConnection.HTTP_OK) 
     { 
      iStream=s.openInputStream();      
      int len=(int) con.getLength(); 
      byte[] data = new byte[8000];      
      byte k; 
      String result=""; 
      while((k = (byte)iStream.read()) != -1) { 
       result = result+(char)k; 
      }     
      try { 
        JSONObject jsonObjectMapData=new JSONObject(result);              
        JSONArray jsonaryPlaceMark = jsonObjectMapData.getJSONArray("Placemark"); 
        JSONObject address= jsonaryPlaceMark.getJSONObject(0); 
       String placeName=address.getString("address"); 
       if(placeName!=null) 
        lblLoc.setText(address.getString("address")); 
       else 
        lblLoc.setText("Location information currently unavilable"); 
      } catch (Exception e) { 
       lblLoc.setText("location information Currently Unavilable"); 
      } 
     } 
    }catch (Exception e) { 
     System.out.println(e); 
     lblLoc.setText("location information Currently Unavilable"); 
    }   
} 

注:我使用的是FacebookBlackBerrySDK-0.3.5-SRC解析JSON或我們可以將XML ASLO。