Javascript 无法从ajax调用中检索google地图标记坐标

Javascript 无法从ajax调用中检索google地图标记坐标,javascript,php,mysql,ajax,google-maps,Javascript,Php,Mysql,Ajax,Google Maps,我试图使用ajax调用从mysql数据库中获取标记,但标记没有显示出来 这是我的connect.php(ajax调用connect.php) 我不知道这是否有效 下面是我的javascript文件,其中包含ajax调用: <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Google Map API V3 with

我试图使用ajax调用从mysql数据库中获取标记,但标记没有显示出来

这是我的connect.php(ajax调用connect.php)

我不知道这是否有效

下面是我的javascript文件,其中包含ajax调用:

<html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 <title>Google Map API V3 with markers</title>
 <style type="text/css">
 body { font: normal 10pt Helvetica, Arial; }
 #map { width: 350px; height: 300px; border: 0px; padding: 0px; }
 </style>
 <script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
 <script type='text/javascript' src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
  <script type='text/javascript' src='http://code.jquery.com/jquery-ui-1.8.14.custom.min.js'></script>


<script>
function initialize() {
var myLatlng = new google.maps.LatLng(51.514980,-0.144328);
var mapOptions = {
zoom: 14,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions);




    //When the Window finishes loading...
    $(window).load(function () {

        //Carry out an Ajax request.
        $.ajax({
            url: 'connect.php',
            success:function(data){
                //Loop through each location.
                $.each(data, function(){
                    //Plot the location as a marker
                    var pos = new google.maps.LatLng(this.lat, this.lon); 
                    new google.maps.Marker({
                        position: pos,
                        map: map
                    });
                });
            }
        });

    });
}
google.maps.event.addDomListener(window, 'load', initialize);

</script>





<body style="margin:0px; border:0px; padding:0px;">
 <div id="map"></div>
 </body>
 </html>

带标记的谷歌地图API V3
正文{font:normal 10pt Helvetica,Arial;}
#映射{宽度:350px;高度:300px;边框:0px;填充:0px;}
函数初始化(){
var mylatng=new google.maps.LatLng(51.514980,-0.144328);
变量映射选项={
缩放:14,
中心:myLatlng
}
var map=new google.maps.map(document.getElementById('map'),mapOptions);
//当窗口完成加载时。。。
$(窗口)。加载(函数(){
//执行Ajax请求。
$.ajax({
url:'connect.php',
成功:功能(数据){
//在每个位置循环。
$.each(数据,函数(){
//将该位置绘制为标记
var pos=new google.maps.LatLng(this.lat,this.lon);
新的google.maps.Marker({
职位:pos,,
地图:地图
});
});
}
});
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
下面是我的数据库图片:


我已经为此挣扎了两天了

请执行以下操作:

 $.ajax({
            url: 'connect.php',
            dataType: 'json',
            success:function(data){
            ...
或者:

success: function(data){
   data = $.parseJSON(data);
...
另外,您的$。每个都不正确,请进行以下调整:

$.each(data, function(ind, val){
                    //Plot the location as a marker
                    var pos = new google.maps.LatLng(val.lat, val.lon); 
                    new google.maps.Marker({
                        position: pos,
                        map: map
                    });
                });
此外,您需要按如下方式获取JSON:

{"lat": "-73.899877", "lon" : "59.8765"}
为此,您需要处理PHP代码

PHP代码更改

$dbname            ='u769748933_tr'; //Name of the database
$dbuser            ='u769748933_ta'; //Username for the db
$dbpass            ='adamas'; //Password for the db
$dbserver          ='mysql.1freehosting.com'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
$query = mysql_query("SELECT 'lat','lon' FROM poi_example");
$result = array();
while($row = mysql_fetch_array($query)){
    array_push($result, array(
        'lat' => $row['lat'],
        'lon' => $row['lon']
    ));
}

echo json_encode($result);

在sql中选择字段时,应该使用反勾号,而不是单引号!此外,在未测试的情况下,您可能希望逐行检索记录,并将结果分配给一个数组,该数组在最后回显以供javascript回调使用

<?php

    $dbname            ='u769748933_tr'; //Name of the database
    $dbuser            ='u769748933_ta'; //Username for the db
    $dbpass            ='adamas'; //Password for the db
    $dbserver          ='mysql.1freehosting.com'; //Name of the mysql server

    $dbcnx = mysql_connect( $dbserver, $dbuser, $dbpass );
    mysql_select_db( $dbname ) or die('unable to connect to database');

    $data=array();
    $query = mysql_query("SELECT `lat`,`lon` FROM `poi_example`");
    if( $query ){

        while( $rs=mysql_fetch_assoc( $query ) ){
            $data[]=array( 'lat'=>$rs['lat'], 'lng'=>$rs['lng'] );
        }

    }

    header('Content-Type: application/json');
    echo json_encode( $data );
?>


您是否尝试打印成功接收的数据<代码>控制台日志(数据)另外AJAX请求正在返回200状态代码?任何打印控制台的示例?inspect元素将为您完成任务。success函数下的console.log(数据)。并检查您的控制台。php文件是罪魁祸首,它没有如下打印:{“lat”:“-73.899877”,“lon”:“59.8765”}有什么建议吗?请共享数据库表pic
$dbname            ='u769748933_tr'; //Name of the database
$dbuser            ='u769748933_ta'; //Username for the db
$dbpass            ='adamas'; //Password for the db
$dbserver          ='mysql.1freehosting.com'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
$query = mysql_query("SELECT 'lat','lon' FROM poi_example");
$result = array();
while($row = mysql_fetch_array($query)){
    array_push($result, array(
        'lat' => $row['lat'],
        'lon' => $row['lon']
    ));
}

echo json_encode($result);
<?php

    $dbname            ='u769748933_tr'; //Name of the database
    $dbuser            ='u769748933_ta'; //Username for the db
    $dbpass            ='adamas'; //Password for the db
    $dbserver          ='mysql.1freehosting.com'; //Name of the mysql server

    $dbcnx = mysql_connect( $dbserver, $dbuser, $dbpass );
    mysql_select_db( $dbname ) or die('unable to connect to database');

    $data=array();
    $query = mysql_query("SELECT `lat`,`lon` FROM `poi_example`");
    if( $query ){

        while( $rs=mysql_fetch_assoc( $query ) ){
            $data[]=array( 'lat'=>$rs['lat'], 'lng'=>$rs['lng'] );
        }

    }

    header('Content-Type: application/json');
    echo json_encode( $data );
?>