How to get geolocation address by using google geolocation API in ASP.NET Web API

In this article we are going to learn how to get geolocation address by using google geolocation API in ASP.NET Web API.

Now we will go and create our application. I hope you will like this.
You can always download the source code here.

dicon Application Code

dicon Database’s Script

Background:

Normally we call google  geolocation api either from server side or from client side,right? Today we  use server side.it is fully dynamic. we will use two tools, One is Postman and Other one is  Fiddler. Both are used for test web api. This google geolocation api depends on latitude and longitude. Booth will be controlled our Client. They will give value.

Creating DataBase:

The following query can be used to create a database in your SQL Server.

The following query can be used to create a table in your SQL Server.

Creating web Application:

Start virtual stdio File>New>Project and click.New Project window will show on your monitor. Select Web and select  ASP.NET Web Application(.NET Framework) and give named and once click ok button again a new window will show on your monitor. Form the following pop up select Web API and once click ok button.

3

 

 

 

 

 

 

 

 

 

 

Figure: Web API Template with add folders and core references for  MVC and Web API.

A project as MVC folder structure with core references  will be created for you.

Untitled

 

 

 

 

 

 

 

 

Create Entity Data Model

Right click on your model folder and click new, select ADO.NET Entity Data Model. Follow the steps given. Once you have done the process , you can see the edmx file and other files in your model folder. Here I gave SalesTrackingEntities for our Entity data model name. Now you can see a file with edmx extension have been created.

Create ResponseModel Class:

we will create ResponseModel class in model folder. Actually this class will be  used for return type.Bellow is the following code

Create virtual class:

we will create virtual class in model folder bellow is following query.

Create API Controller:

Right click on Controllers folder add >controller select Web API 2 Controller-Empty and give name of controller,once click ok

We are going to demonstration our TaskController. First of all I want to say that in this project i do dot use any repository pattern or any multi layer.I have done all coding in TaskController.

We have just pointed _dbContext instant.

We have created  instant of SalesTarackingEntities in TaskController Constructor because access all table and use linq.

We use HttpPost for SaveTask method, we get vmTask object as parameter from out world. when we will test this method using fiddler of postman then we will see how to pass vmTask object form out world. we call SaveTaskDetails method for save, this method exist in TaskController

we can see above method we just save in database using linq and we have call GetAddress Method with two parameter,this method our main focus.bellow is following code.

In this method, we use google api with two parameters latitude and longitude for get address. we have used XElement class and Load static mehtod for geting API’s response.after getting location name we have returned location name.

Get task list by userID:

Our Code is ready. So we will check

Check by Postman:

For HttpPost,

12323

 

wow it’s work done. ok let’s go for HttpGet

5645645

It’s always work now.

Check by Fiddler:

For HttpPost,

fid1212

 

out result is bellow :

ferere3434

 

wow it’s work done. ok let’s go for HttpGet

fiddler4343

 

Hope this will be helpfull. Happy Coding 🙂

Leave a Reply

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