PHP Code:
<?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
Bookmarks