我想在不使用GPS的情况下获取Blackberry设备的位置。所以,我正在使用Opencellid来获得结果。但它给了我错误的位置结果。 任何人都可以澄清什么是确切的程序以及我可能做错了什么?
我的代码:
public final static String getQueryString(){
cellID = Integer.toString(GPRSInfo.getCellInfo().getCellId());
// Retrieves the Location Area Code.
lac = Integer.toString(GPRSInfo.getCellInfo().getLAC());
// Retrieves the mobile country code.
mcc = Integer.toHexString(RadioInfo.getMCC(RadioInfo.getCurrentNetworkIndex()));
// Retrieves the Location network Code.
mnc = Integer.toHexString(RadioInfo.getMNC(RadioInfo.getCurrentNetworkIndex()));
queryStr="http://www.opencellid.org/cell/get?
key="+myapikey+"&mcc="+mcc+"&mnc="+mnc+"&cellid="+cellID+"&lac="+lac+"&fmt=txt";
return queryStr;
}
public void httpGetRequest(){
HttpConnection conn = null;
InputStream in = null; StringBuffer buff = new StringBuffer(); String result = "";
try {
conn=(HttpConnection) Connector.open(getQueryString()+getString(),Connector.READ);
conn.setRequestMethod(HttpConnection.GET); conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
conn.setRequestProperty("Content-Type", "plain/text");
in = conn.openInputStream();
int car;
while( (car=in.read())!= -1){
buff.append((char)car);
}
in.close();
conn.close();
result=buff.toString();
//get latitude and longitude
if(result.startsWith("err")){
System.out.println("Cell not found!");
}else{
int pos=result.indexOf(',');
String lat=result.substring(0,pos);
int pos2=result.indexOf(',',pos+1);
String lon=result.substring(pos+1,pos2);
System.out.println(lat+" "+lon);
getLocationFromGoogleMaps(lat,lon);
}
} catch (Exception ex) {
System.out.println("====Exception: "+ex.getMessage());
} finally {
try {
if (in != null) {
in.close();
}
conn.close();
} catch (IOException e) {
System.out.println("====Exception: "+e.getMessage());
}
}
}
答案 0 :(得分:0)
试试这个 -
try {
int cellID = GPRSInfo.getCellInfo().getCellId();
int lac = GPRSInfo.getCellInfo().getLAC();
String urlString2 = "http://www.google.com/glm/mmap";
// Open a connection to Google Maps API
ConnectionFactory connFact = new ConnectionFactory(); ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(urlString2);
HttpConnection httpConn2;
httpConn2 = (HttpConnection)connDesc.getConnection();
httpConn2.setRequestMethod("POST");
// Write some custom data to Google Maps API
OutputStream outputStream2 = httpConn2.openOutputStream();//getOutputStream();
WriteDataGoogleMaps(outputStream2, cellID, lac);
// Get the response
InputStream inputStream2 = httpConn2.openInputStream();//getInputStream();
DataInputStream dataInputStream2 = new DataInputStream(inputStream2);
// Interpret the response obtained
dataInputStream2.readShort();
dataInputStream2.readByte();
int code = dataInputStream2.readInt();
//Dialog.alert(code+"");
if (code == 0) {
latitude= dataInputStream2.readInt() / 1000000D;
longitude=dataInputStream2.readInt() / 1000000D;
//Dialog.alert(latitude+"-----"+longitude);
dataInputStream2.readInt();
dataInputStream2.readInt();
dataInputStream2.readUTF();
} else {
System.out.println("Error obtaining Cell Id ");
}
outputStream2.close();
inputStream2.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
WriteDataGoogleMaps功能如下 -
private static void WriteDataGoogleMaps(OutputStream out, int cellID, int lac)
throws IOException {
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeShort(21);
dataOutputStream.writeLong(0);
dataOutputStream.writeUTF("en");
dataOutputStream.writeUTF("Android");
dataOutputStream.writeUTF("1.0");
dataOutputStream.writeUTF("Web");
dataOutputStream.writeByte(27);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(3);
dataOutputStream.writeUTF("");
dataOutputStream.writeInt(cellID);
dataOutputStream.writeInt(lac);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.flush();
}