Google Geolocation API - Use longitude and latitude to get address in textbox?

后端 未结 8 624
有刺的猬
有刺的猬 2021-01-31 05:25

I have noticed a lot of information about how to get your location using Google geolocation looks, based on IP address. But I am wondering if and how I could use this service t

8条回答
  •  不知归路
    2021-01-31 05:32

    What you are trying to do is reverse geocoding (See Daniel's answer).

    An example implementation in PHP:

    /**
    * reverse geocoding via google maps api
    * convert lat/lon into a name
    */
    function reverse_geocode($lat, $lon) {
        $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
        $data = json_decode(file_get_contents($url));
        if (!isset($data->results[0]->formatted_address)){
            return "unknown Place";
        }
        return $data->results[0]->formatted_address;
    }
    

提交回复
热议问题