android gps客户端仅在区域内向服务器发送坐标

时间:2012-01-11 01:44:37

标签: android gps client location

我想分享我到目前为止所准备的内容,同时就下一步所需的编码步骤寻求帮助和建议。

基于简短的基本java和android培训以及在线资源,我提出了以下具有以下目标的理论代码(理论上因为我还没有测试过):

  1. 明确选择GPS提供商(GPS /手机/ wifi)以了解手机的位置
  2. 在textview中显示当前位置
  3. 通过3g连接到服务器
  4. 尽可能频繁地将纬度,经度和时间戳发送到服务器
  5. 以下是我准备的代码:

     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. 我如何能够达到我的第五个目标-----我希望Android应用程序开始触发连接到服务器并仅在手机(在汽车中使用)位于道路区域内时发送经度和纬度(比方说1km x 30m)。它会不断地听到它的位置,但是一旦它进入该区域就会开始发送到服务器,并且会一直发送,只有在它离开该区域后才会停止。

1 个答案:

答案 0 :(得分:0)

  1. 否。您要求操作系统提供最佳提供商。这将是明确lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  2. 不会工作,因为您无法从侦听器更新文本视图。看看android.OS.Handler
  3. 从技术上讲,你遇到了这个问题,但是最好不要使用JSON或XML over HTTP而不是发明自己的协议。
  4. 对于5,创建两个位置,一个NorthWest位置和一个代表您的盒子的SouthEast位置。在您的onLocationChanged方法中,将新位置与角落进行比较,以便(l.lat> se.lat&& l.lat< nw.lat)和(l.lon< se。 lon& l.lon> nw.lon)其中“l”是回调的最新位置,“se”是你边界的东南角,“nw”是你的bounder的西北角。如果它符合上述4个条件,则发送到您的服务器。