0

我正在寻找一种将标记添加到我在网页中创建的地图的方法......这是页面的代码......

<link href='//api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.css' rel='stylesheet' />
<script src='//api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.js'></script>

<style>
    #map {
        width: 100%;
        height: 600px;
    }
</style>

<div id='map' />

<script type='text/javascript'>
    var map = L.mapbox.map('map', '[mapname]')

</script>

这会从 mapbox 渲染地图 - 但我无法弄清楚如何编写 Web 服务来提供标记。此数据存储在 SQL 数据库的表中。

我知道我可以加载包含数据的 GeoJSON 文件 - 但我不确定如何创建此文件 - 以及它与常规 JSON 有何不同 - 任何帮助将不胜感激!

谢谢

4

2 回答 2

0

我不知道 GeoJSON,但这是您使用 Google Maps v3 API 处理它的方式:

对于一个标记:

        lng = (4.502384184313996, 4.461185453845246);
        lat = (51.011527400014664, 51.02974935275779);
        cur_loc = new google.maps.LatLng(lat, lng);

        var marker = new google.maps.Marker({
            position: cur_loc, //To be defined with LatLng variable type
            draggable: false, 
            animation: google.maps.Animation.DROP,
            icon: image
        });

        // To add the marker to the map, call setMap();
        marker.setMap(map);

对于从 MySQL (Ajax) 检索的多个标记:

        google.maps.event.addListener(map, 'idle', function () {
            var bounds = map.getBounds();
            var ne_lat = bounds.getNorthEast().lat();
            var ne_lng = bounds.getNorthEast().lng();
            var sw_lat = bounds.getSouthWest().lat();
            var sw_lng = bounds.getSouthWest().lng();
            // Call you server with ajax passing it the bounds
            $.ajax({
                  type: "GET",
                  url: "http://www.zwoop.be/develop/home/bars/bars_get_markers.php",
                  data: {
                      'ne_lat': ne_lat,
                      'ne_lng': ne_lng,
                      'sw_lat': sw_lat, 
                      'sw_lng': sw_lng
                  },
                  datatype: "json",
                  success: function(data){
                    if(data){
                        // In the ajax callback delete the current markers and add new markers
                        function clearOverlays() {
                            for (var i = 0; i < array_markers.length; i++ ){
                                array_markers[i].setMap(null);
                            }
                            array_markers = [];
                        };
                        clearOverlays();

                        //parse the returned json obect
                        //Create a marker for each of the returned objects                        
                        var obj  = $.parseJSON(data);
                        $.each(obj, function(index,el) {
                            var bar_position = new google.maps.LatLng(el.lat, el.lng);
                            image_bar = "http://www.sherv.net/cm/emoticons/drink/whiskey-smiley-emoticon.gif";

                            var marker = new google.maps.Marker({
                                position: bar_position,
                                map: map, 
                                icon: image_bar
                                });
                            //Add info window. With HTML, the text format can be edited. 
                            google.maps.event.addListener(marker, 'click', function() {
                                if (infowindow){
                                    infowindow.close();
                                };
                                content = "<div id='infowindow_container'><h3><a class='profile_name_bar' href='#' id='" + el.profile_id + "'>"+el.profile_name+"</a></h3></div>";
                                infowindow = new google.maps.InfoWindow({ 
                                    content: content
                                });
                                infowindow.open(map,marker);
                            });                            
                            array_markers.push(marker);
                        });

                        //Place the markers on the map
                        function setAllMap(map) {
                          for (var i = 0; i < array_markers.length; i++) {
                            array_markers[i].setMap(map);
                          }
                        }
                        setAllMap(map);

                        //marker clusterer
                        var zoom = 17;
                        var size = size ==-1?null:size;
                        var style = style ==-1?null:style;
                        var markerCluster = new MarkerClusterer(map, array_markers,{maxZoom:zoom,gridSize:size});
                    }
                },
                  error: function (xhr, ajaxOptions, error) {
                        alert(error);
                        }
                    })             
          });

此代码查看地图的视口并动态加载标记。当您缩放/平移时,代码将查询数据库:地图边界的 LatLng 坐标被发送到服务器,并且在数据库中找到的标记坐标从 Ajax 调用返回。标记坐标在客户端加载到数组中并写入地图。我使用标记聚类器来避免标记拥挤。

我希望这有帮助。我不知道您使用的插件的好处。

于 2013-09-11T13:13:55.667 回答
0

我正在做类似的事情,这就是我要做的。

我使用 PHP 从 MySQL 数据库中获取坐标并返回如下内容:

var geoJson = [
    {
        type: 'Feature',
        "geometry": { "type": "Point", "coordinates": [-77.03, 38.90]},
        "properties": {}
    },
    {
       type: 'Feature',
       "geometry": { "type": "Point", "coordinates": [-64.567, 32.483]},
       "properties": {}
    }      
];

PHP 文件如下所示:

<?php  
            // Connect
            $link = mysqli_connect("[host]","[username]","[password]","[database-name]") or die("Error " . mysqli_error($link));

            // Get the coordinates of the places
            $query = "SELECT * FROM `places`";
            $places = $link->query($query);

                var geoJson  = [<?php

                // Loop through places and plot them on map 

                // This is just building the JSON string

                $i = 1;
                while($venue = $venues->fetch_assoc()): 

                    if($i > 1){ echo ","; } ?>
                    {
                        type: 'Feature',
                        "geometry": { "type": "Point", "coordinates": [<?php echo $venue['lon']; ?>, <?php echo $venue['lat']; ?>]},
                        "properties": {}
                    }

                    <?php $i++; ?>

                <?php endwhile; ?>

            ];




            map.markerLayer.setGeoJSON(geoJson);

注意 - 这个 PHP 在制作地图的 Javascript 中。

就像我说的那样,对我来说也是早期。上面的代码有效,但就我目前为止。接下来我要看的是 JavaScript 模板,并使用它们以这种方式提取数据。我有一种感觉,这将是一个更好的方法,但我不确定。无论如何,这可能对您有用,如果您对它有任何进一步了解,请告诉我:)

于 2013-10-25T12:07:39.447 回答