0

在这里,我有一个代码 - 谷歌自定义搜索 - 它有一个字符串并根据字符串自动显示结果......

代码: http: //jsbin.com/Ug ​​UxEKo/3/edit

<script>
  (function() {
    var cx = '004975901230883403229:puwldey9ibm';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:search></gcse:search>

<script>
  $(window).load( function() {
   var input = $('#gsc-i-id1');
   input.val( input.val() + 'las vegas');
     $('input.gsc-search-button').click();
    $('form.gsc-search-box').css('display','none');
  })
</script>

现在我想把这段代码放到我地图的信息框中:

google.maps.event.addListener(marker,'click',function(){
        service.getDetails(request, function(place, status) {
          if (status == google.maps.places.PlacesServiceStatus.OK) {
            var contentStr = '<div class="m_tooltip"><h5>'+place.name+'</h5><p>'+place.formatted_address+'</p>';
            if (!!place.formatted_phone_number) contentStr += '<br>'+place.formatted_phone_number;
            if (!!place.website) contentStr += '<br><a target="_blank" href="'+place.website+'">'+place.website+'</a>';
            if (!!place.photos) contentStr += '<img src='+place.photos[0].getUrl({ 'maxWidth': 300, 'maxHeight': 300 })+'></img>';
            contentStr += '<br>'+place.types+'</p>';
  ***(function() {
    var cx = '004975901230883403229:puwldey9ibm';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
            $(window).load( function() {
   var input = $('#gsc-i-id1');
   input.val( input.val() + 'las vegas');
     $('input.gsc-search-button').click();
    $('form.gsc-search-box').css('display','none');
  })
            contentStr += '<div><gcse:search></gcse:search></div>';***
            //contentStr += '<h5">'+place.reviews[0].text+'</h5></div>';
            ib.setContent(contentStr);
            ib.open(map,marker);
          } else { 
            var contentStr = "<h5>No Result, status="+status+"</h5>";
            ib.setContent(contentStr);
            ib.open(map,marker);
          }
        });

    });
gmarkers.push(marker);

所以我添加到信息框谷歌自定义搜索代码和 $window.load 函数,这适用于其他元素,但对于 infowindow 没有。

我如何调用这个函数但是当信息框被创建时?所以不是在创建 InfoBox 时才在 $window.load 上?

是否可以?

4

1 回答 1

0

试试下面的代码。我没试过。

google.maps.event.addListener(marker,'click',function(){
        service.getDetails(request, function(place, status) {
          if (status == google.maps.places.PlacesServiceStatus.OK) {
            var contentStr = '<div class="m_tooltip"><h5>'+place.name+'</h5><p>'+place.formatted_address+'</p>';
            if (!!place.formatted_phone_number) contentStr += '<br>'+place.formatted_phone_number;
            if (!!place.website) contentStr += '<br><a target="_blank" href="'+place.website+'">'+place.website+'</a>';
            if (!!place.photos) contentStr += '<img src='+place.photos[0].getUrl({ 'maxWidth': 300, 'maxHeight': 300 })+'></img>';
            contentStr += '<br>'+place.types+'</p>';
            contentStr += '<div><gcse:search></gcse:search></div>';

            var cx = '004975901230883403229:puwldey9ibm';
            var gcse = document.createElement('script');
            gcse.type = 'text/javascript';
            gcse.async = true;
            gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(gcse, s);

            var input = $('#gsc-i-id1');
            input.val( input.val() + 'las vegas');
            $('input.gsc-search-button').click();
            $('form.gsc-search-box').css('display','none');

            //contentStr += '<h5">'+place.reviews[0].text+'</h5></div>';
            ib.setContent(contentStr);
            ib.open(map,marker);
          } else { 
            var contentStr = "<h5>No Result, status="+status+"</h5>";
            ib.setContent(contentStr);
            ib.open(map,marker);
          }
        });

    });
gmarkers.push(marker);
于 2013-09-03T12:18:57.680 回答