@page "/"
@using Newtonsoft.Json;
@using Microsoft.AspNetCore.Components.Forms
@using System.Text;
@using System.Net.Http.Headers;
<h1>Counter</h1>
<EditForm Model="@submitActivity" OnSubmit="@Submit">
<div class="row">
<div class="col-md-3">
<p>Location</p>
</div>
<div class="col-md-4">
<input placeholder="Location" @bind="@loc" />
</div>
</div>
<br />
<div>
<button type="submit">Submit</button>
</div>
<div>
@errmsg
</div>
<br />
@latitude
<br />
@longitude
<br/>
@elevation
</EditForm>
@code {
private string loc; string errmsg = ""; string time1 = ""; double latitude = 0.0;
double longitude = 0.0; double elevation;
string time2 = "";
private ACTIVITY submitActivity { get; set; } = new();
public class ACTIVITY
{ public string Dummy { get; set; }
}
private async Task Submit()
{
string key = "Au7sMtQzyQZRzuQ2krOIbZg8j2MGoHzzOJAmVym6vQjHq_BJ8a1YQGX3iCosFh8u";
string query = loc;
using (HttpClient client = new HttpClient())
{
string timez = "";
string final = "";
string timezone = "";
string checknull = "";
string time3 = "";
string timef = "";
Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));
client.BaseAddress = new Uri(geocodeRequest.ToString());
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response1 = null;
try
{
response1 = await client.GetAsync(geocodeRequest);
string jsonstring = await response1.Content.ReadAsStringAsync();
// Parse the JSON response to extract the address
dynamic data1 = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring);
var result1 = data1.resourceSets[0].resources[0];
latitude = result1.point.coordinates[0];
longitude = result1.point.coordinates[1];
}
catch (Exception e)
{
loc = "";
timez = "";
errmsg = "Invalid Location";
}
}
using (HttpClient client1 = new HttpClient())
{
Uri elevationReq = new Uri($"https://api.open-meteo.com/v1/elevation?latitude={latitude}&longitude={longitude}");
client1.BaseAddress = new Uri(elevationReq.ToString());
client1.DefaultRequestHeaders.Clear();
client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response2 = null;
response2 = await client1.GetAsync(elevationReq);
string jsonstring2 = await response2.Content.ReadAsStringAsync();
// Parse the JSON response to extract the address
dynamic datael = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring2);
elevation = datael.elevation[0];
}
}
}
Bing Webapi fails in Blazor Incorrect String Format Exception
Anonymous
The below code works in Web but not in Blazor please suggest. I could see errors in console.
Here is correct website that works
https://locationsearch.azurewebsites.net/Calc
Note :please get free Bing Maps key from MS Website
@page "/"
@using Newtonsoft.Json;
@using Microsoft.AspNetCore.Components.Forms
@using System.Text;
@using System.Net.Http.Headers;
<h1>Counter</h1>
<EditForm Model="@submitActivity" OnSubmit="@Submit">
<div class="row">
<div class="col-md-3">
<p>Location</p>
</div>
<div class="col-md-4">
<input placeholder="Location" @bind="@loc" />
</div>
</div>
<br />
<div>
<button type="submit">Submit</button>
</div>
<div>
@errmsg
</div>
<br />
@latitude
<br />
@longitude
<br/>
@elevation
</EditForm>
@code {
private string loc; string errmsg = ""; string time1 = ""; double latitude = 0.0;
double longitude = 0.0; double elevation;
string time2 = "";
private ACTIVITY submitActivity { get; set; } = new();
public class ACTIVITY
{ public string Dummy { get; set; }
}
private async Task Submit()
{
string key = "My Bing Key";
string query = loc;
using (HttpClient client = new HttpClient())
{
string timez = "";
string final = "";
string timezone = "";
string checknull = "";
string time3 = "";
string timef = "";
Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));
client.BaseAddress = new Uri(geocodeRequest.ToString());
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response1 = null;
try
{
response1 = await client.GetAsync(geocodeRequest);
string jsonstring = await response1.Content.ReadAsStringAsync();
// Parse the JSON response to extract the address
dynamic data1 = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring);
var result1 = data1.resourceSets[0].resources[0];
latitude = result1.point.coordinates[0];
longitude = result1.point.coordinates[1];
Uri elevationReq = new Uri(string.Format("https://api.open-meteo.com/v1/elevation?latitude={latitude}&longitude={longitude}"));
client.BaseAddress = new Uri(elevationReq.ToString());
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response2 = null;
response2 = await client.GetAsync(elevationReq);
string jsonstring2 = await response2.Content.ReadAsStringAsync();
// Parse the JSON response to extract the address
dynamic datael = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring2);
elevation = datael.elevation[0];
}
catch (Exception e)
{
loc = "";
timez = "";
errmsg = "Invalid Location";
}
}
}
}
1 answer
Sort by: Most helpful
-
Anonymous
2024-04-08T15:45:01.6366667+00:00