Search code examples
jquerygoogle-maps-api-3spring-roogoogle-fusion-tables

JQuery and Google Maps JS api not working together


I am really stuck trying to use both the Google Maps API and the JQuery API in the same block.. My page is jspx (Spring) and I need the JQuery so that I can get and parse google fusion table data.

However, when I declare both libraries together the map div will not load (if I load Google Maps alone, all of the mapping works fine).

Here is the relevant code:

<script src="http://code.jquery.com/jquery-latest.pack.js" type="text/javascript"/>
                    <script src="http://maps.googleapis.com/maps/api/js?v=3key=MYKEY&amp;sensor=false" type="text/javascript">
                        <jsp:text/>
                    </script>
                    <script type="text/javascript">
        //<![CDATA[ 
            var map;

function initialize() {
         var mapDiv = document.getElementById('map_canvas');
         var geocoder = new google.maps.Geocoder();
         retrieveDeprivationFigure();
          if (geocoder) {
             geocoder.geocode({ 'address': '${property.postcode}' }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {

                var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng())
                var mapOptions = {
                    zoom: 15,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                  };

                map = new google.maps.Map(document.getElementById('map_canvas'),
                        mapOptions); 
}

        function retrieveDeprivationFigure(){
                var postcode = '${property.postcode}'.split(' ').join('');
                var url = "https://www.googleapis.com/fusiontables/v1/query?sql=SELECT LSOA FROM XXXXXXXX WHERE Postcode='"+ postcode +"'&key=AIzaSyBQaHw1aSWtIjQzAiriBPC3hvm7Bs1R35U&jsonCallback=?";
                var localIMDS;
                console.log("this far");
                $.getJSON(url,
                        function retrieveIMDS(){
                    console.log(url);

                });
         };

  google.maps.event.addDomListener(window, 'load', initialize);   
         // ]]>
</script>   

Any pointers or ideas would help me greatly, I even tried retrieving the deprivation stat via JQuery in a seperate JS block, however despite my efforts, the function was not called on page load.


Solution

  • Thanks for the guidance guys, but I managed to sort the conflicts and issues by creating a head and declaring the jQuery in the head file instead, and leaving the google src where it was.

     <head>
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.js"></script>
     <script type="text/javascript">
    
     </script>
     </head>
    

    thanks again!