# Distance To() Returning incorrect distances

Page 1 of 1

## 4 Replies - 1336 Views - Last Post: 19 April 2013 - 04:31 PM

### #1 GaryDoo

Reputation: 5
• Posts: 141
• Joined: 10-February 13

# Distance To() Returning incorrect distances

Posted 06 April 2013 - 05:36 AM

Hi,

I'm hoping someone can tell me what is wrong with this code?

```public void onLocationchanged(Location location) {
// TODO Auto-generated method stub

Location parent = new Location("parent");

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

parent.setLatitude(Platitude);
parent.setLongitude(Plongitude);

//--INITIALIZING NEW LOCATION & SETTING CHILDS LOCATION
Location child = new Location("child");

child.setLatitude(Clatitude);
child.setLongitude(Clongitude);

//GETTIING DISTANCE BETWEEN PARENT AND CHILD
distance = parent.distanceTo(child);
}
```

No matter what dummy coordinates I send to the emulator the distance is incorrect...is there an issue with my method? I would greatly appreciate help on this.

Is This A Good Question/Topic? 0

## Replies To: Distance To() Returning incorrect distances

### #2 GaryDoo

Reputation: 5
• Posts: 141
• Joined: 10-February 13

## Re: Distance To() Returning incorrect distances

Posted 07 April 2013 - 06:46 AM

no one can help????

### #3 Gungnir

Reputation: 152
• Posts: 527
• Joined: 21-May 11

## Re: Distance To() Returning incorrect distances

Posted 19 April 2013 - 03:21 PM

Are Clatitude and Clongitude constants? Where are they defined?

Is your distance formula ordered properly? Remember that Java doesn't follow the standard mathematical order of operations, so you have to break the distance formula up a little bit.

```int x = (x2 - x1) ^ 2;
int y = (y2 - y1) ^ 2;
int distance = Math.sqrt((x + y));

```

### #4 GaryDoo

Reputation: 5
• Posts: 141
• Joined: 10-February 13

## Re: Distance To() Returning incorrect distances

Posted 19 April 2013 - 04:21 PM

Gungnir, on 19 April 2013 - 03:21 PM, said:

Are Clatitude and Clongitude constants? Where are they defined?

Is your distance formula ordered properly? Remember that Java doesn't follow the standard mathematical order of operations, so you have to break the distance formula up a little bit.

```int x = (x2 - x1) ^ 2;
int y = (y2 - y1) ^ 2;
int distance = Math.sqrt((x + y));

```

Hi, thanks for your reply. I had been using a mobile device & the emulator to test, I suspected that there was an issue passing the coordinates to the emulator, so I got a second mobile device, it works perfectly now, thanks very much though!

Regards, Gary

### #5 Gungnir

Reputation: 152
• Posts: 527
• Joined: 21-May 11

## Re: Distance To() Returning incorrect distances

Posted 19 April 2013 - 04:31 PM

Be wary of screen densities.

It's improbable that the issue was with the emulator, and more likely that your app isn't density independent.

- Android Developer > Supporting Multiple Screens

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }