chinthi
chinthi

Reputation: 89

how to get current latitude and longitude in android without gps

I want to get the longitude and latitude by using GsmCellLocation. I get the CID,LAC,MCC and MNC. But when trying to get longitude and latitude by using

urlString = "http://www.google.com/glm/mmap"

It gives me an openHttpConnection error. I have tried following many tutorials with the same results. Here is my code for getting long/lat which comes from another tutorial.

private Boolean RqsLocation(int cid, int lac) {
    Boolean result = false;
    String urlmmap = "http://www.google.com/glm/mmap";
    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();

            System.out.println("The lattitude value is  " + myLatitude);
            myLongitude = dataInputStream.readInt();
            System.out.println("The lattitude value is  " + myLongitude);
            result = true;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

I have given permissions in manifest file.

This question has asked before but I didn't got the answer from any of them.

Upvotes: 3

Views: 391

Answers (1)

Akhil Jayakumar
Akhil Jayakumar

Reputation: 2312

Without GPS then you can also use Network_Provider for get latitude and longitude.

LocationManager lm = (LocationManager) GlobalApplication
            .getAppContext().getSystemService(Context.LOCATION_SERVICE);

Location location = lm
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if(location != null){
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
}

Upvotes: 1

Related Questions