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 调用之前加载它,然后将它与包含数据的对象连接起来,而不是在“new InfoBox(NewObject)”中传递一个对象

关于如何做到这一点的任何想法,或者是否有不同/更好的方法?

完整脚本

4

1 回答 1

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);
    }

  });
}
于 2013-11-14T11:08:45.563 回答