Friday, May 2, 2008

Integrating Google Maps into PowerSchool

I've been working this week customizing PowerSchool's default student "Addresses" page to calculate the driving distance for each student from their home to the school's front door, using the Google Maps API. In addition to calculating and displaying the distance, I wanted to provide a means of storing the distance in the PowerSchool DB, for later searching and reporting purposes.

I began by creating a new student field: Distance_To_School, and added an input box for this field to the Addresses page. I then added the following Javascript to the page:


// Imports the Google Maps Library
// key values are unique to each domain and can be
// obtained on the Google Maps API page
<script src="http://maps.google.com/maps?file=api&v=2&key
=abcd"
type="text/javascript"/>

<script type="text/javascript">

var map;

// Creates a map object.
function initialize() {
map = new GMap2();
}

// Gets the distance from start to end in miles,
// adding additional miles and displaying this
// distance as the value of the elemid input box
function getDistance(start,end,additional,elemid) {

// Get the directions
var directions = new GDirections(map);
directions.load(start+" to "+end);

// Listen for the directions to load
// This is necessary, or the function will try
// to get the distance before the directions
// have fully loaded
GEvent.addListener(directions,"load",function()
{
// Calculate the distance
drivingdistance =
Math.round((directions.getDistance().meters/1609.344+
parseFloat(additional))*10000)/10000;
// Set value of elemid input box box to
// the calculated distance
document.getElementById(elemid).value=
drivingdistance;
})
}
</script>


I then have initialize() called on page load, and added a link to calculate the distance for each of our schools:
<a href="javascript:getDistance('1 Anywhere Lane, Ourtown
, MA','1 School Road, Ourtown, MA','.0500','distancebox')
"
>Calculate Distance to High School</a>


The additional distance paramter is provided to allow for the distance from the street to the school's drop-off area. This is important for us, as one our schools has a 1/4 mile driveway.

When I have more time, I'm going to modify the page so it automatically populates the distance using the student's currently enrolled school year, and possibly add an option to calculate using alternate mapping services.

Any other suggestions on how this approach could be improved?

No comments: