Google Maps Geocoding & Reverse Geocoding API

Google provides public API which can be used for geocoding and reverse-geocoding of addresses. This API is also available as part of the client-side Google Maps JavaScript API, or for server-side use with the Java Client, Python Client, Go Client and Node.js Client for Google Maps Services. 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.

One of my clients from Canada contact me for reverse geocoding for points of interest. The following was the requirements

  1. Develop a script which does reverse geocoding and format the address in various columns
  2. Get phone number for points of interest
  3. Get latitude and magnitude for nearest police station and hospital for these points of interest
  4. Get phone numbers for nearest police station and hospital

Solution

I used google sheet, google place and google geocoding APIs.

Step 1: Open a fresh google sheet from your google drive

Step 2: Enable google API from script editor

Step 3: Generate google API key so APIs can be used in google sheet

 

Code for main function is below. The purpose of main function is to iterate through a list of points of interest.

function getPlaceDetails() {
  
  for(var i=2;i<124;i++){
  
  var  lat =   SpreadsheetApp.getActiveSheet().getRange("H"+i).getValue();  
  var  lng = SpreadsheetApp.getActiveSheet().getRange("I"+i).getValue();
  var request = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat +","+ lng +"&key=YOUR_API_KEY";  
  var response = UrlFetchApp.fetch(request);  
  var json = JSON.parse(response);
  
  var jsonResult = json.results[0];  
    if(jsonResult!=undefined){
  var placeID= jsonResult.place_id; 
  var aDetails = jsonResult.address_components;  
  var address = getComps(aDetails);
  
  
  var request = "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + placeID +"&key=YOUR_API_KEY";
  
  var response = UrlFetchApp.fetch(request);  
  var json = JSON.parse(response);
  var jsonResult = json.result;
  address[5] = json.result.formatted_phone_number;
  var name= json.result.name;
  
  address[0] = name;
  
  address[6] = name;
  
  SpreadsheetApp.getActiveSheet().getRange("A" + i +":G" + i).setValues( [address]);
  
   gethospital(lat,lng,i);
  getPolice(lat,lng,i);
    
    Utilities.sleep(500);
    
  }
  }
}
 Use the following code for nearest police station
function gethospital(lat,lng,row){
  

  
  var request = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +lat.toString() +", " +lng.toString()  + "&radius=500000&types=hospital&key=YOUR_API_KEY";
  
  var response = UrlFetchApp.fetch(request);
  
  var jsonResult = JSON.parse(response);
  if(jsonResult.results[0]!=undefined){
  var name= jsonResult.results[0].name;
  var placeID= jsonResult.results[0].place_id;
  var pDetails= JSON.parse( UrlFetchApp.fetch("https://maps.googleapis.com/maps/api/place/details/json?placeid=" + placeID +"&key=YOUR_API_KEY")).result;

  
  
  var type= pDetails.address_components[1].long_name.split(" ");
  
    SpreadsheetApp.getActiveSheet().getRange("J" +row).setValue(type[type.length-1]);
  SpreadsheetApp.getActiveSheet().getRange("K"+row).setValue(pDetails.geometry.location.lat + ","+pDetails.geometry.location.lng);
  SpreadsheetApp.getActiveSheet().getRange("L"+row).setValue( pDetails.formatted_phone_number);
  }
  
}

Use the following code for nearest hospital station



function gethospital(lat,lng,row){
  

  
  var request = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +lat.toString() +", " +lng.toString()  + "&radius=500000&types=hospital&key=YOUR_API_KEY";
  
  var response = UrlFetchApp.fetch(request);
  
  var jsonResult = JSON.parse(response);
  if(jsonResult.results[0]!=undefined){
  var name= jsonResult.results[0].name;
  var placeID= jsonResult.results[0].place_id;
  var pDetails= JSON.parse( UrlFetchApp.fetch("https://maps.googleapis.com/maps/api/place/details/json?placeid=" + placeID +"&key=YOUR_API_KEY")).result;

  
  
  var type= pDetails.address_components[1].long_name.split(" ");
  
    SpreadsheetApp.getActiveSheet().getRange("J" +row).setValue(type[type.length-1]);
  SpreadsheetApp.getActiveSheet().getRange("K"+row).setValue(pDetails.geometry.location.lat + ","+pDetails.geometry.location.lng);
  SpreadsheetApp.getActiveSheet().getRange("L"+row).setValue( pDetails.formatted_phone_number);
  }
  
}

If you need any help, please contact me on Fiverr

Leave a Reply

Your email address will not be published. Required fields are marked *