0
我想分享我迄今为止准备好的内容,同时请求下一步所需编码步骤的帮助和建议。android gps客户端只在区域内发送坐标到服务器
短期和基本的Java和Android培训和在线资源基础,我想出了一个具有以下目标(理论,因为我没有测试它尚未)下面的理论代码:
- 明确在一个TextView
- 通过3G连接到服务器选择GPS提供商(GPS /电池/ WIFI)知道手机的位置
- dislays当前位置
- 频繁发送的纬度,经度和时间戳尽可能一台服务器
下面是我编写的代码:
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
}
}
}
请帮助
- 请评论,如果上面的代码将满足给出的四个目标。
- 如何才能满足我的第5个目标-----我希望android应用程序开始触发连接到服务器并仅在手机(在汽车中使用)位于道路区域内时发送经度和纬度(比如面积1km x 30m)。它一直在听它的位置,但一旦进入该区域就会开始发送到服务器,并且将继续发送,并且只有在离开该区域后才会停止。
1.谢谢user931366!感谢您的时间,评论和帮助。你是对的。我认为我应该输入“明确选择BEST提供商(GPS/cell/wifi)来了解手机的位置”,因为这就是我的意思 - 我的不好。 2.好吧,我会检查3.我不熟悉JSON或XML,我会检查你的建议4.很酷5.做出很多感 - 我会验证特定区域是否可以应用 – coollearner 2012-01-11 06:49:43
嗨用户931366。我认为当矩形GPS区域的边与纬度和经度线平行时,对物镜5的回答是可以的。如果矩形区域的边与纬度线和经度线不平行怎么办? – coollearner 2012-01-21 16:31:46