Calculate zipcodes in range - php

I have a website where users start by entering their zipcode.
I have a table with different shops which also contain the zip codes belonging to the shops.
Is it possible to get the zip codes within let's say a 5 km radius of the zip entered by the user?
So I can query the database and find all the shops near the user.
Can I use Google Maps for this? Should I get my hands on a pre-computed list of all zipcodes (single country) and their coordinates? Any other suggestions?

you can store longitude/latitude for each zipcode. That data is available for free on the net , one such is here http://www.boutell.com/zipcodes/. Then you would find the zipcodes in the radius by using great circle distance function which is discussed here MySQL Great Circle Distance (Haversine formula)

Google has a tutorial on this that should point you in the right direction. The bit of trigonometry in the SQL query is the only tough part. At first glance, there are basically two ways to go about this:
Get the lat/long coordinates for each store. You can do this using Google Maps' Geocoding API by providing the street address for the store. You'd only need to do this once for each location.
Only having the zip code for each store, and using a table of zip codes with geometrically-centered lat/long coordinates for each. Find the zip codes that are within range and then show the stores with those zip codes.
The first would be more accurate, but either works. If you're calculating distance from a zip code rather than an address, you'd still need to look up the lat/long coordinates for that zip code.

You'll need that latitude and longitude of each zipcode, and then use the Haversine Formula to get the approximate distance.

you need to get latitude/longtitude from the zipcode and then you can find places near by via maps or geoplaces api. head over to google maps api or YQL geo api.

Related

Search database for locations within polyline coordinates

I am using Google Maps to display locations stored in a MySQL database containing name, latitude and longitude information. An AJAX request is sent and the map is updated with all the locations. So far, so good.
Users have the ability to filter the locations by drawing polylines on the map to create a bounding box. The idea is that only locations within this bounding box are returned and they can draw whatever shape they want.
All the points used to draw the shape are returned a list of coordinates. Where I am struggling is taking the list of coordinates and finding everything within them. Can I achieve this with an SQL query? Is it even possible?
I have seen examples of returning locations within x radius from one set of coordinates and also a square box, but I have no idea how to take that further and get everything within a potentially infinite set of coordinates.
You should transform the shape the user enters into a polygon. Then you transform all locations within a square around this polygon into the same coordinate system.
Using a solution describe in the wikipedia page for point-in-polygon problems you will be able to find all locations within this shape and remap them to coordinates in your sql database and then into locations.
There are however similar question which might help you find a proper solution to your problem. See:
How can I determine whether a 2D Point is within a Polygon?
Point in Polygon Algorithm

API to get Center point zip code of a City

I am looking for an API that will give me all the zip codes within a 20 mile radius of a given US city. So far based on my online search, I could only find APIs that will give me all the zip codes with a 20 mile radius for a given zip code. Hence I’m trying to find an API that will (a) first find the zip code of the center point of a city, and then (b) use the API that I referred above to pass the zip code and the distance (e.g. 20 miles) to get all the zip codes within that 20 mile radius.
So I’m looking for (a) above, which is an API that will return the zip code of the center point of a city. This is all for US.
You can use the Google Geocoding API to get the location coordinates of a city, then use reverse geocoding to look up the actual zip code for that location.
Bear in mind if you get this data from Google you need to use it on a Google Map and are not supposed to use it outside of that.
Use the Geocoding API from Google to first map the city to the corresponding lat-long pair. Then use the Geonames API to get the postal codes in nearby radius. Use the following format for the Geonames API
http://api.geonames.org/findNearbyPostalCodes?lat=47&lng=9&username=demo
Find more details here
If you want a flat file instead, to maintain the data yourself, the USGS Gazetteer Populated Places file has really good city centroid coordinates and is free to use.

List points within a certain radius of a location?

I'm making an API for an iPhone app. Put simply, I have locations stored in a database (Lat/long). When a user send a request for points within, let's say, a five (5) mile radius, I'll need to make use of an algorithm to only give me those points from a MySQL database.
How can I convert miles to minutes in a coordinate? IE, with the tapering nature of longitude in mind, how much do I add to coordinates I'm searching through to compare to the users location?
2 What's a good formula to take into account the way longitude/latitude changes as you go to different points of Earth?
Thanks :)
You never mentioned how you stored the data, so I can't give you the actual code. Here's just something I came up with:
Import all the data into some efficient database. Optimize it to be efficient at pulling up coordinates within a certain X/Y range
Select all the coordinates within a 10x10 mile square. Import them into an array
Use a foreach to go through that array and use the distance formula (distance=sqrt((x-x)^2+(y-y)^2)). If within 5 miles, add it to another array. If not, ignore it.
I don't know how efficient this would be with thousands of coordinates in a small location; you might have to fine tune it (for example, try a smaller box first if you're finding the closest location).
I do have to agree with Darren that Google Maps API might be better suited, although I haven't used it before.

Google Maps API area search

I have a set of adresses / coordinates. In my software the user should be able to search for a specific adress or coordinate and find the surrounding places of the set of coordinates in a specific radius. The results should be shown on a map.
(Mostly like Google Places for Restaurants just with my own set of places)
Displaying the places on a static map is not the problem, but handling the set of addresses (~5000) and calculating the distances to the place searched for.
How can I establish a function like this using Google's APIs?
Thanks!
You can use the harvesine formula and a bounding box. Or you can use a spatial index and a bounding box. The harvesine formula is used for spheres. Most likely you use straight distance (like the crow flies).

How could I build a statistical map?

I'm trying to figure out how to build a statistical map for my web app. Here's what I've got:
I have a MySQL database of zip codes, and each zip code has latitude & longitude.
I have users who have declared what zip code they live in.
I even have a haversine query which will show how many users exist within, for example, 25 miles of a given latitude/longitude, based on their zip code.
My question is this: Using this information, how could I approach building a statistical map for a web application using PHP?
I would be fine with using just a US map or even a North American map for now, but I'm just not sure how to build that map. Some options I've considered:
Show a colored dot on the map, larger or smaller depending on the number of users near that location. I'm not sure how to do this, though, especially if those dots were to overlap!
Show individual "pushpins" where the users are. Seems like this could get out of hand if my user base grows significantly
So back to my question. If I had 300 users in Dallas, 4,000 in NYC, 45 users in Detroit, 403 in Chicago... how would I be able to represent that on a map -- and also how would I draw that map in a web application built on PHP?
You are trying to build a three-dimensional (probably even more dimensions) data display.
Your dimensions are:
X-Location
Y-Location
The value at every location
This really does not define anything about the visual appearance, though.
A simple approach might be to calculate the absolute number of users per state and then color the state on the map according to some scale. You also might calculate the percentage of users living in a state compared to the absolute number and color that instead.
A different approach would be to put a dot for every user on the map, and if this dot was printed before, to change it's color instead, e.g. make it brighter.
In the end, it really depends on what your actual data is and if your approach on visualizing it displays some significant information - but this can only be confirmend after you see it.
As you are looking for a web application have you considered Google Maps. Factor 1. can be implemented using the MarkerClusterer library. A DEMO showing this. The data from your database can be loaded using AJAX.

Categories