[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

Google Maps - AJAX Issue or Variable Scope?

Asked by drgdrg in JavaScript, GIS & GPS Programming, WebApplications

Tags: google maps geocoder getlocations javascript

Hello.  I've written a page that builds a Google Map (you will have to substitute in your key to get it to work in your site).  It draws a map, plots two map points, draws out the polyline and everything looks nice.   If I click on the marker, it does a request to Google to look up the street address for that coordinate.  Very cool, actaully.

When I click it, I get what you see in the attached image "bad.jpg" - in the pop up window it gives an "undefined" message in the text box instead of the address.

If I immediately click on the marker a second time, I do get the street address (along with the coordinates, http status and accuracy rating) which you can see in Good.jpg   NOTE: I have not *reloaded* the page.  I've only clicked that marker a second time.

I was able to get it to work if I add a javascript Alert('hi') before the lines that say:

marker1.openInfoWindowHtml('03/27/2009 11:58<BR>This is point 1.<BR>'+ theaddress );
and
marker10.openInfoWindowHtml('03/27/2009 11:58<BR>This is point 10.<BR>'+ theaddress );

However, this is not the way it should work, from a usability standpoint.  

This would lead me to believe that the first AJAX request is working correctly but that it is trying to render it too quickly with the openInfoWindowHtml - perhaps the AJAX isn't complete yet.  

Any suggestions?

Thank you !!!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<HEAD>
 
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" type="text/javascript"></script>
<script type="text/javascript">
 
	var theaddress='';
 
	initialize = function() {
			
		if (GBrowserIsCompatible()) {
			// Points to be plotted 
			var points = new Array();
			var theaddress;
			
			// Initialize Map
			map = new GMap2(document.getElementById("map_canvas"));
			map.setCenter(new GLatLng(37.4319,-122.1419), 4);
			geocoder = new GClientGeocoder();
 
			// Which Map Control to Use
			map.addControl(new GLargeMapControl());
			
			// Add a scale control ?
			map.addControl(new GScaleControl());
			
			// Which Map Types
			map.addControl(new GMapTypeControl());
			
			// Add Overview Map Control 
			map.addControl(new GOverviewMapControl());
			
 
			function getAddress(overlay, latlng) {  
				if (latlng != null) {	
					address = latlng;    
					var theaddress=''; 
					geocoder.getLocations(latlng, showAddress); 
				}
			}
 
				
			function showAddress(response) {  
				// map.clearOverlays();  
				if (!response || response.Status.code != 200) {    
					alert("Status Code:" + response.Status.code);  
					theaddress = 'Unable to locate address.';  
				} 
				else {    
					place = response.Placemark[0];    
					point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);    
					// marker.openInfoWindowHtml( '<b>orig latlng:</b>' + response.name + '<br/>' +         '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +        '<b>Status Code:</b>' + response.Status.code + '<br>' +        '<b>Status Request:</b>' + response.Status.request + '<br>' +        '<b>Address:</b>' + place.address + '<br>' +        '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);  
					theaddress = '<b>Address:</b> ' + place.address + '<br><b>coord:</b> ' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '  ' + '(stat ' + response.Status.code + ') (accur:' + place.AddressDetails.Accuracy + ')<br>';
				}
			}
 
			
			// If we have coordinates, use those
			point = new GLatLng(37.4349, -122.1219);
			marker1 = new GMarker(point);
			points.push(point);
 
			// Calculate current address and add into floater
			GEvent.addListener(marker1, "click", 
				function() {
					// theaddress = 'hi';
					point = new GLatLng(37.4349, -122.1219);
					// alert('theaddress1: ' + theaddress);
					getAddress(map, point);
					
					// alert(null);
					marker1.openInfoWindowHtml('03/27/2009 11:58<BR>This is point 1.<BR>'+ theaddress );
				});
			map.addOverlay(marker1);
 
 
			// If we have coordinates, use those
			point = new GLatLng(37.3239, -121.9299);
			marker10 = new GMarker(point);
			points.push(point);
 
			// Calculate current address and add into floater
			GEvent.addListener(marker10, "click", 
				function() {
					point = new GLatLng(37.3239, -121.9299);
					getAddress(map, point);
					marker10.openInfoWindowHtml('03/27/2009 11:58<BR>This is point 10.<BR>'+ theaddress );
				});
			map.addOverlay(marker10);
 
			
			var polyline = new GPolyline( [  
				new GLatLng(37.4349, -122.1219), new GLatLng(37.4269, -122.0819), new GLatLng(37.4179, -122.0489), 
				new GLatLng(37.3979, -122.0399), new GLatLng(37.3889, -122.0249), new GLatLng(37.3809, -122.0009), 
				new GLatLng(37.3679, -121.9899), new GLatLng(37.3509, -121.9809), new GLatLng(37.3349, -121.9569), 
				new GLatLng(37.3239, -121.9299) 
				], "#ff0000", 10);
			map.addOverlay(polyline);
		
			// Scale the zoom level to the points on the map and center between the points.
			var bounds = new GLatLngBounds();
		   	for (var i=0; i< points.length; i++) {
				  bounds.extend(points[i]);
		   	}
		   	map.setZoom(map.getBoundsZoomLevel(bounds));
		   	map.setCenter(bounds.getCenter());
 
		}
	}
</script>
</HEAD>
 
<body onload="initialize();" onunload="GUnload()">
<style>
	body,td {font-family: arial; font-size: 10pt;}
</style>
 
<div id="map_canvas" style="width: 700px; height: 500px"></div>
 
</body>
</HTML>
Attachments:
 
Screenshot of UNDEFINED
Screenshot of UNDEFINED
 
 
Screenshot of Successful retrieval
Screenshot of Successful retrieval
 
[+][-]04/03/09 01:09 AM, ID: 24057618Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: JavaScript, GIS & GPS Programming, WebApplications
Tags: google maps geocoder getlocations javascript
Sign Up Now!
Solution Provided By: snsargv
Participating Experts: 1
Solution Grade: A
 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625