Solved

callback with Google Maps API v 3 and Google Fusion Table API v1

Posted on 2013-02-01
2
368 Views
Last Modified: 2013-02-04
I'm attempting to retrieve data from a Google Fusion table using

var script = document.createElement("script");

       script.setAttribute("src","https://www.googleapis.com/fusiontables/v1/query?sql=SELECT * FROM " +
        tableid + " WHERE ST_INTERSECTS(geometry, CIRCLE(LATLNG(" + coordinate.lat() + "," + coordinate.lng() + ", 0.001))&callback=addInfoWindow&key=" + apiKey);
    document.getElementsByTagName("head")[0].appendChild(script);

Open in new window


Using console.log("pin clicked"), I can confirm that addInfoWindow() is being launched. However, the corresponding infoWindow doesn't open. The problem is how I'm trying to receive the data in the response.

https://developers.google.com/fusiontables/docs/samples/mouseover_map_styles recommends to do it this way:

// reduced from the actual version for simplicity sake
function addInfoWindow(data) {

       infowindow.close();
       var rows = data['rows'];
       for (var i in rows) {

          if (rows[i][0]['name'] != '') 
         {
             initialize();
             infowindow.setContent(rows[i][0]['name']);
         }
         else
        {
           initialize(); 
           infowindow.setContent("nothing");
        }

       infowindow.setPosition(coordinate);
       map.setCenter(coordinate);
       map.setZoom(15);
       infowindow.open(map);
        
}

Open in new window


I never make it in the for loop when this script runs, though.
0
Comment
Question by:mcmontyCS
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 38845132
I think you're missing a closing parenthesis before the circle radius in your sql.
script.setAttribute("src","https://www.googleapis.com/fusiontables/v1/query?sql=SELECT * FROM " +
        tableid + " WHERE ST_INTERSECTS(geometry, CIRCLE(LATLNG(" + coordinate.lat() + "," + coordinate.lng() + "), 0.001))&callback=addInfoWindow&key=" + apiKey);

Open in new window

0
 

Author Comment

by:mcmontyCS
ID: 38851588
Thanks, @rober_schutt. You were indeed correct about the closing parenthesis. There were several other issues with my code as well which have been corrected to the following:

function addInfoWindow(data) {

	infowindow.close();
        initialize();
	
	var rows = data['rows'];
	
	if (rows)
        {
		for (var i = 0; i < 1; i++)
		{

			infowindow.setContent(rows[i][1] + rows[i][0]);
 			infowindow.setPosition(coordinate);
        		map.setCenter(coordinate);
			map.setZoom(15);
        		infowindow.open(map);
			
		}
	}
	else
	{
		infowindow.setContent("Not available");
    		map.setCenter(coordinate);
		map.setZoom(15);
        	infowindow.open(map);
	}
	
}



function generateInfoWindow(results, status) {
    
	initialize();

	if (status == google.maps.GeocoderStatus.OK) 
	{
		initialize();
		//center and zoom map
		coordinate = results[0].geometry.location;
 
        marker = new google.maps.Marker({
			map: map,
			layer: layer,
			animation: google.maps.Animation.DROP,
			position: coordinate
		});
		map.setCenter(results[0].geometry.location);
		map.setZoom(15);
		
		
 	// Initialize JSONP request
        var script = document.createElement('script');
        var url = ['https://www.googleapis.com/fusiontables/v1/query?'];
        url.push('sql=');
        var query = "SELECT * FROM " +
        tableid + " WHERE ST_INTERSECTS(geometry, CIRCLE(LATLNG(" + coordinate.lat() + "," + coordinate.lng() + "), 0.001))";
        var encodedQuery = encodeURIComponent(query);
        url.push(encodedQuery);
        url.push('&callback=addInfoWindow');
        url.push('&key=' + apiKey);
        script.src = url.join('');
        var head = document.getElementsByTagName('head')[0];
        head.appendChild(script);

			 
   	} 
	else 
	{
        	alert("Please make sure you entered your City and State");
    	}

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Download a website to hdd 2 51
Loop and hide/show dropdown option with jQuery 4 22
How can I get this SlideToggle to behave? 6 18
Change javascript css 1 20
This article will show, step by step, how to integrate R code into a R Sweave document
Several part series to implement Internet Explorer 11 Enterprise Mode
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now