Integrating Google Maps & Web Services in ASP.NET Core Web API

Author: Dasan | Date: April 11, 2025

1. ๐ŸŽฏ Introduction

In this post, weโ€™ll cover how to use Google Maps and a suite of supporting APIs in an ASP.NET Core Web API backend. We'll include:

2. ๐Ÿ”‘ Get Your Google Maps API Key

  1. Visit Google Cloud Console.
  2. Create a new project and enable the APIs listed above.
  3. Generate your API key from the "Credentials" section.
  4. Apply restrictions (IP or domain-based) for security.

3. ๐Ÿงฐ Setup Your ASP.NET Core Web API

Make sure HttpClient is configured:

// Program.cs
builder.Services.AddHttpClient();

4. ๐Ÿงช API Integrations

4.1 ๐Ÿ“ Places API

// GET /api/google/places?query=restaurants in Chennai
[HttpGet("places")]
public async Task<IActionResult> GetPlaces([FromQuery] string query)
{
    var url = $"https://maps.googleapis.com/maps/api/place/textsearch/json?query={Uri.EscapeDataString(query)}&key={_apiKey}";
    var response = await _httpClient.GetStringAsync(url);
    return Content(response, "application/json");
}

4.2 ๐Ÿ›ฃ๏ธ Directions API (Legacy)

Note: Transit directions are excluded.

// GET /api/google/directions?origin=Chennai&destination=Bangalore
[HttpGet("directions")]
public async Task<IActionResult> GetDirections(string origin, string destination)
{
    var url = $"https://maps.googleapis.com/maps/api/directions/json?origin={origin}&destination={destination}&mode=driving&key={_apiKey}";
    var response = await _httpClient.GetStringAsync(url);
    return Content(response, "application/json");
}

4.3 ๐Ÿš— Distance Matrix API (Legacy)

// GET /api/google/distance?origins=Chennai&destinations=Bangalore
[HttpGet("distance")]
public async Task<IActionResult> GetDistanceMatrix(string origins, string destinations)
{
    var url = $"https://maps.googleapis.com/maps/api/distancematrix/json?origins={origins}&destinations={destinations}&mode=driving&key={_apiKey}";
    var response = await _httpClient.GetStringAsync(url);
    return Content(response, "application/json");
}

4.4 โ›ฐ๏ธ Elevation API

// GET /api/google/elevation?locations=27.9881,86.9250
[HttpGet("elevation")]
public async Task<IActionResult> GetElevation(string locations)
{
    var url = $"https://maps.googleapis.com/maps/api/elevation/json?locations={locations}&key={_apiKey}";
    var response = await _httpClient.GetStringAsync(url);
    return Content(response, "application/json");
}

4.5 ๐Ÿงญ Geocoding API

// GET /api/google/geocode?address=New York
[HttpGet("geocode")]
public async Task<IActionResult> Geocode(string address)
{
    var url = $"https://maps.googleapis.com/maps/api/geocode/json?address={Uri.EscapeDataString(address)}&key={_apiKey}";
    var response = await _httpClient.GetStringAsync(url);
    return Content(response, "application/json");
}

4.6 ๐Ÿ›ค๏ธ Roads API (Snap to Roads)

// GET /api/google/roads?path=60.170880,24.942795|60.170879,24.942796
[HttpGet("roads")]
public async Task<IActionResult> SnapToRoads(string path)
{
    var url = $"https://roads.googleapis.com/v1/snapToRoads?path={path}&key={_apiKey}";
    var response = await _httpClient.GetStringAsync(url);
    return Content(response, "application/json");
}

5. ๐Ÿ” Secure Your API Key

6. ๐Ÿ Conclusion

Youโ€™ve successfully integrated a full suite of Google Maps Web APIs into your ASP.NET Core Web API. These endpoints now serve as your backend support for frontend apps using Maps, Places, Geocoding, and more.

In the next blog, weโ€™ll explore how to connect these APIs with a React Native frontend for a real-time mobile experience.