chetanmadaan
07-20-2010, 05:09 PM
Hi,
i am looking for a php script where With the location search, would it be possible to search within in a range of say 50 miles from a certain zip code? Is it possible to set up the search so that they can search by more than one criteria at a time? For example, if I want to find a location within 50 miles of a certain zip code that has some specific criteria already described in the database.
is this something possible with the google maps api integration where we can take the zip codes miles distance from them an then integrate it into our's.
thanks
C.
here is what i found so far.
http://www.mattcutts.com/blog/fun-with-zip-codes/
bluewalrus
07-20-2010, 07:23 PM
Yes, the google maps API can do this. I'll post you a sample code in a little bit that takes an inputted address and checks the database for the closest address to it within the specified users traveling distance.
chetanmadaan
07-20-2010, 07:24 PM
ah... thanks for your reply... that sounds great.
Waiting.
bluewalrus
07-20-2010, 07:58 PM
Okay this'll be kinda confusing (maybe?) and may not work (deleted some things I think were irrelevant) so post back any questions you have. This was also written for ms sql.
This page was passed from a form with a get request where the address value had the latitude and longitude values in it like (,).
So
http://www.domain.com/site.php?address=(76.56,-89.45)
Random values there not sure if will work just as an example. I used the geocoder to convert from an address to those points if you need that example as well let me know.
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
$increase = 0;
$increase2 = 0;
$statment = 0;
$travel_distance = 60;
$key = "alsadkf234234k23l42j3kf";
$url = $_SERVER['REQUEST_URI'];
$location = explode( "?address=", $url );
$location = str_replace("%20", " ", $location[1]);
$location = str_replace("%29", ")", $location);
$location = str_replace("%28", "(", $location);
$location = str_replace("(", "", $location);
$location = str_replace(")", "", $location);
$location = explode( ",", $location);
$location_lat = $location[0];
$location = explode( "&usernot=", $location[1]);
$notuser = $location[1];
$location_lng = $location[0];
$tsql = "Select Name, Address, City, State, Country, lat, lng from videographers_info";
include 'DBConnection.php';
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false) {
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src="http://maps.google.com/maps?file=api&v=2&key=<?php echo $key;?>" type="text/javascript"></script>
</head>
<body onload="load();">
<div class="float">
<h1>Auto Find Closest Videographer.</h1>
<?php
while (sqlsrv_fetch( $stmt )) {
$Name[]= sqlsrv_get_field( $stmt, 0, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR));
$Address[] = sqlsrv_get_field( $stmt, 1, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR));
$City[] = sqlsrv_get_field( $stmt, 2, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR));
$State[] = sqlsrv_get_field( $stmt, 3, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR));
$Country[] = sqlsrv_get_field( $stmt, 4, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR));
$user_lat[] = sqlsrv_get_field( $stmt, 5);
$user_lng[] = sqlsrv_get_field( $stmt, 6);
$increase++;
}
while ($increase > $increase2 ) {
$value[] = 3959 * acos( cos((( $location_lat* 3.14159 )/180)) * cos((( $user_lat[$increase2] * 3.14159 )/180) ) * cos((($user_lng[$increase2]* 3.14159 )/180) - (($location_lng* 3.14159 )/180) ) + sin( (($location_lat* 3.14159 )/180) ) * sin((( $user_lat[$increase2] * 3.14159 )/180))) ;
$increase2++;
}
$val=min($value);
$key=array_search($val, $value);
$total = substr($value[$key],0,6);
if (($travel_distance - $total ) < 0 ) {
$outside = "This location is outside of any users area.";
echo $outside;
}
echo "The closest user is $total miles from the location. That user is at $Address[$key] <br />, $City[$key], $State[$key], $Country[$key]";
?>
<script type="text/javascript">
//<![CDATA[
var map;
var point;
var marker;
var bounds = new GLatLngBounds();
var blueIcon = new GIcon(G_DEFAULT_ICON);
blueIcon.image = "images/blue.png";
markerOptions2 = { icon:blueIcon };
var redIcon = new GIcon(G_DEFAULT_ICON);
redIcon.image = "images/red.png";
markerOptions = { icon:redIcon };
var opacity = 0.4;
var circle;
var centerMarker;
var circleUnits = "MI"; // assuming miles
var circleRadius = "50"; //assuming 50 miles Unites are above
// On page load, call this function
function load()
{
// Create new map object
map = new GMap2(document.getElementById("map"));
try
{
var endlocation = new GLatLng(<?php echo $user_lat[$key];?>, <?php echo $user_lng[$key];?>);
var startlocation = new GLatLng(<?php echo $location_lat;?>, <?php echo $location_lng;?>);
marker = new GMarker(endlocation, markerOptions);
marker2 = new GMarker(startlocation, markerOptions2);
// Add the marker to map
map.addOverlay(marker);
map.addOverlay(marker2);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
var bounds = new GLatLngBounds();
bounds.extend(startlocation);
bounds.extend(endlocation);
var connectpoints = new GPolyline([startlocation, endlocation], "#ff0000", 5);
map.addOverlay(connectpoints);
var miledistance = startlocation.distanceFrom(endlocation, 3959).toFixed(1);
var kmdistance = (miledistance * 1.609344).toFixed(1);
function doDrawCircle(){
if (circle) {
map.removeOverlay(circle);
}
if (centerMarker) {
map.setCenter(centerMarker.getLatLng())
}
var circlePoints = Array();
var d = circleRadius/3963.189; // radians
with (Math) {
var lat1 = (PI/180)* endlocation.lat(); // radians
var lng1 = (PI/180)* endlocation.lng(); // radians
for (var a = 0 ; a < 361 ; a++ ) {
var tc = (PI/180)*a;
var y = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc));
var dlng = atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(y));
var x = ((lng1-dlng+PI) % (2*PI)) - PI ; // MOD function
var point = new GLatLng(parseFloat(y*(180/PI)),parseFloat(x*(180/PI)));
circlePoints.push(point);
bounds.extend(point);
if (d < 1.5678565720686044) {
circle = new GPolygon(circlePoints, '#000000', 2, 1, '#000000', 0.25);
}
else {
circle = new GPolygon(circlePoints, '#000000', 2, 1);
}
}
map.addOverlay(circle);
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
// If line below is uncommonted shows videographers full travel distance
// If uncommented shows the two points
}
}
doDrawCircle();
}
catch (error)
{
alert(error);
}
}
</script>
</div>
<div id="map"/>
</body>
<?php
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
</html>
I used part of this article to code this as well http://code.google.com/apis/maps/articles/phpsqlsearch.html
chetanmadaan
07-20-2010, 08:01 PM
helped and didn't helped. will let you know when i use it.
thanks for the fast reply.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.