谷歌地图API和PHP


Google Maps API and PHP

我有这个代码

http://jsfiddle.net/DanielMontenegro/7pdU4/3/

现在,假设我想把地图放在一个网站上,让人们通过地理编码服务报告一些特定的事件。我想问一下,我如何才能在一个表中获得该事件的地址/坐标,并将所有用户报告集中到一个数据库中。

在文档的"文章"部分,有很多关于Google Maps API v3与PHP/MMySQL接口的好信息

这个看起来像是回答了你的问题;从信息窗口到数据库:保存用户添加的表单数据

请参阅下面的代码并尝试。

我使用的是php静态数组,您可以根据需要连接数据库并创建数组。请参阅下面的数组。

<?php  $item = array('jaipur, rajasthan,india', 'udaipur, rajasthan,india'); ?>

下面我的代码看看。

<html>
    <head>
        <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyDwFjOazEAe1MI7nHV6vUwkWytOp8LH2Zk" type="text/javascript"></script>
    </head>
    <body onload="initialize();">
    <?php  $item = array('jaipur, rajasthan,india', 'udaipur, rajasthan,india'); ?>
    <script>
    var map = null;
    var geocoder = null;
    function initialize() {
            if (GBrowserIsCompatible()) {

                    map = new GMap2(document.getElementById("map_canvas"));
                    var addresses = ["<?php echo implode ('","', $item); ?>"]

                    var geocoder = new GClientGeocoder();
                    //var addresses = ["A II Z, Ibn Battuta Mall, Sheikh Zayed Road, 5th Interchange, Jebel Ali Village, Dubai","A. Testoni,Dubai Festival City Mall, Ground Floor, Dubai", "Abdulla Hussain Khunji, The Dubai Mall,Downtown, Abu Dhabi"];
                    var curIndex = 0;
                    function showAddress() {
                      var _cur = addresses[curIndex];
                      geocoder.getLatLng(
                        _cur,
                        function(point) {
                          if (!point) {
                            //alert(_cur + " not found");
                            //map.setCenter(new GLatLng(0, 0), 6);
                            //map.setUIToDefault();
                          } else {
                            //console.log(_cur+":"+point);
                            //alert(_cur);
                                    var cafeIcon = new GIcon(G_DEFAULT_ICON);
                                    // Set up our GMarkerOptions object
                                    markerOptions = { icon:cafeIcon };
                                    map.setCenter(point, 6);
                                    var marker = new GMarker(point, markerOptions);
                                    map.addOverlay(marker);
                                    var sales = new Array();
                                    sales = _cur.split("|");

                                    //Add click event on push pin to open info window on click of the icon
                                    GEvent.addListener(marker, "click", function() {
                                            marker.openInfoWindowHtml("<p class='para1bold' style='"font-weight: bold;'">Address <br /><span class='para1' style='font-weight:normal;'>" + sales[1] + "</span></p>");
                                    });
                                    //Provides map,satellite,hybrid and terrain views in the map along with zoom control

                                    map.setUIToDefault();
                          }
                          //do next after done
                          curIndex++;
                          if(curIndex<addresses.length)
                            showAddress();
                        }
                      );
                    }
                    showAddress();
       }  
    }
    </script>
    <div id="map_canvas" style="width:100%; height:750px;"></div>
  </body> 
</html>

您需要设置某种类型的动作侦听器(最好是一个侦听器,用于侦听用户在地图上的单击)。然后,只需对生成的lat/lng进行地理编码,并将您收到的内容存储在表中。

  1. 用户点击地图上的一个点
  2. 点击事件获取用户点击的纬度
  3. Lat/lng是使用Ajax为文本地址进行地理编码的
  4. Lat/lng和文本地址都并排存储在数据库表中