将js代码插入谷歌地图中的信息框

时间:2013-09-03 12:01:46

标签: javascript jquery html google-maps google-custom-search

这里我有一个代码 - 谷歌自定义搜索 - 它有一个字符串并自动显示基于字符串的结果......

代码:http://jsbin.com/UgUxEKo/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上吗?

有可能吗?

1 个答案:

答案 0 :(得分: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);