Problems with a function

[code]
local distans=0
function CalcDist(firstPointLatitude1,firstPointLongitude1,secondPointLatitude1,secondPointLongitude1)
earthRadius = 6367000
firstPointLatitude = (firstPointLatitude1 * math.pi ) / 180
firstPointLongitude = (firstPointLongitude1 * math.pi ) / 180
secondPointLatitude = (secondPointLatitude1 * math.pi ) / 180
secondPointLongitude = (secondPointLongitude1 * math.pi ) / 180
calcLongitude = secondPointLongitude - firstPointLongitude
calcLatitude = secondPointLatitude - firstPointLatitude
stepOne = math.pow(math.sin(calcLatitude/2), 2) + math.cos(firstPointLatitude) * math.cos(secondPointLatitude) * math.pow(math.sin(calcLongitude/2), 2)
stepTwo = 2 * math.asin(math.min(1,math.sqrt(stepOne)))

calculatedDistance = earthRadius * stepTwo
distans= calculatedDistance

end
local citys = xml:loadFile( “town.xml”, system.TemporaryDirectory)

for i=1,#citys.child do
byer[i] = citys.child[i]
end
for i=1,#byer do

adress=byer[i].child[4].value
local latitude, longitude = myMap:getAddressLocation( adress )
currentLocation = myMap:getUserLocation()
currentLatitude = currentLocation.latitude
currentLongitude = currentLocation.longitude
CalcDist(currentLatitude,currentLongitude,latitude,longitude)

if (distans < 10000) then
data[i]={}
data[i].dist=distans
end
end [/code]

This is the code that I am struggling with, if I write if (distans \< 10000) then
then I get a value in distant, but if I write if (distans \< 1000) then then it does not work.

Somebody got a clue??
maholm
[import]uid: 5717 topic_id: 21475 reply_id: 321475[/import]

What do you mean by does not work?

Have you tried this?

[code]
if (distans < 10000) then
data[i]={}
data[i].dist=distans

else
print (distans)

end
[/code] [import]uid: 108660 topic_id: 21475 reply_id: 84996[/import]

The function calculates the distance between two places and it returns a value in meters.
I got many addresses in an table that I send to the function.

If I do

if (distans \< 10000) then  

I can populate the table data but if I try a lower value it does´t end up in the table.
I don´t know how to explain it, but maybe it has something to do with decimals?
Maholm
[import]uid: 5717 topic_id: 21475 reply_id: 85007[/import]

If I search within a radius of 10000 meters it does work but if I try a value below 10000 it doesn´t.

:frowning:
[import]uid: 5717 topic_id: 21475 reply_id: 85010[/import]

All I see is that none of your values are less than 1000.
I can’t see a problem here.

Add the print statement and check the values for yourself.
If you EXPECT some values of less than 1000, and you don’t get any, then the maths in the function is probably suspect.

[import]uid: 108660 topic_id: 21475 reply_id: 85016[/import]

I do expect values less than 1000 so there is something wrong but I don´t find it.
I can´t add the print statement because the map view is not available in the simulator…
[import]uid: 5717 topic_id: 21475 reply_id: 85023[/import]

Print statements do generate output on the device, although it takes a bit of digging to find them.
Why not add a text item on screen to show the value, or add a native alert?

[import]uid: 108660 topic_id: 21475 reply_id: 85046[/import]

If I write like this:

  
 if (distans \<1000) then  
  
  
 data[i]={}  
 data[i].title=byer[i].child[2].value  
 data[i].id=byer[i].child[1].value  
 data[i].xmlid=byer[i].child[3].value  
 data[i].dist=distans  
 local text = display.newEmbossedText(data[i].title.." "..data[i].id.." "..data[i].dist, 0, 0, native.systemFont, 12)  
 text:setTextColor(255,165,0)  
  
 text.x=10   
 end  
  
 for i=1,#data do  
 data2[i]={}   
 data2[i].namn=data[i].title  
 data2[i].link=data[i].xmlid  
 end  
 end   
  
  
 myList = tableView.newList{  
 data=data,  
 default="SliderFrame.png",  
 over="SliderFramedown.png",  
 onRelease=listButtonRelease,  
 top=topBoundary,  
 bottom=bottomBoundary,  
 callback=function(row)  
 local t = display.newText(row.title, 0, 0, native.systemFont, 24)  
 t:setTextColor(255,165,0)  
 t.x =20  
 t.y = 25  
 return t  
 end  
}  

Then I get the textfield printed but not the list, but if I change the value to:

if (distans \<10000) then  

Then the list is generated as well.
I can´t understand what is going on here??

[import]uid: 5717 topic_id: 21475 reply_id: 85142[/import]

If I write like this:

  
 if (distans \<1000) then  
  
  
 data[i]={}  
 data[i].title=byer[i].child[2].value  
 data[i].id=byer[i].child[1].value  
 data[i].xmlid=byer[i].child[3].value  
 data[i].dist=distans  
 local text = display.newEmbossedText(data[i].title.." "..data[i].id.." "..data[i].dist, 0, 0, native.systemFont, 12)  
 text:setTextColor(255,165,0)  
 text.x=10   
 end  
  
 for i=1,#data do  
 data2[i]={}   
 data2[i].namn=data[i].title  
 data2[i].link=data[i].xmlid  
 end  
 end   
  
  
 myList = tableView.newList{  
 data=data,  
 default="SliderFrame.png",  
 over="SliderFramedown.png",  
 onRelease=listButtonRelease,  
 top=topBoundary,  
 bottom=bottomBoundary,  
 callback=function(row)  
 local t = display.newText(row.title, 0, 0, native.systemFont, 24)  
 t:setTextColor(255,165,0)  
 t.x =20  
 t.y = 25  
 return t  
 end  
}  

Then I get the textfield printed but not the list, but if I change the value to:

if (distans \<10000) then  

Then the list is generated as well.
I can´t understand what is going on here??

[import]uid: 5717 topic_id: 21475 reply_id: 85141[/import]