Solved

how to find nearest Hospital location by latitude and longitude

Posted on 2013-06-05
12
1,882 Views
Last Modified: 2013-07-01
Hi Experts

I am working on a Hospital website Portal. Where i will show list of hospitals in the page. Each hospital has own Latitude and longitude. My question is that i want to show the list of hospital with the radius of 50km with the help of latitude and longitude.

e.g Hospital A has longitude and latitude. i want to display other hospital around 50km within radius of HospitalA.

Can I get a solution ?pls help me

thanks for Advance.
0
Comment
Question by:krushnac
  • 3
  • 3
  • 2
  • +1
12 Comments
 
LVL 27

Expert Comment

by:Sammy
Comment Utility
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Interesting question - requires reall programming.

There are two hurdles to jump on this one.

How to efficiently narrow the search and then to find those that fit the actual requirement.

You are looking for hospitals within a circle radius of a specific point - but to pull info from a database using circle maths is not easily done.

So what you do is pull a working set - i.e find all hospitals within a square 100Km to a side with target hospital at the centre - from this you can then eliminate the ones that don't match.

Here is a possible solution

1. There are approximately .45 degrees in a 50Km distance (varies depending where you are on the globe - but lets use that for now).

2. Find all hospitals that are +.45 / -.45 degrees North / South and East / West of your target hospital

select * from hospitals where lattitude > TARGET_LATTITUDE - 0.45 and lattitude < TARGET_LATTITUDE + 0.45 and longitude > TARGET_LONGITUDE - 0.45 and longitude < TARGET_LONGITUDE + 0.45

3. Narrow your search down even further by excluding hospitals you know for definite are in the 50KM radius. You can do this by working out which fall with in the inscribed square

4. Using the fact that the side of an inscribed square is sqrt(2)r with radius 50 we have the side of an inscribed square as  70.7Km so half the distanced is 35.355Km which is approximately 0.32 degrees. So we can exclude all records that match these criteria

select * from hospitals where lattitude > TARGET_LATTITUDE - 0.32 and lattitude < TARGET_LATTITUDE + 0.32 and longitude > TARGET_LONGITUDE - 0.32 and longitude < TARGET_LONGITUDE + 0.32

This gives us our definites

5. Putting it all together we can find a working set of maybe's that we must test with this query

select * from hospitals where
lattitude > TARGET_LATTITUDE - 0.45 AND lattitude < TARGET_LATTITUDE - 0.32 AND lattitude > TARGET_LATTITUDE + 0.32 and lattitude < TARGET_LATTITUDE + 0.45 AND
longitude > TARGET_LONGITUDE - 0.45 and longitude < TARGET_LONGITUDE - 0.32 AND
longitude > TARGET_LONGITUDE + 0.32 and longitude < TARGET_LONGITUDE + 0.45

You can now apply the distance formula to the result set and add the results to the results from the query in step 4
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
If you are working with MySQL then you can do the full calculation in the query

http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Please see this article, where the design pattern and PHP+MySQL code demonstrates how this is done.  (You may want to update to MySQLi or PDO.)
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_4276-What-is-near-me-Proximity-calculations-using-PHP-and-MySQL.html
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:krushnac
Comment Utility
Hi there
i got the solution. thanks for the effort .
0
 
LVL 1

Author Comment

by:krushnac
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for krushnac's comment #a39234058

for the following reason:

I got the solution.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
The close request cites this comment as the solution, but the comment does not contain enough information for us to understand how you solved your problem.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28149228.html#a39234058

Please post your solution and show us how you got the solution.  If any of the responses or articles here were at all helpful, it would be appropriate to award points.

Thanks, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
My recommendation is for the author to post the solution, hopefully with enough detail that we can see if there is original art in the solution.

Absent that, delete no refund since there is nothing new here.
0
 
LVL 1

Accepted Solution

by:
krushnac earned 0 total points
Comment Utility
here is the mysql query
SELECT * , ((
ACOS( SIN( ".$lat." * PI( ) /180 ) * SIN( lat_centroid * PI( ) /180 ) + COS( ".$lat." * PI( ) /180 ) * COS( lat_centroid * PI( ) /180 ) * COS( (
 ".$long." - long_centroid) * PI( ) /180 ) ) *180 / PI( )) *60 * 1.1515) AS `distance` FROM ".$this->table." HAVING distance <=100 ORDER BY distance LIMIT 0 , 10

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now