2.9 KiB
2.9 KiB
Svrnty.GeoManagement.Google
Google Geocoding API provider implementation for the Svrnty.GeoManagement library.
Installation
Add reference to this project or install via NuGet (when published).
Configuration
Using appsettings.json
{
"GoogleGeoManagement": {
"ApiKey": "your-google-api-key-here",
"Language": "en",
"Region": "us"
}
}
Register in Dependency Injection
using Svrnty.GeoManagement.Google.Extensions;
// In your Program.cs or Startup.cs
builder.Services.AddGoogleGeoManagement(
builder.Configuration.GetSection("GoogleGeoManagement"));
Manual Configuration
builder.Services.AddGoogleGeoManagement(options =>
{
options.ApiKey = "your-google-api-key-here";
options.Language = "en";
options.Region = "us";
});
Usage
using Svrnty.GeoManagement.Abstractions.Abstractions;
using Svrnty.GeoManagement.Abstractions.Models;
public class MyService
{
private readonly IGeoManagementProvider _geoProvider;
public MyService(IGeoManagementProvider geoProvider)
{
_geoProvider = geoProvider;
}
public async Task Example()
{
// Forward geocoding - address to coordinates
var address = new Address(
Line1: "1600 Amphitheatre Parkway",
Line2: null,
City: "Mountain View",
Subdivision: "CA",
PostalCode: "94043",
Country: "US",
Location: null,
Note: null);
var geoPoint = await _geoProvider.GetGeoPointAsync(address);
Console.WriteLine($"Coordinates: {geoPoint?.Latitude}, {geoPoint?.Longitude}");
// Reverse geocoding - coordinates to address
var location = new GeoPoint(37.4224764, -122.0842499);
var foundAddress = await _geoProvider.ReverseGeocodeAsync(location);
Console.WriteLine($"Address: {foundAddress?.GetFormattedAddress()}");
// Normalize address
var normalized = await _geoProvider.NormalizeAddressAsync(address);
Console.WriteLine($"Normalized: {normalized?.GetFormattedAddress()}");
// Normalize from string
var normalizedFromString = await _geoProvider.NormalizeAddressAsync(
"1600 Amphitheatre Parkway, Mountain View, CA");
Console.WriteLine($"Normalized: {normalizedFromString?.GetFormattedAddress()}");
}
}
Configuration Options
- ApiKey (required): Your Google Maps API key
- Language (optional): Language code for results (e.g., "en", "fr", "de")
- Region (optional): Region code for biasing results (e.g., "us", "uk", "ca")
Requirements
- .NET 8.0
- Google Maps API key with Geocoding API enabled
- Internet connection for API calls
Error Handling
The provider returns null
for operations that fail (e.g., invalid address, API errors, network issues). All errors are logged using ILogger<GeoManagementGoogleProvider>
.