2013-04-26 2 views
0
var classBounds = new google.maps.LatLngBounds(); 
var query = ""; 

query = "SELECT 'ROAD_NUMBER', 'geometry' FROM [TABLE_ID HERE] WHERE 'ROAD_NUMBER' CONTAINS IGNORING CASE '" + searchString + "' AND 'LINK_NUMBER' CONTAINS '" +searchStringLinkNo + "'"; 
var encodedQuery = encodeURIComponent(query); 

// Construct the URL 
var url = ['https://www.googleapis.com/fusiontables/v1/query']; 
url.push('?sql=' + encodedQuery); 
url.push('&key=[key]'); 
url.push('&callback=?'); 


// Send the JSONP request using jQuery 
$.ajax({ 
    url: url.join(''), 
    dataType: 'jsonp', 
    success: function (data){ 

     var rows = data['rows']; 

     if(rows){ 

      var classRoadGeo1  = rows[0][1].geometry.coordinates; 
      var classRoadGeo2  = rows[rows.length - 1][1].geometry.coordinates; 
      var classRoadGeoStart = classRoadGeo1[0]; 
      var classRoadGeoEnd  = classRoadGeo2[classRoadGeo2.length - 1]; 

      var startHolder = classRoadGeoStart.toString(); 
      var endHolder = classRoadGeoEnd.toString(); 

      var bound1 = startHolder.split(","); 
      var bound2 = endHolder.split(","); 

      var coordinate1 = new google.maps.LatLng(bound1[1], bound1[0]); 
      var coordinate2 = new google.maps.LatLng(bound2[1], bound2[0]); 

      alert(bound1); 
      alert(bound2); 

      classBounds.extend(coordinate1); 
      classBounds.extend(coordinate2);     

     } 
     else{ 

      alert("Unable to locate any counter/classifier stations for the current selection."); 
     } 
    } 
}); 

map.fitBounds(classBounds); 

}fitBounds 및 확장 - 모든 위의 코드이다 완전히 다른 장소

에 게재. 융합 표에서 도형을 가져 와서 다시 문자열로 변환하고 LatLng 객체를 만들어 냈습니다. 그러나 fitBounds를 실행하면 Papa New Guinea로 이동합니다. 좌표는 실제로 호주 시드니의 Kiama에 있습니다. 오류가 없습니다. 무엇이 잘못 되었을지에 대한 아이디어가 있습니까?

답변

0

ajax 요청은 비동기식이므로 map.fitBounds()가 호출 될 때 경계가 아직 확장되지 않았습니다. 마지막 줄을 넣으십시오. "map.fitBounds (classBounds);" 성공 함수 안에있는 if 절의 끝에서 제대로 작동해야합니다.