dtttlua7165 2018-04-08 19:32
浏览 47

使用wordpress模板调用API密钥调用多个谷歌地图

I have the following code for my website, basically I want to display 2 google maps with different locations:

function lokacije(){
$output=" <div id='map'></div>
<script>
    function initMap() {
        var lokacija = {lat: 45.3592940, lng: 14.3495750};
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 16,
            draggable: false,
            scrollwheel: false,
            center: lokacija
        });
        var marker = new google.maps.Marker({
            position: lokacija,
            map: map,
            icon:'http://www.omniaprijevodi.hr/wp 
            content/uploads/2016/06/map_marker.png'
        });
      }
    </script>
    <script async defer src='https://maps.googleapis.com/maps/api/js? 
    key=AIzaSyAglKEMduJgZf1nN42mHkzeNA4jjpI0JA0&callback=initMap'>
    </script>                                               
    ";  

return $output;     
}
add_shortcode("lokacije","lokacije");


function lokacije2(){
    $output=" <div id='map'></div>
    <script>
    function initMap() {
        var lokacija = {lat: 45.813236, lng: 15.996887};
        var map = new google.maps.Map(document.getElementById('map'), {
           zoom: 16,
           draggable: false,
           scrollwheel: false,
           center: lokacija
        });
        var marker = new google.maps.Marker({
            position: lokacija,
            map: map,
            icon:'http://www.omniaprijevodi.hr/wp- 
            content/uploads/2016/06/map_marker.png'
        });
      }
    </script>
    <script async defer src='https://maps.googleapis.com/maps/api/js? 
    key=AIzaSyAglKEMduJgZf1nN42mHkzeNA4jjpI0JA0&callback=initMap'>
    </script>                                               
    ";  

return $output;     
}
add_shortcode("lokacije2","lokacije2");

I call [lokacije] and [lokacije2] from Wordpress template, but I can't get to display them both, in fact none is displayed. Even if I call [lokacije] twice, I get the map to display only the first time - the second time it doesn't appear. Can someone please help me what I'm doing wrong because I'm having a hard time with this one?

Thank you very much in advance!

  • 写回答

1条回答 默认 最新

  • duanlong4890 2018-04-08 19:46
    关注

    First, make sure google api script is loaded before you call the api, and you have to load the script only once. Secondly, your JS code redefines initMap(). Thirdly, you use the same element with ID = map for both maps.

    Use wp_enqueue_script() to load the scripts: https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    and have something like this for the js:

    function initMap(element, lat, lng)
    {
        var map = new google.maps.Map(element, {
           zoom: 16,
           draggable: false,
           scrollwheel: false,
           center: {lat:lat, lng:lng};
        });
    }
    

    and for the php (assuming you have jquery loaded):

    function lokacije1()
    {
        return sprintf('<div map lat="12.34" lng="56.78"></div>');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)