如何在房子内获得Android GPS位置

时间:2012-03-29 19:37:06

标签: android gps titanium

当一个人呆在房子里面时,我需要得到一个人的gps位置。

GPS无法在室内工作,但我听到有人解释说在Android中你首先要检查GPS是否可用。如果不是,则下一步是根据附近的无线网络本地化人员。

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

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

一种选择是Skyhook。这是他们SDK的链接。它与Android兼容。这不会像GPS一样准确,但它会在大多数时间内至少让你进入正确的建筑物内。

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

答案 3 :(得分:0)

你可以在你想要获得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 
     }

} // CELLID

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