【问题标题】:Show JSON Response (lat, lon) in google map在谷歌地图中显示 JSON 响应(纬度、经度)
【发布时间】:2016-12-08 17:21:47
【问题描述】:

我正在尝试构建一个应用程序,该应用程序从数据库 (mysql) 中获取用户数据(纬度、经度),然后在谷歌地图上显示他们的位置。

这里是代码示例:

function initMap() {
        var mapDiv = document.getElementById('map');
        var map = new google.maps.Map(mapDiv, {
        center: {lat: 33.7167, lng: 73.0667},
        zoom: 11
    });

    makeRequest('get_locations.php', function(data) {
        //alert("abc");
        var data = JSON.parse(data.responseText);
        //window.alert(data);

        for (var i = 0; i < data.length; i++) {
            //display(data[i]);
            displayLocation(data[i]);
        }
    });
}   


function makeRequest(url, callback) {
    var request;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
    } else {
        request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
    }
    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {
            callback(request);
        }
    }
    request.open("GET", url, true);
    request.send();
}

get_locations.php

<?php
    include('connection.php');
    $l= array();
    $result = mysqli_query($con,"SELECT * FROM users");
    while ($row = mysqli_fetch_assoc($result)) {
        $l[] = $row;
    }
    $j = json_decode($l, true);
     //echo $j;

?>

有些事情我试图在代码上找到问题但失败了

  1. get_location.php on echo $j; 中显示正确的响应

  2. 在此行之前 var data = JSON.parse(data.responseText); alert("abc") 有效,但在此之后无效,所以我认为问题出在这一行,但不知道为什么会这样, 有什么想法吗?

【问题讨论】:

  • get_location.php 中,您需要使用json_encode 而不是json_decode,并且您还需要回显它,以便ajax 回调可以使用。
  • 哦,我怎么能错过这个?顺便说一句,感谢您的回复:) 它有效

标签: javascript php mysql json google-maps


【解决方案1】:

我认为主要问题是在您需要相反 json_encode 的情况下滥用 json_decode

function initMap() {
        var mapDiv = document.getElementById('map');
        var map = new google.maps.Map(mapDiv, {
        center: {lat: 33.7167, lng: 73.0667},
        zoom: 11
    });

    makeRequest('get_locations.php', function(data) {
        if( data ){
            var data = JSON.parse( data );
            for( var n in data ){
                var obj=data[ n ];

                console.log( '%o', obj );
                displayLocation.call(this,obj);
            }
        }
    });
}   


function makeRequest(url, callback) {
    var request;
        request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {
            callback( request.responseText );
        }
    }
    request.open("GET", url, true);
    request.send();
}




<?php

    include('connection.php');
    $l= array();

    $result = mysqli_query( $con, "SELECT * FROM users" );
    while ( $row = mysqli_fetch_assoc( $result ) ) {
        $l[] = $row;
    }

    /* encode array as a json object */
    $json = json_encode( $l );

    /* send the correct content-type header */
    header('Content-Type: application/json');

    /* send the data */
    exit( $json );

?>

【讨论】:

  • Ram Raider 在上述评论中已解决问题。
猜你喜欢
  • 2014-04-30
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多