Fitbounds naar polygon werkt niet altijd

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Daniel van Seggelen

Daniel van Seggelen

24/02/2017 22:21:48
Quote Anchor link
Ik heb 2 functies die gebruikt worden om to zoomend naar de bounds van een polygon van landen en provincies:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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.
 
Er zijn nog geen reacties op dit bericht.



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.