带有MySQL,PHP和英国邮政编码的Google Maps API

时间:2012-04-07 06:09:30

标签: php mysql ajax api google-maps

我正在寻找一个关于如何从MySQL数据库中提取(英国邮政编码)的脚本或教程,并使用PHP为每个位置的Google地图添加标记,如果存在这样的话。

之前我使用过以下脚本并让它工作得很好,但只能使用lat,lng。

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

任何方向都会很棒,谢谢

修改

这是我到目前为止所做的:

<?php require_once('Connections/dbconnect.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_dbconnect, $dbconnect);
$query_rs_get_postcodes = "SELECT addresses.postcode FROM addresses";
$rs_get_postcodes = mysql_query($query_rs_get_postcodes, $dbconnect) or die(mysql_error());
$row_rs_get_postcodes = mysql_fetch_assoc($rs_get_postcodes);
$totalRows_rs_get_postcodes = mysql_num_rows($rs_get_postcodes);

//query database to retrieve postcode

//store postcode in variable

$postcode = ""/* mysql_result($result,0,"postcode")*/;
?>
<!-- generate google map -->

<!-- google maps key -->    
<script type="text/javascript" src="http://maps.google.com/maps?file=api"></script>

<!-- ajax key -->        
<script type="text/javascript" src="http://www.google.com/uds/api?"></script>    

<script type="text/javascript">
var map;
var localSearch = new GlocalSearch();

var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);


function usePointFromPostcode(postcode, callbackFunction) {

    localSearch.setSearchCompleteCallback(null, 
        function() {

            if (localSearch.results[0])
            {        
                var resultLat = localSearch.results[0].lat;
                var resultLng = localSearch.results[0].lng;
                var point = new GLatLng(resultLat,resultLng);
                callbackFunction(point);
            }else{
                alert("Postcode not found!");
            }
        });    

    localSearch.execute(postcode + ", UK");
}


function setCenterToPoint(point)
{
    map.setCenter(point, 9);
    var marker = new GMarker(point,icon);
    map.addOverlay(marker);
}


function mapLoad() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"),{size:new GSize(347,300)});
        map.addControl(new GLargeMapControl());
        map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_NORMAL_MAP);
    }
}

<!-- This function may not be needed, I haven't tried removing it  -->
function addUnLoadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
      window.onunload = func;
    } else {
      window.onunload = function() {
        oldonunload();
        func();
      }
    }
}

<!-- This call may not be necessary, I haven't tried removing it -->
addUnLoadEvent(GUnload);

</script> 
<script type="text/javascript">
     window.onload = function() {
     mapLoad();
     usePointFromPostcode("<?php echo $row_rs_course_list['postcode']; ?>", setCenterToPoint);
     }
</script>

<!-- use this div to size and position map -->
<div id="map" align="center" style="width:347px; height:300px;"></div>
<?php
mysql_free_result($rs_get_postcodes);
?>

0 个答案:

没有答案