【问题标题】:Android : Finding location using LAC and CIDAndroid:使用 LAC 和 CID 查找位置
【发布时间】:2015-06-25 13:51:37
【问题描述】:

我在我的应用程序中获得了本地区号 (LAC) 和小区 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();
}

【问题讨论】:

    标签: java android gps


    【解决方案1】:

    这是通过 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();       
    }
    

    }

    【讨论】:

    • int 码 = dataInputStream.readInt();代码值总是返回 1。所以 RqsLocation() 总是返回 false
    【解决方案2】:

    “确切位置”是什么意思?在那里检索到的 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) {}
      };
    

    【讨论】:

    • 将来,如果链接被修改,此答案可能无效。
    • 我需要移动塔的位置。我检索了 LAC 和 CID 值。我可以通过在 LocationManager 中传递这些值来获取塔的位置吗?或者还有其他方法吗? @NDorigatti
    • 哦,所以您需要 Cell 位置,我认为是用户位置!看下一个答案
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2014-08-11
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      相关资源
      最近更新 更多