【发布时间】:2011-04-08 11:58:18
【问题描述】:
您好,我是黑莓应用程序开发的新手。我想获取当前 gps 位置详细信息。我已成功获取纬度和经度,但我不知道如何获取当前地址。谁能给我一个示例?谢谢提前。
【问题讨论】:
标签: blackberry
您好,我是黑莓应用程序开发的新手。我想获取当前 gps 位置详细信息。我已成功获取纬度和经度,但我不知道如何获取当前地址。谁能给我一个示例?谢谢提前。
【问题讨论】:
标签: blackberry
您要查找的内容称为“反向地理编码”。 RIM 在 BB 平台上有一个example of exactly how to do this(好吧,无论如何都是一种方法)。
【讨论】:
您可以使用谷歌地图来解决您的问题,如果您请求纬度和经度,谷歌地图将根据您的选择返回一个 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。
【讨论】: