0

I got the values of Local Area Code(LAC) and Cell ID(CID) in my application. But i couldn't find the exact location using these values. I don't know what i am missing. Someone help me in this

 m_manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    GsmCellLocation loc = (GsmCellLocation)m_manager.getCellLocation();
if (loc != null)
{
  // out.format("Location ");
Toast.makeText(getApplicationContext(), "Location",Toast.LENGTH_LONG).show();

   if (loc.getCid() == -1) {
  // out.format("cid: unknown ");
Toast.makeText(getApplicationContext(), "cid: unknown", Toast.LENGTH_LONG).show();
  } else {
  // out.format("cid: %08x ", loc.getCid());
int location=loc.getCid();
String str = Integer.toString(location);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
 }
if (loc.getLac() == -1) {
  // out.format("lac: unknown\n");
Toast.makeText(getApplicationContext(), "lac: unknown", Toast.LENGTH_LONG).show();
} else {
  // out.format("lac: %08x\n", loc.getLac());
int loca=loc.getLac();
String str1 = Integer.toString(loca);
Toast.makeText(getApplicationContext(),  str1, Toast.LENGTH_LONG).show();
}
Prakash
  • 33
  • 1
  • 6

3 Answers3

2

This one way to get location through LAC and CID:

        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

    int cid = cellLocation.getCid();
    int lac = cellLocation.getLac();
    textGsmCellLocation.setText(cellLocation.toString());
    textCID.setText("gsm cell id: " + String.valueOf(cid));
    textLAC.setText("gsm location area code: " + String.valueOf(lac));

    if(RqsLocation(cid, lac)){
     textGeo.setText(
          String.valueOf((float)myLatitude/1000000)
          + " : "
          + String.valueOf((float)myLongitude/1000000));
    latitude=String.valueOf((float)myLatitude/1000000);
     longitude=String.valueOf((float)myLongitude/1000000);
     lat_double=Double.parseDouble(latitude);
     lang_double=Double.parseDouble(longitude);
     geocoder = new Geocoder(AndroidTelephonyManager.this, Locale.ENGLISH);
        try {
            addresses = geocoder.getFromLocation(lat_double,  lang_double, 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        StringBuilder str = new StringBuilder();
        //if (geocoder.isPresent()) {
            // Toast.makeText(getApplicationContext(),
            // "geocoder present",
            // Toast.LENGTH_SHORT).show();
            Address returnAddress = addresses.get(0);

            String area = returnAddress.getFeatureName();
            String thfare = returnAddress.getThoroughfare();
            String localityString = returnAddress.getLocality();
            // String region_code = returnAddress.getCountryCode();
            String zipcode = returnAddress.getPostalCode();
            String state = returnAddress.getAdminArea();
            String sublocal = returnAddress.getSubLocality();
            String city = returnAddress.getCountryName();
     Toast.makeText(getApplicationContext(),  latitude, Toast.LENGTH_LONG).show();
     Toast.makeText(getApplicationContext(), longitude, Toast.LENGTH_LONG).show();
     Toast.makeText(getApplicationContext(), thfare, Toast.LENGTH_LONG).show();
     Toast.makeText(getApplicationContext(), area, Toast.LENGTH_LONG).show();
     Toast.makeText(getApplicationContext(), localityString, Toast.LENGTH_LONG).show();
     Toast.makeText(getApplicationContext(), zipcode, Toast.LENGTH_LONG).show();
     Toast.makeText(getApplicationContext(), sublocal, Toast.LENGTH_LONG).show();
     Toast.makeText(getApplicationContext(), state, Toast.LENGTH_LONG).show();
     Toast.makeText(getApplicationContext(), city, Toast.LENGTH_LONG).show();
    }else{
     textGeo.setText("Can't find Location!");
    };
   // }
}


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

}

KOUSIK daniel
  • 305
  • 1
  • 3
  • 11
  • int code = dataInputStream.readInt(); the code value always return 1 .So RqsLocation() always return false – Ganesan J Jul 21 '21 at 07:19
0

What do you mean for "exact location"? LAC and CID as retrieved there are not likely to give you an exact location. You should use LocationManager and retrieve location filtering by "GSM_Provider" as this good explanation: https://developer.android.com/training/location/index.html here another good explanation: http://developer.android.com/guide/topics/location/strategies.html

Sorry if i'm not posting code but the links already provide a lot of useful code and tricks!

So for the code you can:

// The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

LocationManager locationmanager;
        String context=Context.LOCATION_SERVICE;
        locationmanager=(LocationManager) getSystemService(context);
        String provider=LocationManager.NETWORK_PROVIDER;
locationmanager.requestLocationUpdates(provider, MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);

Where your listener is something like

    // Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };
N Dorigatti
  • 3,482
  • 2
  • 22
  • 33
  • 1
    In future, this answer may not be valid if the link gets modified. – Prerak Sola Jun 24 '15 at 09:37
  • I need the locaton of mobile tower. I retrieved LAC and CID values. Will i be able to get the tower location by passing those values in LocationManager ? or Is there any other way? @NDorigatti – Prakash Jun 24 '15 at 10:10
  • oh so you need the Cell location, i thought user location! see next answer – N Dorigatti Jun 24 '15 at 10:13
0

If you need to retrieve the cell location, you have to explore some of the available online services, one of the free ones is: http://opencellid.org/ but is not complete of course. There are other services online but i'm not familiar, I just tested OpenCellId for a few hours

N Dorigatti
  • 3,482
  • 2
  • 22
  • 33