1
I have the following Actionresult Controller in my application. In this Actionresult I have two Viewbag that captures a Longitude and Latitude values from a Google API.
The values arrive perfectly in the Viewbag below so coming -21.1234567 and -51.1234567
ViewBag.Latitude = Double.Parse(a1, CultureInfo.InvariantCulture);
ViewBag.Longitude = Double.Parse(a2, CultureInfo.InvariantCulture);
Code below the 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 View I have the following script I picked up in the API section of Google Script variables are not taking values
var a1 = @(ViewBag.Latitude);
var a2 = @(ViewBag.Longitude);
Part of the View referring to 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=MINHA CHAVE&callback=initMap">
</script>
Amigo @Wesley Ferreira I was able to solve in this way var latitude = @Html.Raw(Json.Encode(@Viewbag.Latitude)) and so the variable picks the value with point, and thus passing to Google Maps the corresponding values correctly.
– Cyberlacs