【问题标题】:Geocode was not successful for the following reason Zero result - PHP variables地理编码不成功,原因如下零结果 - PHP 变量
【发布时间】:2015-02-19 10:44:10
【问题描述】:

我正在尝试显示从中调用的地址 我的 sql 数据库,我无法显示地址我不断收到此错误“地理编码不成功,原因如下 Zero_Results”

我尝试将完整地址添加到一个变量 $address 中,而不是使用三个变量 $address 、 $county 、 $county 但仍然没有 工作,我想不通。

我想将地址显示为您在图像中看到的标记。

每当我在我的数据库中添加新地址时,我都会在地图中添加新标记。

任何帮助,

提前致谢。

这是我正在使用的代码:

$result = mysqli_query($con,"SELECT * FROM `test`") or die ("Error:      
 ".mysqli_error($con));
     while ($row = mysqli_fetch_array($result))
     {
            $address = $row['address'];
            $county = $row['County'];
            $country = $row['Country'];
     }
        mysqli_close($con); 

        <!DOCTYPE html>
         <html>
          <head>

       <script type="text/javascript"
           src="https://maps.googleapis.com/maps/api/js?                       
           key=mykey&sensor=false">

        </script>


         <script type="text/javascript">
          var geocoder;
          var map;
          function initialize() {
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(          
            53.41291,-8.243889999999965);
            var address = '<?php echo $address.', '.$country.', '.$county; ?                 
          >';

            var myOptions = {
              zoom: 6,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map_canvas"),                  
             myOptions);
            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="width:500px; height:500px"></div>

        </body>
        </html>

【问题讨论】:

  • 收到“ZERO_RESULTS”时发送到地理编码器的地址值是多少? ('&lt;?php echo $address.', '.$country.', '.$county; ?&gt;'; 解析为什么?)它可能不是可地理编码的地址。
  • @geocodezip 只是完整的地址,如 $address store Griffen Road 和 $county 存储的 Dublin 和 $country 存储的 Ireland 都是文本类型。
  • 如果我在the example in the documentation 中输入“Griffen Road, Dublin, Ireland”,它可以工作(不返回 ZERO_RESULTS),所以当你的代码收到报告的错误时,这不是你的代码正在做的事情.请提供一个 Minimal, Complete, Tested and Readable example 来证明该问题。
  • @geocodezip 我知道它是这样工作的,但是当我将它用作 PHP 变量时不起作用
  • @geocodezip 如果我有一个存储在数据库中的地址列表,如何解决这个问题?任何例子或想法

标签: php mysql api google-maps


【解决方案1】:

我的问题解决了,我的解决方案是:

            <script type="text/javascript">
            var geocoder;
            var map;
            function initialize(address) {
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(   
              53.41291,-8.243889999999965);


            var myOptions = {
              zoom: 6,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map_canvas"),            
            myOptions);
            <?php
            require('connection.php');

              $result = mysqli_query($con,"SELECT * FROM `test`") or die  
                ("Error: ".mysqli_error($con));
              $count = 0;


            ?>
            <?php  //Starts while loop so all addresses for the given 
                // information will be populated.
             $addresscounting=0;
               while($row = mysqli_fetch_assoc($result)) //instantiates 
                 // array
           { ?>   


             var address = "<?php echo $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);
              }
            });

         <?php
               $count++;

              } //ends while
          ?>
          }

          </script

【讨论】:

  • 那么你改变了什么?
猜你喜欢
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
相关资源
最近更新 更多