| Timothy D. Ginn |
18-06-2005 17:45 |
Re: FIRST Team Map
Quote:
Originally Posted by DarkJedi613
Wow...thanks a lot...
Maybe in the next version of Team Lookup I'll have it generate coordinates and just put it in the file w/ them. :D
I hadn't really looked into Google APIs too much, though I think there is an API for their Map service now...but yeah the way you're saying would be easier.
To get the coordinates did you just feed it into the search engine from multimap then load the entire page as a string using php and search for the x-coord and y-coord or do they have a built in thing to find coordinates?
You just gave me something to play with now! Thanks. :)
The image in this thread is created w/ PHP but they you saved it, correct? As in it doesn't regenerate everytime...
Thanks a lot.
|
I checked the Google API documentation just now and didn't see any mention of an API for Maps. Oh, and yes, I did save the images that I created with PHP; they're not regenerating every time :-)
As for how I did things
For Multimap I wrote (in PHP):
PHP Code:
/*
* Returns the geographic coordinates of a particular street address
* Return value:
* Array(
* "lon" = longitude coordinate (decimal #)
* "lat" = latitude coordinate (decimal #)
* )
*
* Defaults to no street address, city of Kingston, province of Ontario, country of Canada
*/
function getCoordinates($streetaddr = "", $city = "Kingston", $province = "ON", $country = "CA") {
// define variable to be used to store coordinate results.
$result = array();
// Multimap requires things be trimmed and urlencoded to best match.
$streetaddr = urlencode(trim($streetaddr));
$city = urlencode(trim($city));
$province = urlencode(trim($province));
$country = urlencode(trim($country));
// Try checking database to see if we've already looked this address up
// (if we have, read it from the database, otherwise query Multimap).
$q = mysql_query("SELECT longitude, latitude FROM geoLocations WHERE streetAddress='$streetaddr' AND city='$city' AND province='$province' AND country='$country' LIMIT 1;");
if(mysql_num_rows($q) == 0) {
// Free up the result (to save memory).
mysql_free_result($q);
// Connect to Multimap to retrieve coordinate information
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.multimap.com/map/places.cgi?client=public&lang=&advanced=&db=" . $country . "&cname=Great+Britain&overviewmap=&addr2=" . $streetaddr . "&addr3=" . $city . "&state=" . $province . "&pc=");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$tmp = curl_exec($ch);
curl_close($ch);
// Expand the string into an array so that we can extract longitude and latitude
$res = explode("&", $tmp);
// Prepare result to return
$result = array("lon" => 0, "lat" => 0);
$result["lon"] = str_replace("lon=", "", $res[3]); // longtidude
$result["lat"] = str_replace("lat=", "", $res[4]); // latitude
// Enter the coordinates into the database (to save having to query Multimap for them again, later)
$q = mysql_query("INSERT INTO geoLocations (streetAddress, city, province, country, longitude, latitude) VALUES ('$streetaddr', '$city', '$province', '$country', '" . $result["lon"] . "', '" . $result["lat"] . "');");
} else {
// The position is in our database already, so we can look it up from there
$res = mysql_fetch_object($q);
// Prepare result to return
$result = array("lon" => 0, "lat" => 0);
$result["lon"] = $res->longitude; // longtidude
$result["lat"] = $res->latitude; // latitude
// Free up the result (to save memory)
mysql_free_result($q);
}
// Return result
return($result);
}
This caches the results in the database structure:
Code:
CREATE TABLE `geoLocations` (
`streetAddress` varchar(255) NOT NULL default '',
`city` varchar(255) NOT NULL default 'Kingston',
`province` char(2) NOT NULL default 'ON',
`country` char(2) NOT NULL default 'CA',
`latitude` decimal(10,4) NOT NULL default '0.0000',
`longitude` decimal(10,4) NOT NULL default '0.0000'
) TYPE=MyISAM COMMENT='Stores geographical information about addresses'
You may also be interested in this (which in my implementation I put in a class called GeoLocate with the above function)...
Note: The reason it's not a simple typical equation to find distance (eg.  ) is mainly that the Earth is (roughly) spherical. For those who care, it's not precisely spherical and therefore the below function is an approximation, but, it seems to me to be more than good enough for all of my purposes:
PHP Code:
/*
* Returns the distance (in kilometers) between two geographical coordinates
*
* Coordinates are given in the form:
* Array(
* "lon" = longitude coordinate (decimal #)
* "lat" = latitude coordinate (decimal #)
* )
*
*/
function getDistance($position1, $position2) {
// See: http://jan.ucc.nau.edu/~cvm/latlon_formula.html for an explanation of this formula
// The formula requires that everything be in radians. Assumption: The standard coordinates are always in degrees.
$a1 = deg2rad($position1["lat"]);
$b1 = deg2rad($position1["lon"]);
$a2 = deg2rad($position2["lat"]);
$b2 = deg2rad($position2["lon"]);
$r = 6371.01; // Radius of the Earth (in kilometers)
// From: http://ssd.jpl.nasa.gov/phys_props_earth.html
// Return the distance between the two given points (in kilometers)
return acos(cos($a1)*cos($b1)*cos($a2)*cos($b2) + cos($a1)*sin($b1)*cos($a2)*sin($b2) + sin($a1)*sin($a2)) * $r;
}
If anyone else wants to use these; that's fine by me, but I'd very much like to hear what you use it for :-) Oh, and if you're going to create derivative works, etc. I'd like a bit of credit. After all, it's fun to play around with things; but it's definitely nice to be acknowledged for producing things that other people find helpful.
Anyway, that should be more than enough to tinker with; have fun :-)
|