Can someone please explain my possible options when trying to get razor data inside javascript? I am trying to add markers to google map.
This seems to work:
@model IEnumerable<ServicesPortal.Models.MapsPartialModel>
@{
ViewBag.Title = "Home Page";
}
@if (Model != null)
{
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(46.119944, 14.815333),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
addmarker(46.119944, 14.815333);
// and so on...
function addMarker(x, y) {
var location = new google.maps.LatLng(x, y);
var marker = new google.maps.Marker({
position: location,
map: map,
});
}
} google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map" style="width:auto; height:500px"></div>
}
While this does not:
@model IEnumerable<ServicesPortal.Models.MapsPartialModel>
@{
ViewBag.Title = "Home Page";
}
@if (Model != null)
{
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(46.119944, 14.815333),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
@foreach (var item in Model)
{
<text>
addMarker(@item.Profile.lat, @item.Profile.lng);
</text>
}
function addMarker(x, y) {
var location = new google.maps.LatLng(x, y);
var marker = new google.maps.Marker({
position: location,
map: map,
});
}
} google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map" style="width:auto; height:500px"></div>
}
EDIT: This is also not working...
@model IEnumerable<ServicesPortal.Models.MapsPartialModel>
@{
ViewBag.Title = "Home Page";
}
@if (Model != null)
{
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(46.119944, 14.815333),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
@foreach (var item in Model)
{
<text>
addMarker(@Html.Raw(item.Profile.lat), @Html.Raw(item.Profile.lng));
</text>
}
function addMarker(x, y) {
var location = new google.maps.LatLng(x, y);
var marker = new google.maps.Marker({
position: location,
map: map,
});
}
} google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map" style="width:auto; height:500px"></div>
}
EDIT: Tried also with some variables ... no effect.
@model IEnumerable<ServicesPortal.Models.MapsPartialModel>
@{
ViewBag.Title = "Home Page";
}
@if (Model != null)
{
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(46.119944, 14.815333),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
// test
var lat = 46.119944;
var lng = 14,815333;
@foreach (var item in Model)
{
<text>
addMarker(@lat, @lng);
</text>
}
function addMarker(x, y) {
var location = new google.maps.LatLng(x, y);
var marker = new google.maps.Marker({
position: location,
map: map,
});
}
} google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map" style="width:auto; height:500px"></div>
}
I was trying to use this solution, but I am missing something... Using Razor within JavaScript