Monday, October 24, 2011

Google Map API Callback + CORS

Combining CORS and Google Maps API

function createCORSRequest(method, url) {
var xhr = null;
if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader('accept', "application/json");
}
return xhr;
}

function listenMarker(infowindow, model, map) {

google.maps.event.addListener(model, 'click',
function() {
var request = createCORSRequest("get", host + "url"
+ model.ID);
if (request) {
request.onload = function() {
var response = $j.parseJSON(request.responseText);
processLocationClickResponse(infowindow, model, map,
response);
};
request.send();
}
});
}

function processLocationClickResponse(infowindow, model, map, response) {
var contentString = '<div id="content"><h1 id="firstHeading" class="firstHeading">'
+ response.w
+ '</h1><div id="bodyContent" style="align : left;"><img src="nodeIcons[response.x]" />'
+ '<p>Label: '
+ response.y
+ '</p><p>Label:'
+ response.z + '</p>' + '</div>';
infowindow.setContent(contentString);
infowindow.open(map, marker);
}

No comments:

Post a Comment