2012-01-11 71 views
0

我想分享我迄今爲止準備好的內容,同時請求下一步所需編碼步驟的幫助和建議。android gps客戶端只在區域內發送座標到服務器

短期和基本的Java和Android培訓和在線資源基礎,我想出了一個具有以下目標(理論,因爲我沒有測試它尚未)下面的理論代碼:

  1. 明確在一個TextView
  2. 通過3G連接到服務器選擇GPS提供商(GPS /電池/ WIFI)知道手機的位置
  3. dislays當前位置
  4. 頻繁發送的緯度,經度和時間戳儘可能一臺服務器

下面是我編寫的代碼:

import android.app.Activity; 
import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.TextView; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 

public class GpsActivity extends Activity { 

private LocationManager lm; 
private LocationListener locationListener; 
public static TelephonyManager tm; 
public static TextView tv; 
public static Socket s; 
public static PrintWriter out; 


/** 
* Called when the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
/** 
    * retrieve a reference to provide access to information about the telephony services on the device  
    */ 
    tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
    setContentView(R.layout.main); 
/** 
    * retrieve a reference to provide access to the system location services  
    */    
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  


/** 
* explicitly select the GPS provider, create a set of Criteria and let android choose the best provider available 
*/ 

Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_FINE); 
criteria.setAltitudeRequired(false); 
criteria.setBearingRequired(false); 
criteria.setCostAllowed(true); 
criteria.setPowerRequirement(Criteria.POWER_LOW); 
String provider = lm.getBestProvider(criteria, true); 
/** 
* This method takes in four parameters: 
provider: The name of the provider with which you register 
minTime: The minimum time interval for notifications, in milliseconds. 
minDistance: The minimum distance interval for notifications, in meters. 
listener: An object whose onLocationChanged() method will be called for each location update. 
*/ 
locationListener = new MyLocationListener(); 
lm.requestLocationUpdates(provider, 0, 0, locationListener); 

tv = (TextView) findViewById(R.id.textView1); 
tv.setText("I currently have no Location Data."); 

} 

/** 
* Connects the Android Client to a given server 
* 
* @param name 
*   The name of the remote server 
* @param port 
*   Port number to connect to at the remote server. 
* @throws IOException 
* @throws UnknownHostException 
*/ 
public static void connect(String name, int port) 
     throws UnknownHostException, IOException 
{ 

    s = new Socket(name, port); 
    out = new PrintWriter(s.getOutputStream(), true); 
} 

/** 
* Sends a string message to the server. 
* 
* @param msg 
*   The message to be sent. 
* @throws IOException 
*/ 
public static void send(String msg) throws IOException 
{ 
    if (!s.isClosed() && msg != null) 
    { 
     out.println(msg); 
     if (msg.contains("CMD_QUIT")) 
     { 
      out.close(); 
      s.close(); 
      Log.i("ServerConnection", "Client Disconnected."); 
     } 
    } 
} 


private class MyLocationListener implements LocationListener{ 

    @Override 
    public void onLocationChanged(Location loc) { 
     String txt = "Latitude:" + loc.getLatitude() + "/nLongitude:" + loc.getLongitude(); 
     Log.i("GeoLocation", "My current location is:\n " + txt); 
     tv.setText("My current location is:\n" + txt); 
     String msg = loc.getLongitude() + "\n" + loc.getLatitude() + "\n" 
      + loc.getTime(); 

    try 
     { 
     connect("IP address", 27960); 
     send("CMD_HELLO"); 
     send(msg); 
     send("CMD_QUIT"); 
     } catch (UnknownHostException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 



    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

} 

}

請幫助

  1. 請評論,如果上面的代碼將滿足給出的四個目標。
  2. 如何才能滿足我的第5個目標-----我希望android應用程序開始觸發連接到服務器並僅在手機(在汽車中使用)位於道路區域內時發送經度和緯度(比如面積1km x 30m)。它一直在聽它的位置,但一旦進入該區域就會開始發送到服務器,並且將繼續發送,並且只有在離開該區域後纔會停止。

回答

0
  1. 不可以。您要求操作系統提供最佳提供商。這將是明文lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  2. 不會工作,因爲你不能更新來自偵聽器的文本視圖。看看android.OS.Handler
  3. 從技術上說,你遇到了這個問題,但是最好用JSON或XML over HTTP進行,而不是發明自己的協議。

對於5,創建兩個位置,西北位置,代表你的盒子東南位置。在你onLocationChanged方法,邊角採用比較新的位置,使得(l.lat> se.lat & & l.lat < nw.lat)和(l.lon < se.lon & & l.lon>西北。 lon)其中「l」是回調的最新位置,「se」是你的邊界的東南角,「nw」是你的邊界的西北角。如果它符合以上4個條件,則發送到您的服務器。

+0

1.謝謝user931366!感謝您的時間,評論和幫助。你是對的。我認爲我應該輸入「明確選擇BEST提供商(GPS/cell/wifi)來了解手機的位置」,因爲這就是我的意思 - 我的不好。 2.好吧,我會檢查3.我不熟悉JSON或XML,我會檢查你的建議4.很酷5.做出很多感 - 我會驗證特定區域是否可以應用 – coollearner 2012-01-11 06:49:43

+0

嗨用戶931366。我認爲當矩形GPS區域的邊與緯度和經度線平行時,對物鏡5的回答是可以的。如果矩形區域的邊與緯度線和經度線不平行怎麼辦? – coollearner 2012-01-21 16:31:46

相關問題