Fusion Table Layers
JS:
var initial = document.getElementById('address').value;//initial value is zip codes separated by commas and no space between:: example:40475,40489,44456
$.ajax({
type:"GET",
url: "query.php?zipcode="+encodeURI(initial),
dataType: 'json',
success: function(data) {
map.data.forEach(function (feature) {
map.data.remove(feature);
});
map.data.loadGeoJson('google.json');
map.data.addGeoJson(data);
map.data.setStyle({
icon: '//example.com/path/to/image.png',
fillColor: "#FF0000",
strokeColor: "#FF0000",
fillOpacity: 0.2,
strokeOpacity: 0.6,
strokeWeight: 3
});
}
});
INIT of Google Map:
function initialize() {
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: new google.maps.LatLng(34.0500,-118.2500),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
layer[0] = new google.maps.FusionTablesLayer({
query: {
select: 'geometry',
from: yourtableid
},
templateId: 2,
styleId: 2
});
layer[0].setMap(map);
google.maps.event.addListener(map, "bounds_changed", function() {
displayZips();
});
google.maps.event.addListener(map, "zoom_changed", function() {
if (map.getZoom() < 11) {
for (var i=0; i<labels.length; i++) {
labels[i].setMap(null);
}
}
});
}
query.php
query($query)){
while ($row = $result->fetch_assoc()) {
$array['features'][$count]['type'] = 'Feature';
$array['features'][$count]['properties']['zipCode'] = $row["zipcode"];
$array['features'][$count]['properties']['state'] = $row["state"];
$MultiPolygon = explode('MultiPolygon', $row['geometry']);
$xml = simplexml_load_string($row['geometry']);
if(!$xml->Polygon[1]){
$array['features'][$count]['geometry']['type'] = 'Polygon';
$geometry = trim(preg_replace('/\s+/', ' ', $row['geometry']));
$geometry = strip_tags($geometry);
$geometry = explode(',0.0', $geometry);
$geometry = array_diff( $geometry, array( ' ' ) );
for ($i=0; $i < count($geometry); $i++) { // $array['features'][$count]['geometry']['coordinates'][0][$i][] = $geometry[$i]; $coor = explode(",", $geometry[$i]); $array['features'][$count]['geometry']['coordinates'][0][$i][] = floatval($coor[0]); $array['features'][$count]['geometry']['coordinates'][0][$i][] = floatval($coor[1]); } $count++; }else{ $array['features'][$count]['geometry']['type'] = 'MultiPolygon'; $multicount = 0; foreach ($xml->Polygon as $multi) {
$geometry = (string)$multi->outerBoundaryIs->LinearRing->coordinates[0];
$geometry = trim(preg_replace('/\s+/', ' ', $geometry));
$geometry = explode(',0.0', $geometry);
$geometry = array_diff( $geometry, array( '' ) );
for ($i=0; $i < count($geometry); $i++) { $coor = explode(",", $geometry[$i]); $array['features'][$count]['geometry']['coordinates'][$multicount][0][$i][] = floatval($coor[0]); $array['features'][$count]['geometry']['coordinates'][$multicount][0][$i][] = floatval($coor[1]); // $array['features'][$count]['geometry']['coordinates'][$multicount][0][$i][] = $geometry[$i]; } $multicount++; } $count++; } } echo json_encode($array); $result->free();
}
$conn->close();?>
?>
More Information
A active Database to use of all zip codes, counties,states,population and their coordinates to map please check out Zipnio.com