2013-11-14 4 views
0

Следующая функция работает, но я изучаю улучшение кода. Первое, что я хотел бы сделать, - удалить стиль внутри объекта «myOption», чтобы загрузить их раньше, и оставить его только в том случае, когда мне нужно вернуть данные из вызова AJAX.Присоединиться к объектам

function codeAddress() { 
    var address = document.getElementById('address').value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      draggable: true, 
      position: results[0].geometry.location 
     }); 

     var marker = new google.maps.Marker({ 
      position: marker.position, 
      map: map, 
      title: results[0].formatted_address 
     }); 
     $.ajax({ 
     url: url+marker.position.ob+","+marker.position.pb+"", 
     dataType: "jsonp", 
     success: function (data) { 
      var myOptions = { 
       content: template(data) 
       ,disableAutoPan: true 
       ,maxWidth: 0 
       ,pixelOffset: new google.maps.Size(-240, 0) 
       ,zIndex: null 
       ,boxStyle: { 
       background: "url('tipbox.gif') no-repeat" 
       ,opacity: 0.9 
       ,width: "454px" 
       } 
       ,closeBoxMargin: "10px 10px 2px -20px" 
       ,closeBoxURL: "assets/image/x.png" 
       ,infoBoxClearance: new google.maps.Size(1, 1) 
       ,isHidden: false 
       ,pane: "floatPane" 
       ,enableEventPropagation: false 
      }; 
      var ib = new InfoBox(myOptions); 
      ib.open(map, marker); 
     } 
     }); 

    } 
    else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 

    }); 
} 

Я думал создать объект стиля и загрузить его перед вызовом AJAX и чем присоединиться к нему с объектом, содержащим данные, чем передать один объект внутри «нового Infobox (NewObject)»

Любой идеи о том, как это сделать или, может быть, существует другой/лучший способ?

Full script

ответ

1
function codeAddress() { 
    var address = document.getElementById('address').value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     /* 
     * This is overwritten below 
     var marker = new google.maps.Marker({ 
      map: map, 
      draggable: true, 
      position: results[0].geometry.location 
     }); 
     */ 
     var marker = new google.maps.Marker({ 
       position: marker.position, 
       map: map, 
       title: results[0].formatted_address 
      }), 
      myOptions = { 
       disableAutoPan: true, 
       maxWidth: 0, 
       pixelOffset: new google.maps.Size(-240, 0), 
       zIndex: null, 
       boxStyle: { 
        background: "url('tipbox.gif') no-repeat", 
        opacity: 0.9, 
        width: "454px" 
       }, 
       closeBoxMargin: "10px 10px 2px -20px", 
       closeBoxURL: "assets/image/x.png", 
       infoBoxClearance: new google.maps.Size(1, 1), 
       isHidden: false, 
       pane: "floatPane", 
       enableEventPropagation: false 
      }; 

     $.ajax({ 
      url: url+marker.position.ob+","+marker.position.pb+"", 
      dataType: "jsonp", 
      success: function (data) { 
       myOptions.content = template(data); 
       var ib = new InfoBox(myOptions); 
       ib.open(map, marker); 
      } 
     }); 

    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 

    }); 
} 
Смежные вопросы