Using Google Map API to get geological information of a place

U

The Google Maps Geocoding API is a service that provides geocoding and reverse geocoding of addresses.

Geocoding is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude), which you can use to place markers on a map, or position the map.

Reverse geocoding is the process of converting geographic coordinates into a human-readable address. The Google Maps Geocoding API’s reverse geocoding service also lets you find the address for a given place ID

You access the Google Maps Geocoding API through an HTTP interface.

The following example requests the latitude and longitude of “dhaka”, and specifies that the output must be in JSON format..

https://maps.googleapis.com/maps/api/geocode/json?address=dhaka

Below is a sample geocoding response, in JSON:

{
  "results": [
    {
      "address_components": [
        {
          "long_name": "Dhaka",
          "short_name": "Dhaka",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Dhaka District",
          "short_name": "Dhaka District",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "Dhaka Division",
          "short_name": "Dhaka Division",
          "types": [
            "administrative_area_level_1",
            "political"
          ]
        },
        {
          "long_name": "Bangladesh",
          "short_name": "BD",
          "types": [
            "country",
            "political"
          ]
        }
      ],
      "formatted_address": "Dhaka, Bangladesh",
      "geometry": {
        "bounds": {
          "northeast": {
            "lat": 23.9000025,
            "lng": 90.50910469999999
          },
          "southwest": {
            "lat": 23.6612704,
            "lng": 90.3295468
          }
        },
        "location": {
          "lat": 23.810332,
          "lng": 90.4125181
        },
        "location_type": "APPROXIMATE",
        "viewport": {
          "northeast": {
            "lat": 23.9000025,
            "lng": 90.50910469999999
          },
          "southwest": {
            "lat": 23.6613811,
            "lng": 90.329809
          }
        }
      },
      "place_id": "ChIJgWsCh7C4VTcRwgRZ3btjpY8",
      "types": [
        "locality",
        "political"
      ]
    }
  ],
  "status": "OK"
}

Now we implement it in php.
First we need a form. Create a file named “index.php”

<!DOCTYPE html>
<html>
<head>
    <title>Form</title>
</head>
<body>
    <form action="geodata.php" method="get">
    <input type="text" name="location">
    <input type="submit" name="" value="Submit">
</form>
</body>
</html>

Next we create a file that shows the json response.
That’s why create a file named “geodata.php”

<?php

if (!empty($_GET["location"])) {
    $maps_url = "https://maps.googleapis.com/maps/api/geocode/json?&address=".urlencode($_GET["location"]);

    $json_response = file_get_contents($maps_url);
    $json_array = json_decode($json_response, true);

    print_r($json_array);
    echo $lat = $json_array['results'][0]['geometry']['location']['lat'];
    echo $long_name= $json_array['results'][0]['address_components'][0]['long_name'];

}

  ?>

Enjoy!!

<!DOCTYPE html>
<html>
<head>
    <title>Form</title>
</head>
<body>
    <form action="geodata.php" method="get">
    <input type="text" name="location">
    <input type="submit" name="" value="Submit">
</form>
</body>
</html>

About the author

Imran Pollob

Learn => Practice => Implement => Repeat

Add comment

Categories

Imran Pollob

Learn => Practice => Implement => Repeat

Get in touch