douyong7199 2017-04-15 16:48 采纳率: 0%
浏览 32

在我的PHP模板中包含Google地图

So I have a simple website template I mocked up in php, each purpose separated into its own file; for example Home, Services, Find Us, Functions, Header, Content etc etc. What I'm trying to do is get google maps to display the location specified in the lat / lng however nothing seems to be showing up. For the other parts of the findus.php page I've been setting the content as follows:

    $the_title = 'Find Us';
    $the_content = '<h3>Address<h3>';

And then calling it on the page.php page as follows:

    <h2><?php echo $the_title; ?></h2>
    <?php echo $the_content; ?>

This works fine. For the GMaps API it doesn't. Underneath my $content variables I close the php tags and have the following code:

<div id="map"></div>
<script>
       var map;
        function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: *removed*, lng: *removed*},
        zoom: 8
    });
  }
</script>

<?php $map = '<script src="https://maps.googleapis.com/maps/api/js?key=*removed*&callback=initMap"
async defer></script>' ?>

Notice I opened a new php tag to set $map to the api call. For some reason my map isn't showing on the page.php page (even making sure css sizing is working just in case..). I've been trying to call it with both of the following:

<?php echo $map ?>
<?php print_r($map) ?>

If anyone could help I'd greatly appreciate it. EDIT Ok, I copied the code into a single html page and it works fine when outputting that to the browser. Anyone know if there is a reason its not working inside my findus.php file? Hell, copying and pasting it to page.php didn't output it anywhere so I'm a bit stumped to be honest. Even tried using the html file as an iframe to output the map but it's not working.. EDIT Ok, the code for the Find Us page is below:

<?php
    $the_title = 'Find Us';
    $the_content = '<h3>Address<h3>';
    $the_content2 = '*removed*'

$img = '<img src="map.png" height="350px" width="500px" id=mapimg>';
?>

<div id="map">
 <script>
  var map;
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: *removed*, lng: *removed*},
      zoom: 15
    });
  }
</script>
<script async src="https://maps.googleapis.com/maps/api/js?key=*removed*&callback=initMap"></script>
</div>
<?php include('page.php'); ?>

The CSS styling is as follows:

#map {
height: 250px;
width: 250px;
clear: both;
z-index: -1;
}

The code for the page.php file is below:

<?php include('header.php'); ?>
        <div id="content">
            <div id="main">
                <h2><?php echo $the_title; ?></h2>
                <?php echo $the_content; ?>
                <?php echo $the_content2; ?>
                <?php echo $video ?>
            <?php echo $img ?>
            </div>


            <?php include('sidebar.php'); ?>
        </div>
<?php include('footer.php'); ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP