php posts

Draw a Box Around a Coordinate In Google Maps Based on Miles or Kilometers

Attached File(s): 

This post explains how, given latitude and longitude coordinates in Google Maps (specifically, in the Static API), how one can draw a bounding box around them, based on a distance in either miles or kilometers.

gmap_bounding.pngLet's say you already know the lat/long of a particular point on the map, and now you want to draw a box around it, or maybe you just need to know the lat/long coordinates of another point, so many km or miles away?  Well, this is the script for you!

Based on a script retrieved from here:
http://www.sitepoint.com/forums/showthread.php?656315-adding-distance-gps-coordinates-get-bounding-box

The trick is that Google Maps wants you to provide it with the lat/long coordinates for each corner of the box.  After several hours of searching, I finally found a script which I was able to modify to give me what I needed.

Simply download the script linked at the top of the page, and then use the following code to create the image you see here. 

Enjoy!


    $lat = -33.872986011907216;
    $lng = 150.90904516601563;

    // Create the static map api image.
    $static_maps_url = "http://maps.googleapis.com/maps/api/staticmap";
    $static_maps_url .= "?center=$lat,$lng";
    $static_maps_url .= "&zoom=15";
    $static_maps_url .= "&size=300x300";
    $static_maps_url .= "&maptype=roadmap";
    $static_maps_url .= "&sensor=false";
    $static_maps_url .= "&markers=color:blue|$lat,$lng";

    // Figure out the corners of a box surrounding our lat/lng.
    $d = 0.3;  // distance
    $path_top_right = bpot_getDueCoords($lat, $lng, 45, $d);
    $path_bottom_right = bpot_getDueCoords($lat, $lng, 135, $d);
    $path_bottom_left = bpot_getDueCoords($lat, $lng, 225, $d);
    $path_top_left = bpot_getDueCoords($lat, $lng, 315, $d);
   
    $static_maps_url .= "&path=color:334433|weight:5|fillcolor:0xFFFF0033|";
    $static_maps_url .= "$path_top_left|$path_top_right|$path_bottom_right|";
    $static_maps_url .= "$path_bottom_left|$path_top_left";
     
    // Now, draw the image from Google Maps API!
    print "<img src='$static_maps_url'>";


 

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Encode Any String to Only Alphanumeric Chars - Better Than URLEncode

Have you ever been faced with a situation where you need to encode a string with all sorts of problem characters into a URL-safe string, but, for whatever reason, urlencode() just won't do the job? 

What you need is a function that will give you nothing but numbers and letters-- no %'s or &'s, spaces, or any other non-alphanumeric character.

Well, look no further that PHP's bin2hex() function, and its pack() function.

It can convert this tricky string:

(!X>4Ob=h/&hN\'

Into this much nicer string, which can easily pass through a URL, MySQL query, XML tags, etc, since it is guaranteed to only ever be letters and numbers:

2821583e344f623d682f26684e5c27

And then decoding is a snap.  Here are the functions you need:

function hex_encode($input) {
  return bin2hex($input);
}

function hex_decode($input) {
  return pack("H*", $input);
}

Enoy!

Oracle_query() and Other Oracle Convenience Functions

Attached File(s): 

If you're used to using MySQL with PHP, you already know of the simple and easy to use mysql_query(), mysql_fetch_object(), mysql_affected_rows() and so forth functions.  Well, if you've ever dipped your toes into the tepid waters of Oracle, you know that it lacks the friendly PHP functions that MySQL has.  So, I created a set of convenience functions designed to mirror the MySQL ones.

Just download the attached file at the top, and use it like so:

<?php
  
  include_once("oracle_common_functions.php");

  oracle_connect($username, $pass, $host, "TEST");
  $res = oracle_query(" SELECT * FROM $table_name ") or echo(oracle_error());
  while ($cur = oracle_fetch_array($res)) {
    var_dump($cur);
  }  
  
  oracle_close();
  
?>

Just remember that to use any of this, you also need PHP to be compiled with the necessary oci libraries.

Complete list of functions in the file:

oracle_connect()
oracle_query()
oracle_fetch_array()
oracle_fetch_object()
oracle_affected_rows()
oracle_close()
oracle_error()
oracle_errno()
oracle_escape_string()

Enjoy!

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Find the True URL of Something, Following Redirects

Have you ever wanted to know the definitive URL to something on the Internet, following any 301 redirects?  I am working on a podcast-related site right now, and recently had to keep track of a multitide of podcast URL's, even when they change.  The following PHP code saved the day:

function get_final_url($url) {
 
  $ch = curl_init($url);
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  curl_exec($ch);
  $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  curl_close($ch);  
 
  return $url;
}

To use, let's say you have a url of "http://example123.com/index.html"  but, unbeknownst to you, that URL actually has a 301 redirect to "http://example123.com/welcome.html". 

You can use the function this way:

$url = get_final_url("http://example123.com/index.html");

And $url will now contain "http://example123.com/welcome.html".

Enjoy!

Amazon AWS S3 Query String Authentication with PHP

Okay, this one took me hours to figure out, so I figured I would put my results here for others to find.  Hopefully it will spare others the hours of frustration I went through!

If you do not know what Amazon AWS S3 is, you should read up on it.  Basically, it's super-cheap online file storage for your web site or even personal use.  You can even make it so that files are private, and can only be downloaded with a self-expiring URL (which might only be valid for, say, 1 hour).

Here is how, in PHP, to do just that.  Please note, these code snippits were pieced together from various sources online until I finally got everything working the way it is supposed to.

Collapsible Fieldsets Without jQuery

Attached File(s): 

c_fieldset.jpgYou know those nifty collapsible fieldsets that Drupal uses?  No?  Oh... well, let me explain: Drupal often uses fieldsets to represent information, which can collapse at the click of a mouse.  This lets visitors to your site hide information or forms they don't care about.  It's accomplished using some jQuery wizardry and is uber slick.

What I have created does not use jQuery, and is not uber slick.  But, it does provide the same basic functionality.  The reason I chose not to use jQuery is so that you are not required to download and install any extra libraries.

What I have created is a simple PHP function which, when called, will generate all of the HTML, CSS, and Javascript necessary to give you a collapsible fieldset.

Click here to see a demo of it in action.  (And use the download link at the top of this post to get the source code).

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Meet My Friend, Mr. Curly Brace

The curly brace (also known as the curly bracket) is a fairly powerful tool in PHP.  In a nutshell, it lets you specify the name of a variable in a string.  Why is that so important?  Because what if you want to specify a member variable to an object in another variable?  Maybe an example would elucidate the problem...

Let's say you want to compare apples to oranges in a standard object...

$obj = new stdClass();
$obj->apples[] = "Granny";
$obj->apples[] = "Red";
$obj->apples[] = "Bad";

$obj->oranges[] = "Navel";
$obj->oranges[] = "Tangerine";

Now, let's say that you need to access either the apples array or the oranges array based on some other variable, $fruit

PHP Tip: Sorting Multidimensional Associative Arrays

We've all faced this problem before.  You have a multidimensional associative array, and you need to sort it by some element right in the middle.  Or even sort by several elements.  How do you do it?  Well, here's a quick tip that will keep you from having to program custom comparison routines to get usort() to work.

For example, let's say your array looks something like this:

// array structure:  $a[person name][age][pet name] = pet age

$a["mary"][35]["spot"] = 5;
$a["bob"][29]["nosy"] = 8;
$a["mary"][35]["fido"] = 12;
$a["bob"][29]["eve"] = 2;

Now let's say you want to know the order of pet names, sorted by owner's age, then pet age, then person name, then pet name.  If we go the traditional usort route, we would have to program several annoying and confusing comparison functions.  So, here's the trick:  flatten out the array!

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Syndicate content