Results 1 to 5 of 5

Thread: Search by Zip code with 50 miles of the zip code.

  1. #1
    Join Date
    Apr 2008
    Location
    Little Office!
    Posts
    80
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Search by Zip code with 50 miles of the zip code.

    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/

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Apr 2008
    Location
    Little Office!
    Posts
    80
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default

    ah... thanks for your reply... that sounds great.

    Waiting.

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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 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_rsqlsrv_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$stmt0SQLSRV_PHPTYPE_STRINGSQLSRV_ENC_CHAR));
                
    $Address[] = sqlsrv_get_field$stmt1SQLSRV_PHPTYPE_STRINGSQLSRV_ENC_CHAR));
                
    $City[] = sqlsrv_get_field$stmt2SQLSRV_PHPTYPE_STRINGSQLSRV_ENC_CHAR));
                
    $State[] = sqlsrv_get_field$stmt3SQLSRV_PHPTYPE_STRINGSQLSRV_ENC_CHAR));
                
    $Country[] = sqlsrv_get_field$stmt4SQLSRV_PHPTYPE_STRINGSQLSRV_ENC_CHAR));
                
    $user_lat[] = sqlsrv_get_field$stmt5);
                
    $user_lng[] = sqlsrv_get_field$stmt6);
                
    $increase++;
    }
    while (
    $increase $increase2 ) {
        
    $value[] =  3959 acoscos((( $location_lat3.14159 )/180)) * cos((( $user_lat[$increase2] * 3.14159 )/180) ) * cos((($user_lng[$increase2]* 3.14159 )/180) - (($location_lng3.14159 )/180) ) + sin( (($location_lat3.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 ) < ) {
        
    $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/art...sqlsearch.html
    Last edited by bluewalrus; 07-20-2010 at 08:04 PM.
    Corrections to my coding/thoughts welcome.

  5. The Following User Says Thank You to bluewalrus For This Useful Post:

    chetanmadaan (07-20-2010)

  6. #5
    Join Date
    Apr 2008
    Location
    Little Office!
    Posts
    80
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default

    helped and didn't helped. will let you know when i use it.

    thanks for the fast reply.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •