Ik heb 2 functies die gebruikt worden om to zoomend naar de bounds van een polygon van landen en provincies:


function createCountry(coords, country) {

	
    var currentCountry = new google.maps.Polygon({
          map: map,
		paths: coords,
        strokeColor: '#000000',
        strokeOpacity: 1,
        strokeWeight: 1,
        fillColor: '#FF0000',
        fillOpacity: .5
    });
	

// currentCountry.setMap(map);   
 countries.push(currentCountry);
}


function getArrayBounds(polyArray){
   bounds = new google.maps.LatLngBounds();
    var path, paths; 
    for(var polys = 0; polys < polyArray.length; polys++) {
        paths = polyArray[polys].getPaths();       
        for (var i = 0; i < paths.getLength(); i++) {
            path = paths.getAt(i);
            for (var ii = 0; ii < path.getLength(); ii++) {
                bounds.extend(path.getAt(ii));
            }
        }
    }
	//console.log(bounds);
   // return bounds;
   map.panToBounds(bounds)
   map.fitBounds(bounds);
  
  
}

// de ajax word geladen bij een onchange event

 $.ajax({

        url : "<?=HTML_ROOT?>inc/countries.php?countryUrl=" + a + "&provUrl=" + b + "&cityUrl=" + c,
        cache : true,
        dataType : 'json',
        async : true,

        success : function(data) {


		if (data.country == 1 || data.prov == 1)
		{
		//	alert(data.prov);
	//onderlanden
			
                $.each(data.countryUrl, function(id,country) 
				{

                    var countryCoords;
                    var ca;
                    var co;

						if ('multi' in country) 
						{
	
							var ccArray = [];
	
							for (var t in country['xml']['Polygon']) 
							{
	
								countryCoords = [];
	
								co = country['xml']['Polygon'][t]['outerBoundaryIs']['LinearRing']['coordinates'].split(' ');
	
								for (i = 0; i < co.length; i++) 
								{
	
									ca = co[i].split(',');
	
								   countryCoords.push(new google.maps.LatLng(ca[1], ca[0]));	
								}
	
								ccArray.push(countryCoords);
							}
	
								createCountry(ccArray,country);
	
						} 
						else 
						{
	
								countryCoords = [];
		
								co = country['xml']['outerBoundaryIs']['LinearRing']['coordinates'].split(' ');
							
								var j;
								for (j = 0; j < co.length; j++) 
								{
															ca = co[j].split(',');						
															countryCoords.push(new google.maps.LatLng(ca[1], ca[0]));
																
								}

								createCountry(countryCoords,country);	
                    	} 
                }); // end loop


              
			setTimeout(function() {
				    getArrayBounds(countries);				
			}, 500);          
		  			
		} 
		
		else if (data.city == 1 && data.country == 2)
		{
	//	alert('df');
	
		 var polygonString = data.cityUrl;
 

			countryCoords = [];


								var j;
								for (j = 0; j < polygonString.length; j++) 
								{
									
									ca = polygonString[j].split(' ');											
									countryCoords.push(new google.maps.LatLng(ca[1], ca[0]));
							    }
								
														
								  var poly = new google.maps.Polygon({
		  									      map: map,
												  paths: countryCoords,
												  strokeColor: '#000000',
												  strokeOpacity: 0.8,
												  strokeWeight: 2,
												  fillColor: '#FF0000',
												  fillOpacity: 0.35
											});


												
								  poly.setMap(map); 
								  countries.push(poly);
								
								
			setTimeout(function() {
				    getArrayBounds(countries);				
			}, 500);  	
		
		  // map.fitBounds(bounds);
		   //map.panToBounds(bounds);   
		//google.maps.event.addDomListener(window, "load", initialize);
			
		    }
	
	    }
 
	});


Als ik via ajax countries laad, dan gaat hij wel naar de landen en zoomt daar wel goed in, maar als ik daarna een provincie laad, dan tekent hij de polygon wel goed, maar hij zoomt niet naar de exacte bounderies.
Waar ligt dit nu aan? Ik krijg in de console ook geen foutmeldingen. Ik heb settimeouts gebruikt naar 5 seconden zelfs, maar geen verschil.

Reageren