Showing posts with label Google API. Show all posts
Showing posts with label Google API. Show all posts

Thursday, March 1, 2018

Calculate distance between two Geo location using Google Map API

Introduction

In order to run this proof of concept , we need google api  key,API_Key This can be obtain as per your personal google account credential. create project in developer console in google and generate api_key for you to create proof of concept to consume google api.

Try Out Proof of concepts: Hosted in JSFiddler

Google Developer Console Login


Create Project in Google API Console

Google Distance Matrix API

Google Autocomplete Location API

Production Implication

Create account for BUPA or contact IS support for the same.
FREE QUOTA

Key Implementation things to note

  • Use new google.maps.DistanceMatrixService(); for recommended route.
  • The below example can be useful to find distance for multiple source and destination location
  • You can limit auto complete or restrict to specific countries.   destinationautocomplete.setComponentRestrictions({
  •     'country': ['aus']
  • Use new google.maps.DirectionsService() to calculate alternative routes with legs and steps directions
  • Property setting optimizeWaypoints: true gives accurate distance remove complexity such as turns and other criteria
  • Property setting provideRouteAlternatives: true, helps you find all route path options
  • It used traveling salesman algorithm for optimal map routing with google maps.->optimizeWaypoints

Code Base Implementation

Wednesday, February 10, 2016

Consume Google My Business API Using Service Account.

If it comes to google my business API setup one has to go through developer console request api access , create project and enabled OAuth2.0.

Very important things to note: This solution is applicable only if you're looking at creating a console or batch job that is going to run in server or in azure cloud.

What it tackles?

  1. There is no clear cut direction or documentation as such in terms of service account.
  2. There is no nuget pkg available at the moment.
  3. Sometimes you may face problem with Key.p12 file that is mentioned in documentation.
  4. If key.p12 path results into error look at Byte[] options to load it.

Here is the few tweak in terms of google my business c# implementation. Feel free to comment.

using System;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using System.IO;
using Google.Apis.Mybusiness.v2;
using Google.Apis.Mybusiness.v2.Data;
namespace ConsoleApplication1
{
    /// 
    /// This sample demonstrates the simplest use case for a Service Account service.
    /// The certificate needs to be downloaded from the Google Developers Console
    /// 
    ///   "Create another client ID..." -> "Service Account" -> Download the certificate,
    ///   rename it as "key.p12" and add it to the project. Don't forget to change the Build action
    ///   to "Content" and the Copy to Output Directory to "Copy if newer".
    /// 
    public class Program
    {

        public static void Main(string[] args)
        {
            Console.WriteLine("Google My Business API - Service Account");
            Console.WriteLine("==========================");
            //byte[] scriot=ReadFile("key.p12");
            String serviceAccountEmail = "someemailIDthatiscreatedbydefault@xyz.iam.gserviceaccount.com";

            var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
            //var certificate = new X509Certificate2(scriot);
            string[] scopes = new string[] { "https://www.googleapis.com/auth/plus.business.manage" };

            ServiceAccountCredential credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(serviceAccountEmail) {

               Scopes =scopes,
               User = "googleplacesABC@abc.com.au"
               }.FromCertificate(certificate));

          
            MybusinessService service = new MybusinessService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "ABC-GMB",
            });

            string loc = "accounts/1900345345435435345";
            AccountsResource.LocationsResource.ListRequest locRequest = service.Accounts.Locations.List(loc);
            
            ListLocationsResponse locationsResponse =locRequest.Execute();
      
            Console.ReadLine();
        }
        internal static byte[] ReadFile(string fileName)
        {
            FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            int size = (int)f.Length;
            byte[] data = new byte[size];
            size = f.Read(data, 0, size);
            f.Close();
            return data;
        }
    }
}