【问题标题】:How to get Android GPS location when inside a house在家中时如何获取 Android GPS 位置
【发布时间】:2012-03-29 19:37:06
【问题描述】:

当一个人待在房子里时,我需要获取它的 gps 位置。

GPS 在房屋内无法使用,但我听到有人解释说,在 Android 中您应该首先检查 GPS 是否可用。如果不是,下一步将是根据附近的无线网络定位此人。

我对此很陌生,我计划用钛开发一个简单的应用程序。我的问题是: 当 gps 不可用时,您可以根据无线网络定位某人吗?我可以获取建筑物内人员的位置吗?

【问题讨论】:

  • 这需要有多准确?您是否只需要弄清楚某人在哪个社区,或者您需要知道某人在哪里?

标签: android gps titanium


【解决方案1】:

您向位置提供商询问最佳提供商,如果 GPS 不可用,您将从 cellID(移动桅杆)或无线网络获取位置,请参阅 this 和相关的 stackoverflow question

【讨论】:

    【解决方案2】:

    一个选项是 Skyhook。这是他们的 SDK 的链接。它是安卓兼容的。这不会像 GPS 那样准确,但它至少可以让您在大多数情况下进入正确的建筑物内。

    http://www.skyhookwireless.com/location-technology/sdk.php

    【讨论】:

      【解决方案3】:

      似乎是一个好的开始,我希望这段代码可以帮助:

      try {
                  gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
              } catch (Exception ex) {
              }
              try {
                  network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
              } catch (Exception ex) {
              }
      
              // don't start listeners if no provider is enabled
              if (!gps_enabled && !network_enabled) {
                  AlertDialog.Builder builder = new Builder(this);
                  builder.setTitle("Attention!");
                  builder.setMessage("Sorry, location is not determined. Please enable location providers");
                  builder.create().show();
              }
      
              if (gps_enabled) {
                  locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
              }
              if (network_enabled) {
                  locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
              }
      

      从这里:http://www.hrupin.com/2011/04/android-gps-using-how-to-get-current-location-example

      【讨论】:

        【解决方案4】:

        您可以在想要获取 cellID 位置的任何地方调用下面的 cellID 函数。它不会像 GPS 那样准确

         public void cellID()
        {
            TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        
        
            GsmCellLocation cellLocation = (GsmCellLocation)    telephonyManager.getCellLocation();     
              int cid = cellLocation.getCid();
              int lac = cellLocation.getLac();
              SimpleDateFormat sdfDateTime = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                datetimeCellID = sdfDateTime.format(new Date());
              String cell_Id=String.valueOf(cid);
              String gsm_Loc_Area_Code=String.valueOf(lac);
            //  Toast.makeText(getBaseContext(),"cellid="+cell_Id+"\nGsm Location Area Code:"+gsm_Loc_Area_Code,Toast.LENGTH_LONG ).show();
        
              if(RqsLocation(cid, lac))
              {
                  TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    imeiCellID = tm.getDeviceId();
                  latitude_cellID=String.valueOf((float)myLatitude/1000000);
                  longitude_cellID=String.valueOf((float)myLongitude/1000000);
                // Toast.makeText(getBaseContext(),"Lat:"+latitude_cellID+"Long:"+longitude_cellID,Toast.LENGTH_LONG ).show();
        
        
              }//if
              else
              {
                  Toast.makeText(getBaseContext(),"CellID : Can't find Location",Toast.LENGTH_LONG ).show();
              }//else 
             }
        

        }//小区ID

          private Boolean RqsLocation(int cid, int lac)
          {
              //Toast.makeText(getBaseContext(),"inReqloc",Toast.LENGTH_LONG ).show();
           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();    
          }  
        

        【讨论】:

          猜你喜欢
          • 2013-05-06
          • 1970-01-01
          • 2015-05-17
          • 1970-01-01
          • 2020-12-17
          • 1970-01-01
          • 2023-04-10
          • 2016-07-30
          • 1970-01-01
          相关资源
          最近更新 更多