simplemaps

United States Cities and Zip Codes Database

We're happy to release an open source database of US zip codes, the associated city, the state and the latitude and longitude.

Source: 2000 Census. Formatted by Simplemaps.

License: MIT (attribution appreciated)

Format: SQL and CSV

Structure:


Download CSV Download SQL

Please Share on Twitter!



PHP Example of How to Query a Unique List of Cities from a String

//Replace with your database credentials
$username = "root";
$password = "";
$hostname = "localhost"; 

//What the user is searching for
$query = 'Minneapolis';

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "";
$selected = mysql_select_db("us_cities",$dbhandle) 
  or die("Could not select excel");
$query=mysql_real_escape_string($query);


//Query mysql database
$sql = "SELECT * FROM  `zips` WHERE  `city` LIKE  '%".$query."%'LIMIT 0 , 10";  
$result = mysql_query($sql);

//Create array of cities
$i=0;
$cities=array();
while ($row = mysql_fetch_array($result)) {
    $cities[$i]=$row{'city'}.", ".$row{'state'};
    $i++;
}

//Make sure they are unique
$out =array_unique($cities);

//The final array
var_dump($out);


JavaScript Function to Get the Distance Between Two Zip Codes (Source)

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}

Resources

Tools, freebies, and articles about mapping and web development. Things we've learned and want to share!

Free Maps

Tools

Articles

Links

© Simplemaps.com 2010-2015 | License | Privacy Policy | Releases | Testimonials | Resources | Documentation
Deprecated Maps: Flash USA Map | Flash World Map | Flash Canada Map | Flash Europe Map | Flash Congress Map
Formerly FlashUSAmap.com and Flashworldmap.com