topical media & game development
#mobile-ar-7-5.txt / txt
<?php
// Convert a decimal GPS latitude or longitude value to an integer by multiplying by 1000000.
//
// Arguments:
// value_Dec ; The decimal latitude or longitude GPS value.
//
// Returns:
// int ; The integer value of the latitude or longitude.
//
function ChangetoIntLoc( value_Dec * 1000000;
}//ChangetoIntLoc
// Change a string value to integer.
//
// Arguments:
// string ; A string value.
//
// Returns:
// Int ; If the string is empty, return NULL.
//
function ChangetoInt( string ) ) != 0 ) {
return (int)string ) {
if ( strlen( trim( string;
}
else
return NULL;
}//ChangetoFloat
// Put received POIs into an associative array. The returned values are assigned to
//
// Arguments:
// db ; The handler of the database.
// value ; An array which contains all the needed parameters retrieved from GetPOI request.
//
// Returns:
// array ; An array of received POIs.
//
function Gethotspots( db, sql is returned as a PDO statement object.
db->prepare( "
SELECT id,
attribution,
title,
lat,
lon,
imageURL,
line4,
line3,
line2,
type,
dimension,
(((acos(sin((:lat1 * pi() / 180)) * sin((lat * pi() / 180)) +
cos((:lat2 * pi() / 180)) * cos((lat * pi() / 180)) *
cos((:long - lon) * pi() / 180))
) * 180 / pi()) * 60 * 1.1515 * 1.609344 * 1000) as distance
FROM POI_Table
HAVING distance < :radius
ORDER BY distance ASC
LIMIT 0, 50 " );
// PDOStatement::bindParam() binds the named parameter markers to the specified parameter values.
value['lat'], PDO::PARAM_STR );
value['lat'], PDO::PARAM_STR );
value['lon'], PDO::PARAM_STR );
value['radius'], PDO::PARAM_INT );
// Use PDO::execute() to execute the prepared statement sql->execute();
// Iterator for the response array.
sql query results and return each row as an array indexed by column name.
sql->fetchAll(PDO::FETCH_ASSOC);
/* Process the
// if pois array is empty, return empty array.
if ( empty(response["hotspots"] = array ();
}//if
else {
// Put each POI information into pois as poi["actions"] = array();
// Store the integer value of "lat" and "lon" using predefined function ChangetoIntLoc.
poi["lat"] );
poi["lon"] );
// Change to Int with function ChangetoInt.
poi["type"] );
poi["dimension"] );
// Change to demical value with function ChangetoFloat
poi["distance"] );
// Put the poi into the response array.
i] = i++;
}//foreach
}//else
return
* Main entry point **
/* Pre-define connection to the MySQL database, please specify these fields.*/
dbhost = "localhost";
dbuser = "database_username";
value */
// Put needed parameter names from GetPOI request in an array called
keys = array( "layerName", "lat", "lon", "radius" );
// Initialize an empty associative array.
_GET and put them in keys as _GET[value[_GET[key ." parameter is not passed in GetPOI request.");
}//foreach
}//try
catch(Exception e->getMessage();
}//catch
try {
/* Connect to MySQL server. We use PDO which is a PHP extension to formalise database connection.
For more information regarding PDO, please see http://php.net/manual/en/book.pdo.php.
*/
// Connect to predefined MySQl database.
dbhost; dbname=dbuser, db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );
/* Construct the response into an associative array.*/
// Create an empty array named response.
response["layer"] = response["hotspots"] = Gethotspots( value );
// if there is no POI found, return a custom error message.
if ( empty( response["errorCode"] = 20;
response["errorCode"] = 0;
response, print it into JSON format.*/
// Put the JSON representation of jsonresponse.
response );
// Declare the correct content type in HTTP response header.
header( "Content-type: application/json; charset=utf-8" );
// Print out Json response.
echo db to NULL to close the database connection.
e )
{
echo
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.