mo Posted November 5, 2010 Share Posted November 5, 2010 I am using the below script which works fine to query all regions in a specified radius of my input latitude and longitude. However my database has duplicate city/state pairs due to different zip codes and the query returns duplicate cities. I tried to use distinct but since the query has to be ordered by distance, I could not get this to work. Any suggestions. Note: Table contains columns city, region(state), country, zip, latitude and longitude. SELECT `city`, `region`, 3956 * 2 * ASIN(SQRT(POWER(SIN(( 37.4404 - abs(`latitude`)) * pi()/180 / 2),2) + COS(37.4404 * pi()/180 ) * COS(abs(`latitude`) * pi()/180) * POWER(SIN((-121.8705 - `longitude`) * pi()/180 / 2), 2) )) as distance FROM `locationdb` having distance < 20 ORDER BY distance limit 10 Quote Link to comment https://forums.phpfreaks.com/topic/217846-geo-search-using-haversine-formula-distinct-not-working/ Share on other sites More sharing options...
mo Posted November 5, 2010 Author Share Posted November 5, 2010 Here are my current results. I have logic in my PHP to loop through the result and remove duplicates but that is a sloppy way of removing the duplicates and since the cities are not in order, the logic does not always work. city region distance Ascending -------------------------------------------- Milpitas CA 0 San Jose CA 4.05605867292861 San Jose CA 4.27379165682789 San Jose CA 4.72803292240825 San Jose CA 5.10657017304142 Fremont CA 5.37310902047679 Santa Clara CA 5.86235168930009 San Jose CA 6.31906928616225 Alviso CA 6.57871062331007 Fremont CA 6.7615683089475 Quote Link to comment https://forums.phpfreaks.com/topic/217846-geo-search-using-haversine-formula-distinct-not-working/#findComment-1130672 Share on other sites More sharing options...
fenway Posted November 9, 2010 Share Posted November 9, 2010 Then before you order by distance, you need to "collapse" the city/state pairs -- and decide which distance to user. Quote Link to comment https://forums.phpfreaks.com/topic/217846-geo-search-using-haversine-formula-distinct-not-working/#findComment-1132254 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.