aspose file tools*
The moose likes Other Open Source Projects and the fly likes Look For Specific Zip Code/Office Location near Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Look For Specific Zip Code/Office Location near "inputted" Zip Code" Watch "Look For Specific Zip Code/Office Location near "inputted" Zip Code" New topic
Author

Look For Specific Zip Code/Office Location near "inputted" Zip Code

Jeremy McNally
Ranch Hand

Joined: Feb 21, 2014
Posts: 92
I am having the hardest time finding a way to do this. I am looking into Google Maps API, Geocoding and so on but is there an easier way to make this happen?

My goal is to input certain locations in a specific zip code in every state in the USA. If I have a location in Boston, MA let's say but someone types in a Zip Code "near" Boston, how can I match that zip code to the nearest office location of that zip code (by the zip code)... and display it back to the user for example? Maybe even not display it back to the user, just retrieve the Boston Location (or nearest office location to the inputted zip code) in my program and use it to do something...? (The office shall be searched by zip code)...

This is a major problem and is a very urgent matter to figure out. If anyone can provide help to me and examples in code perhaps (database / Java / etc...) I would be very appreciative. Thank you in advance for your responses to this topic. Thank you.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2434
    
  28

First of all, EaseUp

Secondly, do you need the nearest distance as the crow flies, or do you need the nearest distance by car? To find the distance between 2 zipcodes, a very naive implementation can be that you can look up lat long of both zipcodes using the Geocoding API, and then compute the distance between the 2 lat longs.

To compute distance by car, the distance matrix api provides a ready made service to find the distance by address.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

The Google Maps Api (or some other mapping API) is your best bet. You can look up the geographic coords of places by name and then use a distance calculation. Be aware that linear calculations won't be very accurate as the Earth's surface is (somewhat) of a sphere. You can use API utilities or find an algorithm online.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeremy McNally
Ranch Hand

Joined: Feb 21, 2014
Posts: 92
I need the nearest office location per the 'input zip code' the user types in.

EX: Office in Rochester, NH. - <Rochester, NH Zip Code = 03867>
User enters zip code of Dover, NH. <Dover, NH Zip Code = 03820>

CONT'D EX: Another office location is in Manchester, NH which is much farther away so I need the closest office location to the zip code the user enters in.


Bear, I have been looking into Google Maps API, and I have never tried anything of this sort before so in terms of actually doing it I have no idea where to even start, end etc... Google Maps API from my research if I were to use Geocoding API, I would 'need' to have a map display otherwise its against terms of service. Also with Google Maps, if the site gets 25000 hits per day for 9 consecutive days, there is a pretty decent charge for using it. This is okay if that were really the case.

I really need solid advice on how to use something like this though. That is my main concern in reference to my issue listed above. I did leave examples of the mountain I need to climb to get through this. Thank you, you guys. I look forward to your reply and enjoy your day in the meantime.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

So you need something free? Yes, the Google Maps API needs a license for commercial use.

That's what I use so I haven't researched any totally free alternatives, but I imagine that they exist.

To do it all on your own, you'll need a way to look up the coordinates (latitude and longitude) of zip code centers, and you'll need (I guess) a database of the coordinates of the offices. The rest is just a matter of math (algorithms available on-line -- lots of trig!)
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2434
    
  28

What you need to do is get the lat/long of all the zipcodes in the US from Google Maps(or some sort of geocoding service) and store it in your database. Don't get it from Google Maps on every request. There are only 99K possible zip codes in the US. Even if you pull them out of Google Maps once a month, you won't reach their limit. Just do it over a week. Zip codes don;t change that often. Storing them shouldn't be a big deal too. There are only 42K zipcodes in the US. ANy database should be able to handle 42K records.

If you don't want to pull it out of Google, there are providers that will sell you this data from 30-40 bucks. It's not that costly
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Yeah, that's what I meant by "look up". I have exactly such a table in the DB (along with a lot of other stats like population and median income per zip code).
Jeremy McNally
Ranch Hand

Joined: Feb 21, 2014
Posts: 92
I do want to clarify, I am not concerned if it is free or not. I just need to come up with a solution to this problem. I am really worried about it because I am not good with Trigonometry. I am not sure how to do a Radius search based on a zip code to find the "Nearest" office location to what the user entered in for the zip code. I have a lot riding on this problem and I could use all the help I can get on it.

BEAR: Is there any example code you can show me so I have an idea on what to do. Something showing something very similar? Even a Pseudo-code version of it would be great. I just need a starting point in Java terms as I have no 'clue' where to begin.

Anything will help and I can honestly say that out of any other question I may have asked in this forum overall, this topic here is the 'most' important to me to get done. Thank you to Anyone who can give me some detailed help on this. Thank you.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2434
    
  28

Jeremy McNally
Ranch Hand

Joined: Feb 21, 2014
Posts: 92
Okay great that Pseudo example was great and cleared up a few blocks in my noggin. I have one issue with it though.

I need to search either in a circle or square. I would have to (instead of Point A to Point B) etc... I would need to gather all the offices within a (whatever) mile Radius of the inputted zip code. So to sum that up I am looking at searching in every direction at once from the center point (inputted zip code) and listing each office that may have come up in that search.

Maybe it would just be an expansion of the pseudo-code you left in your last post? Or is it something different maybe? Would you have a Pseudo example in those terms?
Jeremy McNally
Ranch Hand

Joined: Feb 21, 2014
Posts: 92
Or maybe I am mis-understanding it. I see where you have FOR EACH - office which would technically be every office in the database if you already did it according to the conditions I put in my last reply.
THEN basically I am finding the one with the shortest distance and assigning it to the variable I will use to do what I need with what ever office is stored there?

Did I sort of explain how your example would work; correctly?
Jeremy McNally
Ranch Hand

Joined: Feb 21, 2014
Posts: 92
I honestly think you just helped me very much so. I have the ultimate solution and will be coding it between now and the next couple days once I can get to that point. Please keep watching this post though because I might have another question on this topic BUT as for now, great example. Thank you... I took me a second to catch onto it comparing it to what I need but I think this structure will work very well.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Be sure to use the distance calculation that was linked to. Not only is the surface of the Earth not flat (it's not really a sphere either, but close enough), but the lines of longitude are not parallel the way that the lines of latitude are. Assuming a flat, Cartesian plane will introduce errors and discrepancies quickly.

I cannot share my code for this calculation as it is not open-sourced by my client.
Jeremy McNally
Ranch Hand

Joined: Feb 21, 2014
Posts: 92
Okay Bear. Just to clarify are you talking about the 'Andrew.hedges.name/experiments/haversine'?



If this is the code you mean, I am horrible with Trigonometry so is there anyway you can break this down for me in English!?

Thank you if you do. I just need to have a better understanding of what I would put where.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2434
    
  28

If you are horrible with Trig, you probably don't want to know how Haversine works. Just close your eyes and use it
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Bingo! I don't even pretend to know how it works. Just create a method that feeds it the coords and returns the result.
Jeremy McNally
Ranch Hand

Joined: Feb 21, 2014
Posts: 92
Okay well, I will just close my eyes and use it. Think I am going to take a few basic Trig classes / tutorials online. But yes, this is very complicated for me so I MAY be back!!!
 
 
subject: Look For Specific Zip Code/Office Location near "inputted" Zip Code