0

我一直在尝试使用 MapIT API 制作地图(传单)。

由于存在跨浏览器问题,我无法使用 javascript,因此我将信息发送到 PHP,PHP 获取信息并将其回显给函数:

Javascript函数:

    reqwest({
    url: 'php/ajax.php',
    data: 'switch=mapdetail&mapitid=' + mainid,
    type: 'json',
    success: function(data) {
    console.log('passed ' + data);
        var area = new L.GeoJSON(data);
        map.addLayer(area);

        var bounds = new L.LatLngBounds();
        area._iterateLayers(function (layer) {
            if (layer instanceof L.MultiPolygon) {
                layer._iterateLayers(function(layer2) {
                    var new_bounds = layer2.getBounds();
                    bounds.extend(new_bounds.getSouthWest());
                    bounds.extend(new_bounds.getNorthEast());
                });
            }
            if (layer instanceof L.Polygon) {
                bounds = layer.getBounds();
            }
        }, area);
        map.fitBounds(bounds);
    }
});     

PHP位:

    $mapid = $_POST['mapitid'];

    $thedata = json_encode("http://mapit.mysociety.org/area/".mapid.".geojson?simplify_tolerance=0.0001");

    echo thedata;

在 chrome 开发工具中,我将其作为 concole.log (通过 [object XMLHttpRequest]

其次是 未捕获的错误:无效的 GeoJSON 对象。

我猜当数据返回时,它是某种格式,但我看不到。以下是网站直接提供的格式:

http://mapit.mysociety.org/area/2514.geojson?simplify_tolerance=0.0001

更新编辑

好的,我现在可以在 Chrome 开发工具中看到代码了。

新代码:Javascript:

 $.ajax({
    url: 'php/ajax.php',
    data: 'switch=mapdetail&mapitid=59868',
    type: 'POST',
    dataType: 'json',
    success: function(data) {

    console.log(data);
        var area = new L.GeoJSON(data);
        map.addLayer(area);

        var bounds = new L.LatLngBounds();
        area._iterateLayers(function (layer) {
            if (layer instanceof L.MultiPolygon) {
                layer._iterateLayers(function(layer2) {
                    var new_bounds = layer2.getBounds();
                    bounds.extend(new_bounds.getSouthWest());
                    bounds.extend(new_bounds.getNorthEast());
                });
            }
            if (layer instanceof L.Polygon) {
                bounds = layer.getBounds();
            }
        }, area);
        map.fitBounds(bounds);
    }
});                 

新的PHP:

    $mapid = $_POST['mapitid'];

    $thedata = json_encode(file_get_contents("http://mapit.mysociety.org/area/".$mapid.".geojson?simplify_tolerance=0.0001"));

    echo $thedata;

这是结果:

maind id = 59868 { "type": "Polygon", "coordinates": [ [ [ -2.487218, 51.524673 ], [ -2.486795, 51.525041 ], [ -2.486594, 51.525985 ], [ -2.487938, 51.526411 ], [ -2.488176, 51.527001 ], [ -2.488628, 51.527298 ], [ -2.490577, 51.527407 ], [ -2.490898, 51.527912 ], [ -2.490764, 51.528206 ], [ -2.490262, 51.528182 ], [ -2.489666, 51.528908 ], [ -2.489246, 51.528918 ], [ -2.488917, 51.529388 ], [ -2.488017, 51.529577 ], [ -2.488903, 51.530538 ], [ -2.490376, 51.531367 ], [ -2.491287, 51.531554 ], [ -2.493421, 51.532469 ], [ -2.493637, 51.532719 ], [ -2.493405, 51.532908 ], [ -2.494018, 51.533586 ], [ -2.493813, 51.533668 ], [ -2.494053, 51.533871 ], [ -2.494691, 51.533692 ], [ -2.494773, 51.533975 ], [ -2.495049, 51.534001 ], [ -2.497023, 51.535115 ], [ -2.497958, 51.535299 ], [ -2.498869, 51.535796 ], [ -2.503044, 51.537171 ], [ -2.505539, 51.538153 ], [ -2.505462, 51.538237 ], [ -2.505768, 51.538400 ], [ -2.506749, 51.538587 ], [ -2.509226, 51.539616 ], [ -2.510444, 51.539373 ], [ -2.516245, 51.537093 ], [ -2.519487, 51.534951 ], [ -2.519770, 51.535112 ], [ -2.518834, 51.535661 ], [ -2.519198, 51.535847 ], [ -2.520104, 51.535281 ], [ -2.520993, 51.535800 ], [ -2.523176, 51.536491 ], [ -2.523705, 51.536050 ], [ -2.524164, 51.535015 ], [ -2.524400, 51.535036 ], [ -2.526038, 51.535597 ], [ -2.528674, 51.537841 ], [ -2.530464, 51.538917 ], [ -2.531443, 51.539279 ], [ -2.533221, 51.540257 ], [ -2.535901, 51.541205 ], [ -2.538733, 51.541768 ], [ -2.540781, 51.542575 ], [ -2.540439, 51.542960 ], [ -2.540130, 51.543027 ], [ -2.540296, 51.543266 ], [ -2.540235, 51.543512 ], [ -2.538972, 51.544100 ], [ -2.538923, 51.544262 ], [ -2.539178, 51.544446 ], [ -2.538498, 51.544564 ], [ -2.538700, 51.544894 ], [ -2.538134, 51.545338 ], [ -2.537945, 51.546351 ], [ -2.537423, 51.546550 ], [ -2.537010, 51.546434 ], [ -2.535453, 51.546656 ], [ -2.533427, 51.547221 ], [ -2.531784, 51.547893 ], [ -2.530263, 51.548780 ], [ -2.529550, 51.549392 ], [ -2.526786, 51.550568 ], [ -2.526825, 51.550845 ], [ -2.526537, 51.551310 ], [ -2.526124, 51.551516 ], [ -2.526089, 51.551792 ], [ -2.525607, 51.552004 ], [ -2.525590, 51.552291 ], [ -2.525345, 51.552302 ], [ -2.525157, 51.552636 ], [ -2.524847, 51.552626 ], [ -2.522527, 51.553966 ], [ -2.522093, 51.553866 ], [ -2.522108, 51.554036 ], [ -2.521753, 51.554080 ], [ -2.521573, 51.554376 ], [ -2.521406, 51.554261 ], [ -2.521062, 51.554511 ], [ -2.520244, 51.554625 ], [ -2.519966, 51.554989 ], [ -2.519671, 51.554849 ], [ -2.517060, 51.555948 ], [ -2.513820, 51.555008 ], [ -2.513911, 51.554328 ], [ -2.513547, 51.554467 ], [ -2.510762, 51.553441 ], [ -2.510516, 51.552596 ], [ -2.509840, 51.551590 ], [ -2.509863, 51.551092 ], [ -2.509555, 51.551038 ], [ -2.509897, 51.550668 ], [ -2.508169, 51.550138 ], [ -2.506176, 51.547936 ], [ -2.506093, 51.548065 ], [ -2.502299, 51.547223 ], [ -2.499534, 51.546897 ], [ -2.496746, 51.547906 ], [ -2.494741, 51.547731 ], [ -2.492722, 51.548296 ], [ -2.490157, 51.549388 ], [ -2.486311, 51.549706 ], [ -2.486413, 51.549090 ], [ -2.486582, 51.548905 ], [ -2.487810, 51.548293 ], [ -2.487170, 51.547647 ], [ -2.487368, 51.546444 ], [ -2.487133, 51.546054 ], [ -2.486300, 51.545561 ], [ -2.485866, 51.544907 ], [ -2.484368, 51.544669 ], [ -2.483687, 51.544353 ], [ -2.483522, 51.544032 ], [ -2.483795, 51.543799 ], [ -2.483713, 51.543623 ], [ -2.482981, 51.543752 ], [ -2.482626, 51.544099 ], [ -2.481342, 51.544563 ], [ -2.481302, 51.544783 ], [ -2.480959, 51.545043 ], [ -2.479887, 51.545650 ], [ -2.479938, 51.545885 ], [ -2.479773, 51.546057 ], [ -2.478910, 51.546471 ], [ -2.477119, 51.546819 ], [ -2.476491, 51.547366 ], [ -2.475257, 51.547549 ], [ -2.474229, 51.547278 ], [ -2.474094, 51.547572 ], [ -2.473668, 51.547696 ], [ -2.472217, 51.547233 ], [ -2.472826, 51.547159 ], [ -2.472977, 51.546795 ], [ -2.472157, 51.546492 ], [ -2.471731, 51.546512 ], [ -2.471710, 51.546357 ], [ -2.470886, 51.546099 ], [ -2.470910, 51.545976 ], [ -2.470226, 51.545737 ], [ -2.469964, 51.545485 ], [ -2.471224, 51.543133 ], [ -2.471587, 51.541971 ], [ -2.472669, 51.541868 ], [ -2.473324, 51.540817 ], [ -2.471415, 51.540643 ], [ -2.471310, 51.539609 ], [ -2.471503, 51.538436 ], [ -2.470360, 51.538025 ], [ -2.469045, 51.537066 ], [ -2.468343, 51.536020 ], [ -2.467243, 51.535040 ], [ -2.466285, 51.534661 ], [ -2.466215, 51.533903 ], [ -2.465796, 51.533080 ], [ -2.464993, 51.532483 ], [ -2.464058, 51.532100 ], [ -2.464207, 51.531620 ], [ -2.465837, 51.531453 ], [ -2.467140, 51.529029 ], [ -2.468394, 51.527443 ], [ -2.471064, 51.524940 ], [ -2.472790, 51.525590 ], [ -2.475423, 51.524617 ], [ -2.475349, 51.524353 ], [ -2.476017, 51.523592 ], [ -2.476650, 51.523409 ], [ -2.478830, 51.523299 ], [ -2.479678, 51.523355 ], [ -2.481826, 51.523899 ], [ -2.484982, 51.524108 ], [ -2.487218, 51.524673 ] ] ] }

未捕获的错误:无效的 GeoJSON 对象。

4

2 回答 2

0

根据您编辑的信息 - 使用那个 JS,那个 PHP 但没有,在json_encode这里可以正常工作,获取并显示边界。您不需要进一步编码 JSON,当您从服务器获取它时它已经是 JSON,您只是通过代理它。

于 2014-03-26T11:59:16.450 回答
0

我认为这是因为您 json_encode URL 而不是您想要的内容。

    $thedata = json_encode(file_get_contents("http://mapit.mysociety.org/area/".$mapid.".geojson?simplify_tolerance=0.0001"));

此外,使用 POST 方法只提供不修改服务器存储数据的信息也不是一件好事。GET 请求在这里就可以了。

于 2012-08-18T11:18:53.943 回答