doudi7570 2012-03-08 03:58
浏览 70
已采纳

PHP / mySQL和Google Maps Geocoding

Basically what I have is a sql database with various data points in it (name, email, etc.) and importantly fields that contain addresses. What I am attempting to do is use these addresses (possibly just the zip codes?) to plot pins on a google map with their geocoding system. I know that i need to collect all of the zips from the database in an array and do some sort of foreach for every zip but I am unsure of how to do this. Here is what I have so far, can anyone help me?

<?php
$dbh=mysql_connect('my_database_host.edu','my_database_user','my_database_password');

if (!$dbh)
  {
  die('Could not connect: ' . mysql_error());
  } 
mysql_select_db('my_database_name', $dbh);

$query = "SELECT fldZip FROM tblLocation";
$findzips = mysql_query($query);
while($row =mysql_fetch_array($findzips)) {
    echo $row['fldZip'];
    }

?>

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=my_api_key&sensor=false">
    </script>
    <script type="text/javascript">
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

}

  function codeAddress(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="height:90%;top:30px"></div>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • duanqujing3863 2012-03-08 04:04
    关注

    Instead of using the Javascript-API to geocode the ZIPs use the geocoding-webservice via PHP.

    That also would be a good point to store the LatLng's inside the DB, so you don't have to request them again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘