Calculating distance from KML coordinates

1,043 views
Skip to first unread message

Fehmi Noyan ISI

unread,
Aug 30, 2011, 5:15:22 PM8/30/11
to KML Developer Support - Getting Started with KML, fnoy...@yahoo.com
Hi

I'm traying to calculate the distance between two points given in KML.

Here are my placemark properties;

Point 1 <Placemark>
<name>b</name>
<LookAt>
<longitude>33.23257567375305</longitude>
<latitude>36.94942859636785</latitude>
<altitude>0</altitude>
<heading>43.35513991553418</heading>
<tilt>0</tilt>
<range>61908.9363721757</range>
<altitudeMode>relativeToGround</altitudeMode>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Point>
<altitudeMode>clampToGround</altitudeMode>
<gx:altitudeMode>clampToSeaFloor</gx:altitudeMode>
<coordinates>33.12023120186334,36.98160327782041,0</coordinates>
</Point>
</Placemark>


Point 2 <Placemark>
<name>a</name>
<LookAt>
<longitude>33.18652778042419</longitude>
<latitude>36.9678304970375</latitude>
<altitude>0</altitude>
<heading>43.32745181347833</heading>
<tilt>0</tilt>
<range>61816.1998136991</range>
<altitudeMode>relativeToGround</altitudeMode>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Point>
<altitudeMode>clampToGround</altitudeMode>
<gx:altitudeMode>clampToSeaFloor</gx:altitudeMode>
<coordinates>33.18652778042419,36.9678304970375,0</coordinates>
</Point>
</Placemark>

And here is the code I use (get it from net in JavaScript format,
convert it it C++)

#include <iostream>
#include <stdlib.h>
#include <math.h>
double ConvertToRadians (double val){
double PI = 3.145;
return (val*PI) / 180;
}
int main(int argc, char *argv[])
{
double R = 6371; // km
double lat1 = 36.9678304970375;
double lat2 = 36.94942859636785;
double lon1 = 33.18652778042419;
double lon2 = 33.23257567375305;
double d =
acos(sin(ConvertToRadians(lat1))*sin(ConvertToRadians(lat2)) +
cos(ConvertToRadians(lat1))*cos(ConvertToRadians(lat2)) *
cos(ConvertToRadians(lon2-lon1))) * R;
std::cout << d << std::endl;
system("PAUSE");
return 0;
}

The distance supposed to be 5.9 ~ 6 km, but program gives something
like 4,5 km.

What is wrong with the calculation?

Thanks...

Fehmi Noyan ISI

unread,
Aug 30, 2011, 5:36:33 PM8/30/11
to KML Developer Support - Getting Started with KML
I found the mistake I made...I should use the coordinates in <point>
but what I did in my C++ code was using the coordinates in <LookAt>.
It's ok now...
Reply all
Reply to author
Forward
0 new messages