【问题标题】:PHP function for Reverse Geocoding for street number用于街道号反向地理编码的 PHP 函数
【发布时间】:2016-01-15 23:51:54
【问题描述】:

以下代码 from this post 对我有用,但它会返回完整地址(街道编号、街道名称、城市、州、邮政编码、国家/地区)。有没有办法让它只返回街道号码和名称而不是完整地址? Google 并没有提供太多帮助。

<?
  function getaddress($lat,$lng)
  {
     $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
     $json = @file_get_contents($url);
     $data=json_decode($json);
     $status = $data->status;
     if($status=="OK") {
       return $data->results[0]->formatted_address;
     } else {
       return false;
     }
  }

  $lat= 26.754347; //latitude
  $lng= 81.001640; //longitude
  $address= getaddress($lat,$lng);
  if($address) {
    echo $address;
  } else {
    echo "Not found";
  }
?>

【问题讨论】:

  • 为什么不从返回的地址中过滤掉门牌号和名字呢?
  • 这就是我想做的,但我不知道该怎么做。

标签: php google-maps google-maps-api-3


【解决方案1】:

经过大量搜索和反复试验,我设法得到了我需要的工作。如果有更有效的方法来做到这一点,我会喜欢反馈。

<?php
    function getAddress($lat, $lon) {
        $url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".
        $lat.",".$lon."&sensor=false";
        $json = @file_get_contents($url);
        $data = json_decode($json);
        $status = $data->status;
        $address = '';
        if($status == "OK"){
            foreach($data->results[0]->address_components as $address_component) {
                if(in_array('street_number', $address_component->types)) {
                    $street_number = $address_component->long_name;
                }
                if(in_array('route', $address_component->types)) {
                     $route = $address_component->long_name;
                }
            }
        }
        return $street_number." ".$route;
    }

    echo getAddress($lat,$lon);
?>

【讨论】:

    【解决方案2】:
    <!DOCTYPE HTML>
    
    <head>
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script type="text/javascript">
    var watchID;
    var geoLoc;
    // Get the latitude & longitude;
    function showLocation(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            getAddress(latitude, longitude);
    }
    // Handel any errors that my come
    function errorHandler(err) {
      if(err.code == 1) {
        alert("Error: Access is denied!");
      }else if( err.code == 2) {
        alert("Error: Position is unavailable!");
      }
    }
    // Get the location of the current location settings
    function getLocation(){
        $("#output").html("Getting Location. Please Wait...");
       if(navigator.geolocation){
          // timeout at 60000 milliseconds (60 seconds)
          var options = {enableHighAccuracy:true,maximumAge:30000,timeout:27000};
          geoLoc = navigator.geolocation;
          watchID = geoLoc.watchPosition(showLocation, errorHandler, options);
       }else{
          alert("Sorry, browser does not support geolocation!");
       }
    }
    // Get the address
    function getAddress(latitude, longitude){
        $.get("http://maps.google.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=false", function(data){
            $(data).find("formatted_address").each(function(){
                var unitData = $(this).text();
                $("#output").html("Latitude : " + latitude + "<br> Longitude: " + longitude + "<br>");
                unitData = unitData.split(',');
                var street_number = unitData[0];
                var street_address = unitData[1];
                $("#output").append(street_number);
                $("#output").append(street_address);
                return false;
            });     
        });
    }
    </script>
    </head>
    <html>
      <body>
    <form>
        <input type="button" onclick="getLocation();" value="Get Location"/>
      </form>
    <div id="output"></div>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多