Getting longitude and latitude through "www.google.com/glm/mmap"

2.1k views Asked by At

How to get longitude and latitude via LAC and CELLID?

1

There are 1 answers

0
sneha v On

Use below code to get lat and lng for using cid and lac.

    private Boolean findLocation(int cid, int lac) {
    Boolean result = false;

    String urlmmap = "http://www.google.com/glm/mmap";
    // Set-up proxy
    Properties systemProperties = System.getProperties();
    systemProperties.setProperty("http.proxyHost", urlmmap);
    //systemProperties.setProperty("http.proxyPort", String.valueOf(port));
    systemProperties.setProperty("http.keepAlive", "false");

    try {
        URL url = new URL(urlmmap);
        URLConnection conn = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        httpConn.connect();

        OutputStream outputStream = httpConn.getOutputStream();
        WriteData(outputStream, cid, lac);

        InputStream inputStream = httpConn.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);

        dataInputStream.readShort();
        dataInputStream.readByte();
        int code = dataInputStream.readInt();
        if (code == 0) {
            myLatitude = dataInputStream.readInt();
            myLongitude = dataInputStream.readInt();

            result = true;

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;

}

private void WriteData(OutputStream out, int cid, 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(cid);
    dataOutputStream.writeInt(lac);

    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.flush();
}

public class NetworkAsync extends AsyncTask<Void,Void,String>
{
    int cid, lac, dbm;
    ProgressDialog progressDialog;
    public NetworkAsync(int cid, int lac, int dbm)
    {
        this.cid = cid;
        this.lac = lac;
        this.dbm = dbm;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(DashboardActivity.this);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(Void... voids) {
        if(findLocation(cid, lac)){
            latlng=
                    String.valueOf((float)myLatitude/1000000)
                            + " : "
                            + String.valueOf((float)myLongitude/1000000);
            Log.d("latlong",latlng);
            latLngList.add(latlng+" : "+dbm);
        }else{
            latlng = "Can't find Location!";
            Log.d("latlong",latlng);
        }
        return latlng;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        progressDialog.dismiss();
    }
}

Use below code where you want to call this above methods, I mean in button click or in onResume() etc..

NetworkAsync networkAsync = new NetworkAsync(location.getCid(),location.getLac(),-81);
networkAsync.execute();