使用ajax加载谷歌地图API时出错

时间:2011-11-11 07:35:48

标签: php jquery google-maps

我有一个文件index.php使用jquery:

<div id="nav"><a href="#"><input type="hidden" value="36.753671,-119.768336" />test</a></div>
<script type="text/javascript">
$(document).ready(function(){
    $('#nav a').click(function() {
        var id = $(this).find('input:hidden').val();
        var dataString = 'id='+id;
        $.ajax({
           type: 'POST',
           url: 'map.php',
           data: dataString,
           cache: false,
           success: function(html) {
                $('.map').html(html);
           } 
        });
    });
});
<div class="map"></div>
map.php中的

$id = $_POST['id'];
    <script src="http://maps.google.com/mapsfile=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAA" type="text/javascript"></script>
    <body onload="initialize()" onunload="GUnload()">
        <div id="map_canvas" style="width: 200; height: 200px"></div>
    </body>
    <script type="text/javascript">
    function initialize() {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(<?php echo $id ?>), 16);
            map.setUIToDefault();
        }
    }
    </script>
  

当我输入输入id =('36.753671,-119.768336')时ajax不是结果,   我可以帮帮我吗?我想要这个想法

1 个答案:

答案 0 :(得分:1)

我看到map.php中的脚本标记在PHP代码中,不应该是。两种方法来解决。 1)

$id = $_POST['id'];
?> //close php tag here
    <body onload="initialize()" onunload="GUnload()">
        <div id="map_canvas" style="width: 200; height: 200px"></div>
    </body>
    <script type="text/javascript">
    function initialize() {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(<?php echo $id ?>), 16);
            map.setUIToDefault();
        }
    }
    </script>

2)

$id = $_POST['id'];
   echo '<script src="http://maps.google.com/mapsfile=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAA" type="text/javascript"></script>
    <body onload="initialize()" onunload="GUnload()">
        <div id="map_canvas" style="width: 200; height: 200px"></div>
    </body>
    <script type="text/javascript">
    function initialize() {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(<?php echo $id ?>), 16);
            map.setUIToDefault();
        }
    }
    </script>';