1

我在此页面上看到了有关如何动态加载标记的示例

https://developers.google.com/maps/articles/phpsqlsearch_v3

我看到了另一个来自 BIOSTALL 的代码点火器 Google-maps api。

但是这个(http://biostall.com/codeigniter-google-maps-v3-api-library)库不会动态加载标记我如何使用它自己的库来实现这一点。

我应该尝试在 map init 上获取标记还是该库提供了一种使用 ajax 加载这些标记的方法

4

2 回答 2

1

Trying the pseudocode suggested by Biostall, this is what i have implemented:

$.ajax({
          url: '*URL*',
          type: "POST",
          data: ({value : *value*}),
          dataType: "json", //retrieved Markers Lat/lng in Json, thus using this dataType
          success: function(data){
            //Removing already Added Markers//////////
                for(var i=0; i < markers.length; i++){
                    markers[i].setMap(null);
                }
                markers = new Array();
            //////////////////////////////////////////
            // Adding New Markers////////////////////

                for (var i = 0, len = data.length; i < len; ++i) { // Iterating the Json Array
                    var d = data[i];

                    var lat = parseFloat(d.lattitude);
                    var lng = parseFloat(d.longitude);  
                    var myLatlng = new google.maps.LatLng(lat,lng);

                    var marker = {
                        map:map,
                        position:myLatlng // These are the minimal Options, you can add others too
                    };
                    createMarker(marker);
                }
            }
       }
    );

Note: If an array of Markers is being sent to this ajax call, it must be json encoded with the php function json_encode(). And thus you can use the dataType: "json" as mentioned in the ajax call parameters.

This worked for me, hope this might help.

于 2013-06-19T08:03:46.443 回答
1

首先,感谢您使用我的图书馆。值得注意的是,该库只是简化 Google 地图代码生成的一种方式。它代表您构建 JavaScript 和 HTML,从而可以快速轻松地将地图添加到您的页面。

开发人员可能希望通过一百万零一种方式与 Google Maps API 进行交互,并且该库不可能满足每一个实例的需求。因此,有时在这样的定制情况下,您可能需要添加自己的代码,以便它按您的需要执行。

因此,我是否建议您在 echo $map['js'] 之后简单地添加您需要的自定义 JS。库附带了一个名为 createMarker() 的函数,如果您查看源代码,就会看到该函数。

在伪代码中,这将如下所示:

<?php echo $map['js']; ?>
<script type="text/javascript">

    // Get marker(s) with ajax

    // Call createMarker() function to add marker(s) to map

</script>

我希望这会有所帮助。

于 2013-03-05T08:36:31.350 回答