简体   繁体   English

将js代码插入Google Map中的信息框

[英]Insert js code to infobox in google map

Here I have a code - google custom search - which have a string and automaticly show results based on string... 在这里,我有一个代码-Google自定义搜索-该代码具有一个字符串,并根据字符串自动显示结果...

CODE: http://jsbin.com/UgUxEKo/3/edit 代码: 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>

Now I want to put this code into infobox in my map: 现在,我想将此代码放入地图中的信息框:

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

So I add to info box google custom search code and $window.load function, and this for other elements but for infowindow dont. 因此,我将添加到信息框的Google自定义搜索代码和$ window.load函数,这用于其他元素,但不适用于infowindow。

How I can call this function but when infobox is created ? 创建信息框后如何调用此函数? So not on $window.load just when InfoBox is created? 因此,不是仅在创建InfoBox时在$ window.load上吗?

Is it possible? 可能吗?

Try the below code. 试试下面的代码。 I have not tried. 我没有尝试过。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM