黑莓gps获取当前位置地址?

时间:2011-04-08 11:58:18

标签: blackberry

您好我是黑莓应用程序开发的新手。我想获得当前gps位置详细信息。我已经成功获得了经纬度但我不知道如何获取当前地址。任何人都可以给我一个样本吗?请感谢提前。

2 个答案:

答案 0 :(得分:3)

您正在寻找的是“反向地理编码”。 RIM在BB平台上有一个example of exactly how to do this(好吧,无论如何都要这样做)。

答案 1 :(得分:3)

您可以使用Google地图解决您的问题,如果您请求经纬度,Google地图将根据您的选择返回JSON(或XML)。

网址为:http://maps.google.com/maps/geo?json&ll="+_lattitude+","+_longitude

这将以JSON格式返回给定纬度和经度的所有细节,并且您必须解析Google地图返回的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。

相关问题