Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

Java
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

Java
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();


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.
Java
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
Posted

1 solution

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
Java
double lat = DOuble.parseDouble("latitude_value_string")

I am putting a pseudo code below.
JavaScript
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    coordinates = ("" + latitude + "," + longitude);
    Transmit(coordinates);         
}

On the receiving your code will look like something below, assuming that you have retrieved the string in strData.
C#
string[] coOrds = strData.split(ne Char[] {','});
double latitude = Double.parseDouble(coOrds[0]);
double longitude = Double.parseDouble(coOrds[1]);
}


Regards,
 
Share this answer
 
v2
Comments
GaryDoo 17-Mar-13 15:26pm    
Hi Prasad, thank you for your speedy reply, I had looked at parseDouble already, however, this issue I was having was that there are two doubles within the one string a latitude double and a longitude double. I'm wondering how, when I have the following line of code

double lat = double.parseDouble(location);

do I extract or parse one of the doubles? i.e. the latitude double?


I have used tcp/ip sockets to transmit the coordinates, now as I've said I'm quite inexperienced in android/java, could you explain the JSON structure method or separator method?

Regards,
Gary
Prasad Khandekar 18-Mar-13 1:53am    
Hello,

I have updated the solution with example.

Regards,
GaryDoo 3-Apr-13 13:50pm    
Prasad...I've tried to implement this, but I seem to get errors...

String[] Loc = location.split(",");
double Clatitude = Double.parseDouble(Loc[0]);
double Clongitude = Double.parseDouble(Loc[1]);

These are the lines of code I am using to split the string and then to parse them to doubles, does this look incorrect to you?
Prasad Khandekar 3-Apr-13 16:19pm    
In order for this to work you need to ensure that the location string indeed contains the coordinates separated by the comma ",". Also you can add a condition (loc.length > 1) then compute Clongitude.

What is the error you are getting?
GaryDoo 3-Apr-13 17:06pm    
my apologies Prasad, it was an oversight on my behalf, I was testing with a simple string and had forgotten to comment it out, it was this that was causing me errors as it was trying to split a string with no ","!!!


However, if I could I would like to ask your advice, is there anyway for me to test that these 'Clatitude' and 'Clongitude' are correct? Is there any way to display them for testing purpose? I am trying to calculate the distance and display it, I am using the following code
(should I open a new discussion?)

public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Location parent = new Location("parent");

Platitude = location.getLatitude();
Plongitude = location.getLongitude();

parent.setLatitude(Platitude / 1e6);
parent.setLongitude(Plongitude / 1e6);

Location child = new Location("child");

child.setLatitude(Clatitude / 1e6);
child.setLongitude(Clongitude / 1e6);

distance = parent.distanceTo(child);

String distanceSt = String.valueOf(distance);

TextView myTextview = (TextView) findViewById(R.id.distTo);
myTextview.setText(distanceSt);
}
}
I am testing with a mobile device and an emulator. I sent test coordinates to the emulator an I got a result of 9.5 meters, so to test I input the coordinates of my mobile device and I get a distance of 9.3 meters where I'm sure this should be zero.

Can you see where I'm going wrong?

Regards,
Gary

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900