# Geo Code

**URL:** <https://www.ruby-forum.com/t/geo-code/160736>\
**Category:** Rails\
**Created:** [February 19, 2009, 8:02am UTC](https://www.ruby-forum.com/t/geo-code/160736 "2009-02-19T08:02:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 19, 2009, 8:02am UTC](https://www.ruby-forum.com/t/geo-code/160736/1 "2009-02-19T08:02:28Z")

</div>

Hello Everyone,

any one has idea abt geocode ? in rails

I want to find distance in miles between 2 points , I have lattitude and  
longitude of both points ,

how can i do that ?

please help me

thanks

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 19, 2009, 9:55am UTC](https://www.ruby-forum.com/t/geo-code/160736/2 "2009-02-19T09:55:26Z")

</div>

Cyrus D. wrote:

> I want to find distance in miles between 2 points , I have lattitude and  
> longitude of both points ,

I do this (inside a model what has latitude and longitude attributes)

RadConv=(Math::PI/180)  
R=3958

def get\_distance\_to(to\_lat, to\_lon)  
lat1=self.latitude \* RadConv  
lon1=self.longitude \* RadConv  
lat2=to\_lat \* RadConv  
lon2=to\_lon \* RadConv

```
 diff_lon = (lon2 - lon1).abs
 diff_lat = (lat2 - lat1).abs

 a = Math.sin(diff_lat/2) * Math.sin(diff_lat/2) + Math.cos(lat1) *

```

Math.cos(lat2) \* Math.sin(diff\_lon/2) \* Math.sin(diff\_lon/2);  
c = 2 \* Math.atan2(Math.sqrt(a), Math.sqrt(1-a));  
d = R \* c;

```
 return d;

```

end

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 19, 2009, 9:57am UTC](https://www.ruby-forum.com/t/geo-code/160736/3 "2009-02-19T09:57:07Z")

</div>

Thanks Andrew

but is there a way that i can do with geo code kit ?

any idea ?

Andrew P. wrote:

> Cyrus D. wrote:
> 
> > I want to find distance in miles between 2 points , I have lattitude and  
> > longitude of both points ,
> 
> I do this (inside a model what has latitude and longitude attributes)
> 
> RadConv=(Math::PI/180)  
> R=3958
> 
> def get\_distance\_to(to\_lat, to\_lon)  
> lat1=self.latitude \* RadConv  
> lon1=self.longitude \* RadConv  
> lat2=to\_lat \* RadConv  
> lon2=to\_lon \* RadConv
> 
> ```
> diff_lon = (lon2 - lon1).abs
> diff_lat = (lat2 - lat1).abs
> 
> a = Math.sin(diff_lat/2) * Math.sin(diff_lat/2) + Math.cos(lat1) *
> 
> ```
> 
> Math.cos(lat2) \* Math.sin(diff\_lon/2) \* Math.sin(diff\_lon/2);  
> c = 2 \* Math.atan2(Math.sqrt(a), Math.sqrt(1-a));  
> d = R \* c;
> 
> ```
> return d;
> 
> ```
> 
> end

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 19, 2009, 10:02am UTC](https://www.ruby-forum.com/t/geo-code/160736/4 "2009-02-19T10:02:14Z")

</div>

On Thu, Feb 19, 2009 at 12:57 AM, Cyrus D. \<  
removed\_email\_address@domain.invalid\> wrote:

> > > 
> > 
> > ```
> > lon1=self.longitude * RadConv
> > 
> > return d;
> > 
> > ```
> > 
> > end

Hi, please read the following:

[http://geokit.rubyforge.org/](http://geokit.rubyforge.org/)

Good luck,

-Conrad
