【问题标题】:android gps client sending coordinates to server only if within an areaandroid gps客户端仅在区域内向服务器发送坐标
【发布时间】:2012-01-11 01:44:36
【问题描述】:

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

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

  1. 明确选择 GPS 提供商 (GPS/cell/wifi) 以了解手机的位置
  2. 在文本视图中显示当前位置
  3. 通过 3g 连接到服务器
  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 )。它一直在监听它的位置,但是一旦它进入区域就会开始发送到服务器,并且会持续发送并且只有在它离开区域时才会停止。

【问题讨论】:

    标签: android gps client location


    【解决方案1】:
    1. 没有。您要求操作系统提供最佳提供商。这将是明确的lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
    2. 无法工作,因为您无法从侦听器更新文本视图。看看android.OS.Handler
    3. 从技术上讲,您会遇到这种情况,但您最好使用 JSON 或 XML over HTTP,而不是发明自己的协议。
    4. 是的

    对于 5,创建两个位置,一个西北位置和一个东南位置,代表您的盒子。在您的 onLocationChanged 方法中,将新位置与角进行比较,例如 (l.lat > se.lat && l.lat nw。 lon) 其中“l”是回调中的最新位置,“se”是边界的东南角,“nw”是边界的西北角。如果满足上述4个条件,那么你发送到你的服务器。

    【讨论】:

    • 1.谢谢用户931366!感谢您的时间、评论和帮助。你是对的。我想我应该输入“明确选择最佳提供商(GPS/手机/wifi)以了解手机的位置”,因为这就是我的意思——我的错。 2. 好的,我会检查 3. 我不熟悉 JSON 或 XML,我会检查你的建议 4. 很酷 5. 很有意义 - 如果可以应用,我会在特定区域进行验证
    • 您好 user931366。我认为当矩形 GPS 区域的边与纬度和经度线平行时,您对目标 5 的回答是可以的。如果矩形区域的边不平行于经纬线会怎样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    相关资源
    最近更新 更多