1

I'm using Code Igniter and the Googlemaps library. This library generates a lot of Javascript code dynamically, including the contents of the InfoWindows for each new marker, but I'd like to keep that in a separate template file, like a regular View.

I have this Javascript code (from Googlemaps' library):

        var lat = marker.getPosition().lat();
        var long = marker.getPosition().lng();

        var windowContent = "";

        if( _new ) {
            var newIW = new google.maps.InfoWindow( { content: windowContent } );

What I want to do is to load windowContent from a template file. I have already succeeded in dynamically generating a form for this variable and using lat and long variables defined just above, but how can I achieve this in Code Igniter? I can't use load->view because I'm not in a Controller's context. And I cannot use include() or readfile() either because of CI's security constraints.

Any hints?

5
  • put contentWindow content into a file, and use ajax to retrieve the file, and do the initialization code for the googlemaps infowindow in the callback Commented Jul 29, 2013 at 13:56
  • Could you elaborate on that? I don't know what you meant by the initialization code. Commented Jul 29, 2013 at 14:03
  • var newIW = new google.maps.InfoWindow( { content: windowContent } ); is the initialization code. As ajax is an async process you would only be able to execute the code once the ajax call has returned with the content. Commented Jul 29, 2013 at 14:06
  • So replace windowContent in this initialization with an ajax call, passing lat and long as parameters to get the template back? Commented Jul 29, 2013 at 14:10
  • Added an answer showing what an ajax call would look like with your code. Commented Jul 29, 2013 at 14:29

1 Answer 1

2

Using pure javascript, get the lat and long, make a url with the lat and long in the query string, and use xhr to do the ajax call.

var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();

var xhr;
var url = "http://myurl.to/script.php?lat="+lat+"&lng="+long;
if(typeof XMLHttpRequest !== 'undefined') 
    xhr = new XMLHttpRequest();
else {
    //Get IE XHR object
    var versions = ["MSXML2.XmlHttp.5.0", 
            "MSXML2.XmlHttp.4.0",
            "MSXML2.XmlHttp.3.0", 
            "MSXML2.XmlHttp.2.0",
            "Microsoft.XmlHttp"];

    for(var i = 0, len = versions.length; i < len; i++) {
        try {
            xhr = new ActiveXObject(versions[i]);
            break;
        }
        catch(e){}
    }
}
xhr.onreadystatechange = function(){
    //This function is called every so often with status updates
    //It is complete when status is 200 and readystate is 4

    if(xhr.status == 200 && xhr.readyState === 4) {  
        //Returned data from the script is in xhr.responseText
            var windowContent = xhr.responseText;

            //Create the info window
            var newIW = new google.maps.InfoWindow( { content: windowContent } );

            //Pass newIW to whatever other function to use it somewhere
    }
};

xhr.open('GET', url, true);
xhr.send();

if using a library like jQuery it would be like

var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();
var url = "http://myurl.to/script.php";
jQuery.ajax({
   "url":url,
   "data":{ //Get and Post data variables get put here
      "lat":lat,
      "lng":long
   },
   "dataType":"html", //The type of document you are getting, assuming html
                      //Could be json xml etc
   "success":function(data) { //This is the callback when ajax is done and successful
      //Returned data from the script is in data
      var windowContent = data;

      //Create the info window
      var newIW = new google.maps.InfoWindow( { content: windowContent } );

      //Pass newIW to whatever other function to use it somewhere
   }
});
3
  • I tried a simplified jQuery version of your code, with a function call windowContent = load_content(lat, long). The called URL is working fine and I want it to return "data" but somehow it is not working. I have even tested with alert(data) and it's all there, but it doesn't get returned. Maybe it has something to do with the time it takes the Ajax call to load. Commented Jul 29, 2013 at 14:42
  • 1
    ajax is async it doesnt return data back when calling it, it is why you have to get the data in the callback function, return data; from the callback function will not return it to the windowContent variable like in the context of windowContent = load_content(lat, long) assuming load_content is a wrapper function for doing the ajax call itself. Commented Jul 29, 2013 at 14:44
  • Thanks a lot for the code and the infos. Really helped with a lot of concepts. Will try to set the infowindow contents inside the Ajax call as you proposed as soon as I get home. :) Commented Jul 29, 2013 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.