你好,这是我在stackloverflow上的第一篇文章,我是begginer程序员,我会尽力解释我的问题。
首先让我告诉你,代码来自在线教程,如果有人知道回答我,我想问你一些事情。
让我们从代码开始......
public void onStatusChanged(String provider, int status, Bundle extras)
{
Thread t = new Thread(new Runnable() {
@Override
public void run()
{
try
{
Socket connection;
connection = new Socket(textIp.getText().toString(), Integer.parseInt(textPort.getText().toString()));
DataOutputStream dos,dos1;
dos1=new DataOutputStream(connection.getOutputStream());
dos = new DataOutputStream(connection.getOutputStream());
dos.writeDouble(lat);
dos1.writeDouble(lon);
dos.flush();
dos.close();
dos1.flush();
dos1.close();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
});
t.start();
}
你可以看到我正在使用“onStatusChanged”方法,我正在尝试将我的坐标从gps发送到服务器......
这是我的服务器代码,我不知道放在哪里。
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(6666);
while (true)
{
Socket connection = ss.accept();
DataInputStream dIS = new DataInputStream(connection.getInputStream());
DataInputStream DIS1= new DataInputStream(connection.getInputStream());
// System.out.println("lat"+dIS.readDouble()+"\n"+"Long:"+DIS1.readDouble());
dIS.close();
DIS1.close();
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
好的,现在我的服务器代码包含那些行
LatLng latLng = new LatLng(lat, long);
//lat,long=are not initialized it's just for me to try to explain
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
好吧现在,如果我手动给坐标,它会在地图中弹出一个标记并说“你在这里”,但我想从客户端给出坐标,同时客户端移动标记(服务器应用程序)更新和也在地图上移动
我的一些问题......
问题1:我是否需要在服务器上使用其他“onStatusChanged”方法?
问题2:我是否需要同步线程?为什么我使用线程?
我想给我一些想法,以便搜索和阅读!
如果您无法理解我的问题是什么,请告诉我,我会再尝试解释一下,如果我需要更具体...