本文介绍了Android:使用LAC和CID查找位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用程序中获得了本地区域代码(LAC)和单元ID(CID)的值。但我找不到使用这些值的确切位置。我不知道我错过了什么。有人帮我解决这个问题。
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();
}
推荐答案
通过LAC和CID:
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();
}
}
这篇关于Android:使用LAC和CID查找位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!