By : sublantic Published On Sunday, January 30, 2011, 08:57 In PHP Scripts
This is a collection of location based functions that can get a users location based on their ip address using the IPInfoDB api, return advanced details on a specific location with the Yahoo PlaceFinder api, or dynamically create maps (as images or with javascript) using the Google Maps api.
This function interacts with the IPInfoDB.com API to get a users location based on their IP address. It can return city, state, country, latitude & longitude and is accurate within a 25 mile radius. This function accepts an IP Address and returns an array of location based values.
$location = $location->ip ($_SERVER['REMOTE_ADDR']); // Values returned $location['country']; $location['country_code']; $location['state']; $location['city']; $location['post_code']; $location['latitude']; $location['longitude'];
See below on how to generate maps
This function uses the Yahoo PlaceFinder API to get detailed information about a specific location. This is the way to go if you want to get more detailed information or find a more exact location. It accepts a textual location (zip code, city/state, street address, latitude/longitude, etc) and returns an array of location based values.
$info = $location->info ('400 Broad St Seattle, WA 98109');
// Values returned
$info['country'];
$info['country_code'];
$info['state'];
$info['state_code'];
$info['county'];
$info['city'];
$info['post_code'];
$info['cross_street'];
$info['house'];
$info['street'];
$info['radius'];
$info['quality'];
$info['woeid'];
$info['latitude'];
$info['longitude'];
Country: United States
Country Code: US
State: Washington
State Code: WA
County: King County
City: Seattle
Post Code: 98109
Cross Street: Near the intersection of Broad St and John St
House: 400
Street: Broad St
Radius: 500
Quality: 87
Where on Earth ID: 12798953
Latitude: 47.619859
Longitude: -122.348749
Use this to automatically generate Google Maps for a specific location. This function accepts a number of parameters:
The first example uses javascript to generate a dynamic map. To generate a map with javascript you’ll need to pass both the latitude and longitude coordinates to the location parameter (the latidude and longitude can be retreived with either the ip location or place finder functions descriped above). The map is injected into a div with an id of “map_canvas” by default (whose height/width can be modified via css). If you want to set the width & height with css instead of via the function, pass “css” to both the width and height parameters.
$coordinates = $info['latitude'].','.$info['longitude']; echo $location->map($coordinates, 'javascript', 'hybrid', '100%', '300px', '16', 'map_1');
Currently the map function in only able to add one javascript map per page. If you need multiple maps (or just want to generate an image instead) simply set the second parameter to the type of image you want returned (jpg, jpg-baseline, png, png32 or gif). You can set the dimensions to anything you like, but Google’s maximum allowed image size is 630 by 630 pixels. A few examples of different image settings follow…
echo $location->map($coordinates, 'png32', 'roadmap', '630px', '630px', '11', 'map_2');

echo $location->map($coordinates, 'jpg', 'satellite', '630px', '200px', '7', 'map_3');

echo $location->map($coordinates, 'gif', 'terrain', '200px', '200px', '5', 'map_4');

echo $location->map($coordinates, 'png', 'satellite', '630px', '200px', '1', 'map_5');
