Created
September 22, 2020 14:38
-
-
Save codercampos/0c290ab99c15890f717540526ab77073 to your computer and use it in GitHub Desktop.
Basic example how to use Google's Geocode API on Xamarin Forms (and C# in general I guess).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using Newtonsoft.Json; | |
namespace YourApp.Models | |
{ | |
/// <Summary> | |
/// Basic Google's geocode information model. | |
/// </Summary> | |
public class Geocode | |
{ | |
[JsonProperty("formatted_address")] | |
public string FormattedAddress { get; set; } | |
[JsonProperty("address_components")] | |
public AddressComponent[] AddressComponents { get; set; } | |
} | |
public class AddressComponent | |
{ | |
[JsonProperty("long_name")] | |
public string LongName { get; set; } | |
[JsonProperty("short_name")] | |
public string ShortName { get; set; } | |
[JsonProperty("types")] | |
public List<string> Types { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json.Linq; | |
using Refit; | |
namespace YourApp.Services | |
{ | |
public interface IGoogleService | |
{ | |
[Get("/geocode/json")] | |
Task<JToken> GetGeocodeInformation(string latlng, string key); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Don't forget to initialize IGoogleService Inject the service or initialize it | |
// More information: https://github.com/reactiveui/refit | |
async Task GetGeocode (Position? selectedLocation) { | |
if (selectedLocation != null) { | |
try | |
{ | |
var googleApi = RestService.For<IGoogleService>("https://maps.googleapis.com/maps/api"); | |
var token = await googleService.GetGeocodeInformation | |
($"{selectedLocation.Value.Latitude}, {selectedLocation.Value.Longitude}", | |
AppConstants.GoogleApiKey); | |
var results = token["results"].ToObject<List<Geocode>> (); | |
GetAddress (results); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine (e); | |
//Analytics.TrackEvent("Error", new Dictionary<string, string> | |
//{ | |
// {"Exception", e.Message}, | |
// {"From", "YourPage"} | |
//}); | |
} | |
} | |
void GetAddress(List<Geocode> geocodes) | |
{ | |
foreach (var geocode in geocodes) | |
{ | |
foreach (var addressComponent in geocode.AddressComponents) | |
{ | |
if (addressComponent.Types.Contains("locality") || addressComponent.Types.Contains("administrative_area_level_1")) | |
{ | |
State = addressComponent.LongName.Replace("Department", "").Trim().TrimEnd().TrimStart(); | |
} | |
else if (addressComponent.Types.Contains("country")) | |
{ | |
Country = addressComponent.LongName; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment