0

我已经阅读了很多关于 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}",
4

1 回答 1

0

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

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

{
   "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 调用的成功函数中创建标记。)

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

于 2013-01-30T14:29:54.667 回答