Find location details by using address on google api in PHP

Votes : 5
815

If we have few details of address, How can we get location of a place ( latitude or longitude ) or make full place address?

The most easy way to get location details by using google api and also it is free service. fast respond. Only need google api key.

How to get google api key

  1. If you have google account. Every first time login to it or not register to google account, then login to it.
  2. Click here to get google api key.
  3. google api key
    Then click GET A KEY button.
  4. Get api project name. Agree with google term and condition. Then CREATE AND ENABLE API button. google api key
  5. You can get google api key now.
    google api key
    Then click DONE button.
  6. If you want to get new one or previous one click again GET A KEY button.
  7. You can create new project or select existing project. finally click Enable API button to activate api key into selected project and also show project api key.
    google api key
    google api key
function ESGeo_address($address, $api_key) {
    /*
     * Find location details by using address in google api
     * @param (string|required) $address - Address, Do you want to find location details.
     * @param (string|required) $api_key - Google api key.
     * 
     * @return (array) Return location details, If something wrong return false.
     * 
     * source : http://codrate.com/articles/find-location-details-by-using-address-by-using-google-api-in-php
     */
    $address = preg_replace("/[^A-Za-z0-9]/", " ", $address);
    $address = explode(' ', strtolower($address));
    $address = array_flip($address);
    unset($address['']);
    $address = implode('+', array_keys($address));

    $address = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=' . $address . '&key=' . $api_key);

    $address = json_decode($address, TRUE);
    if (!empty($address['results'])) {
        return $address['results'][0];
    } else {
        return false;
    }
}

Example

print_r(get_google_address('esila panadura sri lanka', 'google_api_key'));

Array
(
    [address_components] => Array
        (
            [0] => Array
                (
                    [long_name] => Panadura
                    [short_name] => Panadura
                    [types] => Array
                        (
                            [0] => locality
                            [1] => political
                        )

                )

            [1] => Array
                (
                    [long_name] => Kalutara
                    [short_name] => Kalutara
                    [types] => Array
                        (
                            [0] => administrative_area_level_2
                            [1] => political
                        )

                )

            [2] => Array
                (
                    [long_name] => Western Province
                    [short_name] => WP
                    [types] => Array
                        (
                            [0] => administrative_area_level_1
                            [1] => political
                        )

                )

            [3] => Array
                (
                    [long_name] => Sri Lanka
                    [short_name] => LK
                    [types] => Array
                        (
                            [0] => country
                            [1] => political
                        )

                )

        )

    [formatted_address] => Panadura, Sri Lanka
    [geometry] => Array
        (
            [location] => Array
                (
                    [lat] => 6.7242048
                    [lng] => 79.9291742
                )

            [location_type] => GEOMETRIC_CENTER
            [viewport] => Array
                (
                    [northeast] => Array
                        (
                            [lat] => 6.7255537802915
                            [lng] => 79.930523180291
                        )

                    [southwest] => Array
                        (
                            [lat] => 6.7228558197085
                            [lng] => 79.927825219708
                        )

                )

        )

    [place_id] => ChIJq1NvFa5I4joR-SrKP5U0vNo
    [types] => Array
        (
            [0] => establishment
            [1] => point_of_interest
            [2] => storage
        )

)
asked 4 months,13 days ago

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila