Google maps & variable scope
Posted: 27 January 2010 09:44 AM   [ Ignore ]
Sr. Member
RankRankRankRank
Total Posts:  132
Joined  2009-10-02

I’ve got this function that plots a point from an address in a text field

function poiFromAddress() {
    
var address document.getElementById('address').value
    
var geocoder = new GClientGeocoder();

    
geocoder.getLatLng(

        
address,

        function(
point){
            
if (!point{

                      alert(
"We're sorry, " address " cannot be located");

            
else {
                
var html "<b>Search Location</b><br>" address;
                  
html += '<br /><a href="[removed]gmap.setZoom(15);">Zoom in</a>'
                  var 
marker createDeluxeMarker(1pointjobiconhtml);
                  
gmap.addOverlay(marker);              
                  
mapbounds.extend(point);
            
}
        }
)

For some reason the mapbounds.extend does not work. I’ve tried just about every combination and I can’t get this point to be part of the calculation for zooming and centering the map. I believe it is a variable scope issue.

Profile
 
 
Posted: 27 January 2010 11:50 AM   [ Ignore ]   [ # 1 ]
Administrator
RankRankRankRank
Total Posts:  249
Joined  2008-12-18

Is mapbounds defined outside the scope of poiFromAddress and have you verified that it’s been instantiated correctly?

Profile
 
 
Posted: 27 January 2010 12:16 PM   [ Ignore ]   [ # 2 ]
Sr. Member
RankRankRankRank
Total Posts:  132
Joined  2009-10-02

Yes, map bounds works for all other points.

Through further investigating it turned out to be an order of execution problem. The ‘function’ code was executed after the other code on the page so it wasn’t zooming properly, but repeating the zooming code block inside the function fixed it.

function poiFromAddress() {
    
var address document.getElementById('address').value
    
var geocoder = new GClientGeocoder();

    
geocoder.getLatLng(

        
address,

        function(
point){
            
if (!point{

                      alert(
"We're sorry, " address " cannot be located");

            
else {
                
var html "<b>Search Location</b><br>" address;
                  
html += '<br /><a href="[removed]gmap.setZoom(15);">Zoom in</a>'
                  var 
marker createDeluxeMarker(1pointjobiconhtml);
                  
gmap.addOverlay(marker);
                  
mapbounds.extend(point);
                  
zoom gmap.getBoundsZoomLevel(mapbounds)
                
gmap.setZoom(zoom);
                
gmap.panTo(mapbounds.getCenter());
                
setDistances(point)
            
}
        }
)

Profile
 
 
   
 
 
‹‹ Idle Calculations      mobile maps ››