2011-06-15

Grabbing a Region Map

Following on from yesterday's post about the map thing I made, I thought someone might like to see the code for grabbing the map. It's nothing clever and, after I threw it together, I realised that the gridsurvey.com API docs had an example anyway, but I'll post it here anyway in case anyone finds it useful.

It's written such that all you need do is drop the script in a prim (ideally a cube, I guess) and it'll "just work". Note that it places the map on face 0 of the prim -- this is handy for creating a one-prim framed map of your home.

You can find the code on gist, and embedded below:

//////////////////////////////////////////////////////////////////////
// Holds the key of the last request.
key g_keyRequest;
//////////////////////////////////////////////////////////////////////
// Force a refresh of the region map.
RefreshMap()
{
g_keyRequest = llHTTPRequest( "http://api.gridsurvey.com/simquery.php?" +
"region=" + llEscapeURL( llGetRegionName() ) + "&item=objects_uuid",
[ HTTP_METHOD, "GET" ], "" );
}
//////////////////////////////////////////////////////////////////////
// Default state.
default
{
//////////////////////////////////////////////////////////////////
// Start entry.
state_entry()
{
// Do an initial refresh.
RefreshMap();
// Set a timer so we refresh every so often.
llSetTimerEvent( 43200 );
}
//////////////////////////////////////////////////////////////////
// Refresh on touch.
touch_start( integer total_number )
{
RefreshMap();
}
//////////////////////////////////////////////////////////////////
// Refresh on timer.
timer()
{
RefreshMap();
}
//////////////////////////////////////////////////////////////////
// Handle the HTTP response from the request for the map UUID.
http_response( key request_id, integer status, list metadata, string body )
{
// If the response is in response to our request...
if ( request_id = g_keyRequest )
{
// ...if it's not an error...
if ( llGetSubString( body, 0, 4 ) != "Error" )
{
// Set the texture of the first face to the map.
llSetTexture( body, 0 );
}
}
}
}
//////////////////////////////////////////////////////////////////////
// Holds the key of the last request.
key g_keyRequest;
//////////////////////////////////////////////////////////////////////
// Force a refresh of the region map.
RefreshMap()
{
g_keyRequest = llHTTPRequest( "http://api.gridsurvey.com/simquery.php?" +
"region=" + llEscapeURL( llGetRegionName() ) + "&item=objects_uuid",
[ HTTP_METHOD, "GET" ], "" );
}
//////////////////////////////////////////////////////////////////////
// Default state.
default
{
//////////////////////////////////////////////////////////////////
// Start entry.
state_entry()
{
// Do an initial refresh.
RefreshMap();
// Set a timer so we refresh every so often.
llSetTimerEvent( 43200 );
}
//////////////////////////////////////////////////////////////////
// Refresh on touch.
touch_start( integer total_number )
{
RefreshMap();
}
//////////////////////////////////////////////////////////////////
// Refresh on timer.
timer()
{
RefreshMap();
}
//////////////////////////////////////////////////////////////////
// Handle the HTTP response from the request for the map UUID.
http_response( key request_id, integer status, list metadata, string body )
{
// If the response is in response to our request...
if ( request_id == g_keyRequest )
{
// ...if it's not an error...
if ( llGetSubString( body, 0, 4 ) != "Error" )
{
// Set the texture of the first face to the map.
llSetTexture( body, 0 );
}
}
}
}

No comments:

Post a Comment