Android:使用LAC和CID查找位置

时间:2015-06-24 07:55:24

标签: java android gps

我在应用程序中获得了Local Area Code(LAC)和Cell ID(CID)的值。但我无法使用这些值找到确切的位置。我不知道自己错过了什么。有人帮助我

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

3 个答案:

答案 0 :(得分:2)

这种通过LAC和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();       
}

}

答案 1 :(得分:0)

你是什么意思"准确的位置"?检索到的LAC和CID不太可能为您提供确切的位置。 您应该使用LocationManager并通过" GSM_Provider"检索位置过滤。正如这个好的解释:https://developer.android.com/training/location/index.html 这是另一个很好的解释 http://developer.android.com/guide/topics/location/strategies.html

很抱歉,如果我没有发布代码,但链接已经提供了很多有用的代码和技巧!

所以对于代码你可以:

// 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);

你的听众在哪里

    // 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) {}
  };

答案 2 :(得分:0)

如果您需要检索单元格位置,您必须浏览一些可用的在线服务,其中一个免费的服务是: http://opencellid.org/ 但当然不完整。 网上有其他服务,但我不熟悉,我刚刚测试了OpenCellId几个小时