传单的javascript库的JSON数据

时间:2013-01-29 23:26:20

标签: ajax json leaflet geojson

我已经阅读了很多关于geojson但我想知道是否有其他方法。以下是我添加目前兴趣点的方法:

     var map = L.map('map').setView([39.76, -98.5], 4);
 L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);



var marker = L.marker([33.1958, -117.3786]).addTo(map);

marker.bindPopup("<b>Trainers</b><br>Oceanside, CA.").openPopup();
var marker = L.marker([38.9125, -75.4283]).addTo(map);
marker.bindPopup("<b>Trainers</b><br>Milford, DE.").openPopup();
var marker = L.marker([41.26129, -95.93262]).addTo(map);
marker.bindPopup("<b>Trainers</b><br>Omaha, NE.").openPopup();
var marker = L.marker([25.77516, -80.2002]).addTo(map);
marker.bindPopup("<b>Trainers</b><br>Miami, FL.").openPopup();

//map.on('moveend', onMapMove);


//On mouse over will display the coordinates
map.on('mousemove', function (e) {
        var latlng = e.latlng;
        document.getElementById('latlong').innerHTML = latlng;}, 
    this);

这是我的ajax:

  $(document).ready(function(){ 
    $.ajax({
      type: 'POST',     
      dataType: "json",
      url: '<?php echo matry::base_to('tests/map_it');?>',
      success: function (data) 
      {
         alert($.parseJSON(data[0]).id);
        $('#alerts').html(data);
        data[0].id

      }

      });
    });

这是我的php:

$mapit = sql::results("Select * from event.ACS.trainer where inactive is null or inactive=0");
foreach ($mapit as $row)
{
    $return[] = json_encode($row, JSON_FORCE_OBJECT);
}
echo json_encode($return);

我的json对象正在返回我需要的所有正确数据。我现在仍然坚持在我们的javascript库中实现我的兴趣点。

这是我的json输出:

"{"id":19385,"first":"KIRLY","last":"MAELLI","trainer_address1":"19 NE 111TH COT","trainer_address2":null,"CITY":"MIMI","STATE":"FL","trainer_zip":"379","trainer_phone":"(-6490","trainer_fax":null,"trainer_cell":"(-6490","website_trainer_id":115,"trainer_email":"MOBA@YAHOO.COM","trainer_group":"","inactive":null}",
"{"id":19386,"first":"CHY","last":"MEH","trainer_address1":"1014 ROAD","trainer_address2":null,"CITY":"MIORD","STATE":"DE","trainer_zip":"63","trainer_phone":"","trainer_fax":null,"trainer_cell":"(-8811","website_trainer_id":118,"trainer_email":"CAH@AOL.COM","trainer_group":"","inactive":null}",

1 个答案:

答案 0 :(得分:0)

您可以将sql查询的结果转换为服务器端的GeoJSON。这是一个好主意,因为任何了解GeoJSON的客户都可以使用它,而Leaflet将为您解释数据。

创建GeoJSON特征集合并不比创建“普通”JSON复杂。 有关示例,请参阅the specs

{
   "type": "FeatureCollection",
   "features": [
       { "type": "Feature", 
         "properties": { "title": "Trainers", "placename": "Oceanside, CA."}, 
          "geometry": { "type": "Point", "coordinates": [ -117.3786, 33.1958 ] } },
       { "type": "Feature", 
         "properties": { "title": "Trainers", "placename": "Milford, DE."}, 
         "geometry": { "type": "Point", "coordinates": [ -75.4283, 38.9125 ] } }
    ]
}

(注意,与传单相比,纬度/经度对的顺序相反)

在PHP中,您需要填充数组和词组以及json_encode。 未经测试的猜测(但我怀疑它很遥远):

array('type' => 'FeatureCollection', 
      'features' => array(
           array( "type" => "Feature",
                  "properties" => array("title" => "Trainers", 
                                        "placename" => "Oceanside, CA."),
                  "geometry" => array( "type" => "Point", 
                                  "coordinates" => array( -117.3786, 33.1958 ))),
           array( "type" => "Feature",
                  "properties" => array("title" => "Trainers", 
                                        "placename" => "Milford, DE."),
                  "geometry" => array( "type" => "Point", 
                                  "coordinates" => array( -75.4283, 38.9125 )))
       ))

在Leaflet中创建GeoJSON图层时,提供onEachFeature函数来创建包含要素数据的弹出窗口:

function onEachFeature(feature, layer) {
    layer.bindPopup("<b>"+feature.properties.title +
                    "</b><br>"+feature.properties.placename);
}

(但是,您可以按原样保留服务器代码,并在ajax调用的成功函数中创建标记。)

不确定我是否说明了这一点,但是当您想要调试它时,在浏览器中使用开发工具栏会有所帮助。