如何在Blackberry中实现此代码

时间:2011-12-07 09:37:54

标签: android blackberry geolocation migration

我使用下面的代码来获取使用android的位置坐标。现在我想在Blackberry中实现相同的功能。但我发现Blackberry中没有很多课程。任何人都可以为我提供本程序中使用的类的替代方案。我在Blackberry中使用了Criteria类来获取坐标。但是它适用于仅在版本4.7.1之上的移动设备Here。所以我发现这个代码有助于android,我想在blackBerry中实现这个。谢谢

public class GeoLocationActivity extends Activity {
    GsmCellLocation location;
    int cellID, lac;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TelephonyManager tm  = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
    location = (GsmCellLocation) tm.getCellLocation();

    Button btnDisplayMap = (Button) findViewById(R.id.displayMap);
    btnDisplayMap.setOnClickListener(new OnClickListener()
    {
        public void onClick(View arg0)
        {
            //---obtain the CellID and LAC of the device---
            CellLocation.requestLocationUpdate();        
            cellID = location.getCid();
            lac = location.getLac();

            try {
                if (!displayMap(cellID, lac))
                {
                    TextView t = (TextView) findViewById(R.id.status);
                    t.setText("Location not found!");
                }

            } catch (Exception e) {                    
                e.printStackTrace();
            } 
        }
    });
}

private boolean displayMap(int cellID, int lac) throws Exception 
{
    String urlString = "http://www.google.com/glm/mmap";            

    //---open a connection to Google Maps API---
    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();
    HttpURLConnection httpConn = (HttpURLConnection) conn;        
    httpConn.setRequestMethod("POST");
    httpConn.setDoOutput(true); 
    httpConn.setDoInput(true);
    httpConn.connect(); 

    //---write some custom data to Google Maps API---
    OutputStream outputStream = httpConn.getOutputStream();
    WriteData(outputStream, cellID, lac);       

    //---get the response---
    InputStream inputStream = httpConn.getInputStream();  
    DataInputStream dataInputStream = new DataInputStream(inputStream);

    //---interpret the response obtained---
    dataInputStream.readShort();
    dataInputStream.readByte();
    int code = dataInputStream.readInt();
    if (code == 0) {
        double lat = (double) dataInputStream.readInt() / 1000000D;
        double lng = (double) dataInputStream.readInt() / 1000000D;
        dataInputStream.readInt();
        dataInputStream.readInt();
        dataInputStream.readUTF();

        EditText et= (EditText)findViewById(R.id.lat);
        et.setText(lat+"");
        EditText et2= (EditText)findViewById(R.id.lon);
        et2.setText(lng+"");
        return true;
    }
    else
    {           
        return false;
    }
}

private void WriteData(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();       
        }

}

1 个答案:

答案 0 :(得分:0)

为了在blackberry中实现此代码:

public void getGPS() {
    int cellID = GPRSInfo.getCellInfo().getCellId();
    int lac = GPRSInfo.getCellInfo().getLAC();
    String urlString = "http://www.google.com/glm/mmap";
    //---open a connection to Google Maps API---
    HttpConnection httpConn = (HttpConnection) Connector.open(urlString);
    httpConn.setRequestMethod("POST");

    //---write some custom data to Google Maps API---
    OutputStream outputStream = httpConn.openOutputStream();
    writeData(outputStream, cellID, lac);

    //---get the response---
    InputStream inputStream = httpConn.openInputStream();
    DataInputStream dataInputStream = new DataInputStream(inputStream);

    //---interpret the response obtained---
    dataInputStream.readShort();
    dataInputStream.readByte();
    int code = dataInputStream.readInt();
    if (code == 0)
    {
        double lat = (double) dataInputStream.readInt() / 1000000D;
        double lng = (double) dataInputStream.readInt() / 1000000D;
        dataInputStream.readInt();
        dataInputStream.readInt();
        dataInputStream.readUTF();
        lattitude = String.valueOf(lat);
        longitude = String.valueOf(lng);
    }
}

private void writeData(OutputStream out, int cellID, int lac) throws IOException {
    DataOutputStream dataOutputStream = new DataOutputStream(out);
    dataOutputStream.writeShort(21);
    dataOutputStream.writeLong(0);
    dataOutputStream.writeUTF("es");
    dataOutputStream.writeUTF("BlacbBerry");
    dataOutputStream.writeUTF("1.3.1");
    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();
}