Android : Finding Location Using Lac And Cid
Solution 1:
This one way to get location through LAC and CID:
TelephonyManagertelephonyManager= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocationcellLocation= (GsmCellLocation) telephonyManager.getCellLocation();
intcid= cellLocation.getCid();
intlac= 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 = newGeocoder(AndroidTelephonyManager.this, Locale.ENGLISH);
try {
addresses = geocoder.getFromLocation(lat_double, lang_double, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilderstr=newStringBuilder();
//if (geocoder.isPresent()) {// Toast.makeText(getApplicationContext(),// "geocoder present",// Toast.LENGTH_SHORT).show();AddressreturnAddress= addresses.get(0);
Stringarea= returnAddress.getFeatureName();
Stringthfare= returnAddress.getThoroughfare();
StringlocalityString= returnAddress.getLocality();
// String region_code = returnAddress.getCountryCode();Stringzipcode= returnAddress.getPostalCode();
Stringstate= returnAddress.getAdminArea();
Stringsublocal= returnAddress.getSubLocality();
Stringcity= 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){
Booleanresult=false;
Stringurlmmap="http://www.google.com/glm/mmap";
try {
URLurl=newURL(urlmmap);
URLConnectionconn= url.openConnection();
HttpURLConnectionhttpConn= (HttpURLConnection) conn;
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
OutputStreamoutputStream= httpConn.getOutputStream();
WriteData(outputStream, cid, lac);
InputStreaminputStream= httpConn.getInputStream();
DataInputStreamdataInputStream=newDataInputStream(inputStream);
dataInputStream.readShort();
dataInputStream.readByte();
intcode= 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;
}
privatevoidWriteData(OutputStream out, int cid, int lac)throws IOException
{
DataOutputStreamdataOutputStream=newDataOutputStream(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();
}
}
Solution 2:
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 metersprivatestaticfinallong MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters// The minimum time between updates in millisecondsprivatestaticfinallong 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 updatesLocationListener locationListener = newLocationListener() {
publicvoidonLocationChanged(Location location) {
// Called when a new location is found by the network location provider.makeUseOfNewLocation(location);
}
publicvoidonStatusChanged(String provider, int status, Bundle extras) {}
publicvoidonProviderEnabled(String provider) {}
publicvoidonProviderDisabled(String provider) {}
};
Solution 3:
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
Post a Comment for "Android : Finding Location Using Lac And Cid"