Skip to content

Commit

Permalink
Merge pull request #70 from teknologi-umum/maps
Browse files Browse the repository at this point in the history
Improve /map
  • Loading branch information
ronnygunawan authored Dec 2, 2023
2 parents d97b760 + 4e79add commit cd96b25
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
5 changes: 3 additions & 2 deletions BotNet.Services/BotCommands/SearchPlace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public static async Task SearchPlaceAsync(ITelegramBotClient telegramBotClient,
SEARCH_PLACE_LIMITER.ValidateActionRate(message.Chat.Id, message.From!.Id);

try {
string coords = await serviceProvider.GetRequiredService<GeoCode>().SearchPlaceAsync(commandArgument);
(double lat, double lng) = await serviceProvider.GetRequiredService<GeoCode>().SearchPlaceAsync(commandArgument);
string staticMapUrl = serviceProvider.GetRequiredService<StaticMap>().SearchPlace(commandArgument);

await telegramBotClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputFileUrl(staticMapUrl),
caption: coords,
caption: $"<a href=\"https://www.google.com/maps/search/{lat},{lng}\">View in 🗺️ Google Maps</a>",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch {
Expand Down
13 changes: 6 additions & 7 deletions BotNet.Services/GoogleMap/GeoCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public GeoCode(IOptions<GoogleMapOptions> options) {
/// <param name="place">Place or address that you want to search</param>
/// <returns>strings of coordinates</returns>
/// <exception cref="HttpRequestException"></exception>
public async Task<string> SearchPlaceAsync(string? place) {
public async Task<(double Lat, double Lng)> SearchPlaceAsync(string? place) {
if (string.IsNullOrEmpty(place)) {
return "Invalid place";
throw new HttpRequestException("Invalid place");
}

if (string.IsNullOrEmpty(_apiKey)) {
return "Api key is needed";
throw new HttpRequestException("Api key is needed");
}

Uri uri = new(_uriTemplate + $"?address={place}&key={_apiKey}");
Expand Down Expand Up @@ -75,11 +75,10 @@ public async Task<string> SearchPlaceAsync(string? place) {

Result? result = body.Results[0];

string lat = result.Geometry!.Location!.Lat.ToString();
string longitude = result.Geometry!.Location!.Lng.ToString();
string coordinates = $"lat: {lat}, long: {longitude}";
double lat = result.Geometry!.Location!.Lat;
double lng = result.Geometry!.Location!.Lng;

return coordinates;
return (lat, lng);
}
}
}
2 changes: 1 addition & 1 deletion BotNet.Services/GoogleMap/StaticMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BotNet.Services.GoogleMap {
public class StaticMap {
private readonly string? _apiKey;
protected string mapPosition = "center";
protected int zoom = (int)ZoomLevel.Streets;
protected int zoom = 13;
protected string size = "600x300";
protected string marker = "color:red";
private string _uriTemplate = "https://maps.googleapis.com/maps/api/staticmap";
Expand Down

0 comments on commit cd96b25

Please sign in to comment.