如何从城市名称中找到位置坐标,然后将这些坐标分配给变量?

I'm trying to figure out how I can find the location co-ordinates from a user-inputted city name. I know that Google makes this possible with their API, but I haven't been able to figure out how to actually take those results and assign them to php variables to either be stored in a database, or to be used for search functions.

Essentially, what I'm trying to do is implement a search system on my website that allows users to search for other users within a certain distance of a city.

If anyone could help out a noob programmer that would be awesome :)

Thanks!

Here is some code to get you started

PHP to find the lat/lng

    function buildGeocode($address_1, $address_2, $city, $state, $zip_code, $country){

        $base_url = "http://maps.googleapis.com/maps/api/geocode/json";

        $address = $address_1 . ' ' . $address_2 . ', ' . $city . ' ' . $state . ' ' . $zip_code . ' ' . $country;

        $request_url = $base_url . "?address=" . urlencode($address) . "&sensor=false";

        $data = json_decode(file_get_contents($request_url), true);

        return array('lat' => $data['results'][0]['geometry']['location']['lat'], 'lng' => $data['results'][0]['geometry']['location']['lng']);

    }

Here is some MYSQL to find items in a given distance as the bird flys in miles, this should be converted to PDO or escaped propperly

SELECT SQL_CALC_FOUND_ROWS
            *
            FROM (SELECT location_tbl.*, ( 3959*(2*ASIN(SQRT(POW(SIN(((location_tbl.lat-" . $lat . ")*(PI()/180))/2),2)+COS(location_tbl.lat*0.017453293)*COS(" . $lat . "*0.017453293)*POW(SIN(((location_tbl.lng-" . $lng . ")*(PI()/180))/2),2)))) ) AS distance
                FROM location_tbl
            WHERE location_tbl.active = 1 AND location_tbl.deleted = 0) AS tmp_tbl
        WHERE distance <= '" . $dis . "'
        ORDER BY distance ASC