2017-10-14 3 views
0

からショーアドレス:私はこのようなLatLongブログ表示するクエリを持っている緯度と長い緯度

Latitude : <?=$post_search['lat'];?> 
Long Latitude : <?=$post_search['long'];?> 

サンプル結果:

Latitude : -7.966620399999998 
Long Latitude : 112.000002 

はどのようにして、クエリをベースアドレスを表示する機能を作成することができます。

<?=$post_search['lat'];?> 
<?=$post_search['long'];?> 

私のコードを使用し、このライブラリー:see in pastebin

+3

を意味していますか?その場合は、[google maps reverse geocoding api](https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding)をご覧ください。 – davidchoo12

答えて

0

あなたのライブラリを知りません。しかし、それは単にライブラリなしで作る。 googleapisジオコードを使用する必要があります。

それはあなたがあなたのリクエストでlatlngを設定し、これを実行し、要求した後、あなたはJSONをパースする必要が持っている単純なクエリ

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true 

です。 PHPのための

例:あなたは、実際のアドレス文字列に緯度長い値を変換する機能が必要であることを

const GOOGLE_MAPS_URL = 'http://maps.googleapis.com/maps/api/geocode/json'; 

function getUrlLatLng($latLng) { 
    $queryString = http_build_query(['latlng' => $latLng,'sensor'=> true]); 
    return GOOGLE_MAPS_URL . '?' . $queryString; 
} 

function getData($lat, $lng) { 
    $latLng = implode(',',[$lat, $lng]); 
    $url = getUrlLatLng($latLng); 
    return json_decode(file_get_contents($url)); 
} 

function parseAddress($result) { 

    return ($result[0]->formatted_address); //this parse address ass field you need; 
} 

function getAddress($lat, $lng) { 
    $address = (array)@getData($lat, $lng); 

    if(empty((array)@$address['results'])) { 
    throw new Exception('Not result'); 
    } 

    return $address['results']; 
} 

function getFullAddress($lat, $lng) { 
    $address = getAddress($lat, $lng); 
    return parseAddress($address); 
} 

$result = getFullAddress(-7.966620399999998, 112.000002); 

echo $result; 
関連する問題