DistanceTo()与字符串 [英] DistanceTo() with string

查看:109
本文介绍了DistanceTo()与字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们,



我对android很新,很缺乏经验。我正在尝试开发一个应用程序,通过java服务器将一个电话(客户端a)的坐标传输到(客户端b),然后我想检查客户端之间的距离。我已设法获取客户端a的坐标,通过套接字将它们发送到服务器,然后发送到客户端b。问题是我以字符串格式发送它们。为了利用android.loaction中的distanceTo()方法,我需要有一个双lat和双长的客户端a。



我我只是从客户端附上一段代码,我是如何发送坐标的。



Hi folks,

I''m newish to android, very inexperienced. I''m trying to develop an application that transmits the coordinates of one phone (client a) to (client b) via a java server, I then want to check the distance between clients. I have managed to get the coordinates of client a, send them via a socket to the server and then onto client b. The problem is I send them in a string format. In order to make use to make use of the distanceTo() method in the android.loaction I need to have a double lat and double long of client a.

I''m just attaching a snippet of code from client a how I''m sending the coordinates.

public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
             latitude = location.getLatitude();
             longitude = location.getLongitude();
             coordinates = (""+latitude + longitude);
             Transmit(coordinates);         
        } 





传输方式如下





The transmit method is a follows

private void Transmit(final String message) {
    Thread trans = new Thread(new Runnable() {
        public void run() {
            Log.d("TRANSMIT", "CALLED");
            // TODO Auto-generated method stub
            try {
                s = new Socket("192.168.1.1", 2222); // connect to
                                                        // server
                Log.d("CONNECTED", "Connected");
                DataOutputStream _OutPut = new DataOutputStream(
                        s.getOutputStream());
                _OutPut.writeBytes(message + "\n");
                _OutPut.flush();
                _OutPut.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    trans.start();





所以客户b收到的坐标为字符串,我不确定是什么意思如何在distanceTo()方法中使用字符串格式的coorinates或如何从字符串中提取它们,或者我应该在double数组中发送它们?所以我需要将它们作为单独的双打出来,即



So client b recieves the coordinates as a string and I''m unsure as to how to use the coorinates in string format in the distanceTo() method or how to extract them out of a string, or should i send them in a double array? So I need to take them out as individual doubles i.e.

client a lat = client a long =





任何对此的帮助都会受到赞赏,因为我开始找到自己的脚,但有些事情会引发问题。



提前致谢。



问候,

加里



Any help on this would be appreciated, as I''m starting to find my feet but there are certain things that throw up problems.

Thanks in advance.

Regards,
Gary

推荐答案

您好,

首先,我建议使用分隔符或JSON结构来传输此信息。这种方式在客户端上你没有任何问题分开纬度和&经度值。现在可以使用
Hello,
First of all I will suggest to use a separator or a JSON structure to transmit this information. This way on client you do not have any problems separating the latitude & Longitude values. Now converting a string to numeric form can easily be achieved using
double lat = DOuble.parseDouble("latitude_value_string")



我在下面放一个伪代码。


I am putting a pseudo code below.

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    coordinates = ("" + latitude + "," + longitude);
    Transmit(coordinates);         
}



在收到您的代码时,您的代码将如下所示,假设您已在 strData


On the receiving your code will look like something below, assuming that you have retrieved the string in strData.

string[] coOrds = strData.split(ne Char[] {','});
double latitude = Double.parseDouble(coOrds[0]);
double longitude = Double.parseDouble(coOrds[1]);
}





问候,



Regards,


这篇关于DistanceTo()与字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆