I have the following ActionResult Controller in my application. In this ActionResult I have two ViewBags that capture a deserialization of Longitude and Latitude values originating from a Google API.
The values arrive perfectly in the ViewBag below coming like this -21.1234567 and -51.1234567
ViewBag.Latitude = Double.Parse(a1, CultureInfo.InvariantCulture);
ViewBag.Longitude = Double.Parse(a2, CultureInfo.InvariantCulture);
Code below ActionResult
public ActionResult Detalhes(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Empresa empresa = db.Empresa.Find(id);
if (empresa == null)
{
return HttpNotFound();
}
WebClient client = new WebClient();
string url = "https://maps.googleapis.com/maps/api/geocode/json?address=[Numero]+[Endereco],+[Bairro],+[Estado]&key=MINHA CHAVE";
url = url.Replace("[Numero]", empresa.Numero);
url = url.Replace("[Endereco]", empresa.Endereco.Replace(" ", "+"));
url = url.Replace("[Bairro]", empresa.Bairro.Replace(" ", "+"));
url = url.Replace("[Estado]", empresa.Estado);
string value = client.DownloadString(url);
dynamic dyn = JsonConvert.DeserializeObject(value);
string a1 = dyn["results"][0].geometry.location.lat;
string a2 = dyn["results"][0].geometry.location.lng;
/* QUERO PASSAR OS VALORES ABAIXO PARA A VIEW */
ViewBag.Latitude = Double.Parse(a1, CultureInfo.InvariantCulture);
ViewBag.Longitude = Double.Parse(a2, CultureInfo.InvariantCulture);
return View(empresa);
}
Below in the View I have the following script that I got in the API section of Google Script variables are not getting values
var a1 = @(ViewBag.Latitude);
var a2 = @(ViewBag.Longitude);
Part of the View for Google Maps
<h3>Localização</h3>
<div id="map"></div>
<script>
function initMap() {
var a1 = @(ViewBag.Latitude);
var a2 = @(ViewBag.Longitude);
var uluru = { lat: a1, lng: a2 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: uluru,
gestureHandling: 'cooperative'
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MINHACHAVE&callback=initMap">
</script>