Created
April 30, 2011 07:30
-
-
Save wookiehangover/949518 to your computer and use it in GitHub Desktop.
a wrapper for the bing maps api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(win, $, Microsoft) { | |
if( !$ || !Microsoft ) { | |
throw "Missing dependencies"; | |
} | |
var MS = function( $map ){ | |
var API_KEY = 'An8yWa3XskSOa7CEhg2XSUayDt3feSVnj4ckyklE934aIVKoNOS6kTtP4RCIY-X_', | |
BASE_URL = 'http://dev.virtualearth.net/REST/v1/', | |
// map placeholder | |
map = null, | |
// this will hold the item location geo-coords | |
item_location, item_addr, | |
// re-map MS global to a shorter var name | |
m$ = Microsoft.Maps; | |
if( arguments.length < 1 ){ | |
throw "Invalid Arguments"; | |
} | |
// wrapper for creating a map object and | |
// instantiating a item location pin | |
function loc( elem, params ) { | |
// overwrite elem to point to the active item | |
elem = ( !elem ) ? $('.map-item.active') : $( elem ); | |
// create the map object | |
if( !map ) map = new m$.Map( $map[0], { credentials: API_KEY, mapTypeId: "r" } ); | |
// cache the item address | |
item_addr = elem.text(); | |
// initial request URL | |
var request_url = BASE_URL +"Locations/"+ item_addr +"?output=json&jsonp=?&key="+ API_KEY, | |
infobox; | |
// detect what flavor of params we're dealing with | |
if( typeof params == "string" ){ | |
params = { title: params }; | |
} else if( $.isArray( params ) ){ | |
params = { title: params[0], text: params[1] }; | |
} | |
// Grab the Map | |
$.getJSON( request_url ).success(function(result){ | |
// let's just assume that the API returns good data | |
// and throw an alert if things aren't kosher | |
try { | |
item_location = result; | |
// clear out the map | |
map.entities.clear(); | |
var bbox = result.resourceSets[0].resources[0].bbox, | |
// set the map viewing area | |
viewBoundaries = m$.LocationRect.fromLocations( | |
new m$.Location(bbox[0], bbox[1]), | |
new m$.Location(bbox[2], bbox[3]) | |
), | |
// grab a location object from the coords | |
location = new m$.Location( | |
result.resourceSets[0].resources[0].point.coordinates[0], | |
result.resourceSets[0].resources[0].point.coordinates[1] | |
), | |
// place a pin on the address | |
pushpin = new m$.Pushpin( location ); | |
// draw the rollover with item info | |
infobox = new m$.Infobox(location, $.extend( params, { visible: true } ) ); | |
// add a microsoft friendly click handler | |
// to toggle rollover state | |
m$.Events.addHandler(pushpin, 'click', function(){ | |
infobox.setOptions({ visible: !infobox._visible }); | |
}); | |
// draw everything onto the map | |
map.setView({ bounds: viewBoundaries }); | |
map.entities.push(pushpin); | |
map.entities.push(infobox); | |
} catch( invalid_data ) { | |
alert('Unable to find location'); | |
} | |
}); | |
} | |
// draws directions between 2 points | |
function route( $start, $end, $target ){ | |
var start = $start.selector ? $start.text() : $start, | |
end = $end.selector ? $end.text() : $end, | |
directions_url = BASE_URL +"Routes?wp.0="+ start +"&wp.1='"+ end +"'&routePathOutput=Points&output=json&key=" + API_KEY; | |
$.ajax({ | |
type: 'GET', | |
crossDomain: true, | |
dataType: 'jsonp', | |
url: directions_url, | |
jsonp: 'jsonp' | |
}) | |
.success(function(result){ | |
try { | |
// Make sure that the map is cleared of any old routes; | |
map.entities.clear(); | |
// Set the map view | |
var bbox = result.resourceSets[0].resources[0].bbox, | |
viewBoundaries = m$.LocationRect.fromLocations(new m$.Location(bbox[0], bbox[1]), new m$.Location(bbox[2], bbox[3])), | |
routeline = result.resourceSets[0].resources[0].routePath.line, | |
routeLegs = result.resourceSets[0].resources[0].routeLegs[0].itineraryItems, | |
routepoints = [], routeshape, | |
pins = [], | |
routeLength = routeline.coordinates.length; | |
// Set the map boundaries | |
map.setView({ bounds: viewBoundaries}); | |
for (var i = 0; i < routeLength; i++) { | |
routepoints[i] = new m$.Location(routeline.coordinates[i][0], routeline.coordinates[i][1]); | |
} | |
pins[0] = new m$.Location(routeline.coordinates[0][0], routeline.coordinates[0][1]); | |
pins[1] = new m$.Location(routeline.coordinates[ routeLength - 1 ][0], routeline.coordinates[ routeLength - 1 ][1]); | |
// Draw the route on the map | |
routeshape = new m$.Polyline(routepoints, { strokeColor:new m$.Color(200,0,0,200) }); | |
map.entities.push(routeshape); | |
map.entities.push( new m$.Pushpin(pins[0], { text: "A" })); | |
map.entities.push( new m$.Pushpin(pins[1], { text: "B" })); | |
if( !! $target ) return; | |
// loops through the route legs and outputs the list of directions | |
$.each( routeLegs, function(i, leg){ | |
$('<div>', { 'class': 'map-directions' }) | |
.append('<p>'+ leg.instruction.text +'</p>') | |
.append('<span class="map-index">'+(i + 1)+'</span>') | |
.appendTo( $target ); | |
}); | |
} catch( invalid_data ) { | |
alert('Unable find directions from that location.'); | |
} | |
}); | |
} | |
return MS = { | |
loc: loc, | |
route: route, | |
BASE_URL: BASE_URL, | |
API_KEY: API_KEY | |
}; | |
}; | |
win.MS = MS; | |
})(window, jQuery, Microsoft); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo here: http://jsbin.com/akafo5/