Jump to content

Google Maps Api


skaterdav85

Recommended Posts

so here i want to plot points on a google map. the points are coming from an array. I want to loop through the 'for loop' towards the bottom of the script, and each time it calls my plotPoint function and passes it a new address. But its not working. What am i doing wrong? anyone know? Thanks!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>ITP 404 - HW 6</title><script src="http://maps.google.com/maps?file=api" type="text/javascript"></script><script type="text/javascript">var uscInfo = new Array(5);//this creates a multidimensional arrayfor(i=0; i<5; i++) {	uscInfo[i] = new Array(2);}uscInfo[0][0] = '839 Childs Way, Los Angeles, CA 90089';uscInfo[0][1] = 'tutor.jpg';uscInfo[1][0] = '3715 McClintock Avenue, Los Angeles, CA 90089';uscInfo[1][1] = 'ISE.jpg';uscInfo[2][0] = '3650 McClintock Ave, Los Angeles, CA 90089';uscInfo[2][1] = 'itp.jpg';uscInfo[3][0] = '1025 West 34th Street, Los Angeles, California 90089';uscInfo[3][1] = 'ee.jpg';uscInfo[4][0] = '3620 South Vermont Ave., Los Angeles, California 90089';uscInfo[4][1] = 'kap.jpg';var map = null;var geocoder = null;var uscAddress = 'University of Southern California, Los Angeles, CA 90089';	function showAddress(address) {	  if (geocoder) {		geocoder.getLatLng(		  address,		  function(point) {			if (!point) {			  alert(address + " not found");			} else {			  map.setCenter(point, 13);			  var marker = new GMarker(point);			  map.addOverlay(marker);			  marker.openInfoWindowHtml(address);			}		  }		);	  }	} //end of showAddress function		function plotPoint(address) {	  if (geocoder) {		geocoder.getLatLng(		  address,		  function(point) {			if (!point) {			  alert(address + " not found");			} else {			  //map.setCenter(point, 13);			  map.addOverlay(new GMarker(point))			  //var marker = new GMarker(point);			  //map.addOverlay(marker);			  marker.openInfoWindowHtml(address);			}		  }		);	  }	} 	function initialize() {	  if (GBrowserIsCompatible()) {		map = new GMap2(document.getElementById("map_canvas"));		//map.setCenter(new GLatLng(37.4419, -122.1419), 13);		geocoder = new GClientGeocoder();		  map.setCenter(showAddress(uscAddress), 13);	  for(i=0; i<5; i++) {			plotPoint(uscInfo[i][0]);			}				}	}</script></head>  <body onload="initialize();" onunload="GUnload()">	  <div id="map_canvas" style="width: 700px; height: 500px"></div>  </body></html>

Link to comment
Share on other sites

I have had several problems using for loops with googles api in the past and have taken to pulling the data into the script from an xml file generated using php and mysql with data from a database. I find it much easier to update that way, but it depends on how often you are changing the data.

Link to comment
Share on other sites

  • 2 months later...

I got your code to work by uncommenting the following. I read somewhere that not explicitly setting the center before trying to work with the overlay provides unpredictable results. Working on that theory (albeit with the geocoder being the questionable element and not necessarily the overlay), I tried uncommenting this and all JS errors went away and all your points show up. Granted, you probably need to either pick a point closer to USC or better yet not display the "map_canvas" until you have re-centered on the geocoder results as it is less than optimal to have the map pause on the Google campus before resolving to the points you want to see.

		//map.setCenter(new GLatLng(37.4419, -122.1419), 13);

I hope that helps (sorry for taking so long, but I have been really busy).Mark :)

Link to comment
Share on other sites

  • 3 weeks later...
I have had several problems using for loops with googles api in the past and have taken to pulling the data into the script from an xml file generated using php and mysql with data from a database. I find it much easier to update that way, but it depends on how often you are changing the data.
Say there is a REST web service that gives you latitude and longitude coordinates (in the XML). How would you go about plotting these on a map? Would you just output all the points into a JS array for Google Maps to loop through and plot? or is there a better way?
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...