I am trying to display Google Maps inside a Partial View with JSON. I have already tried the code inside a normal view and it works perfectly fine.
I have the following:-
Partial View ShowMap.cshtml
@using Microsoft.Web.Helpers
<script src="~/Scripts/jquery-1.8.3.min.js"></script>
<div class="experienceRestrictedText">
@Maps.GetGoogleHtml("1, Redmond Way, Redmond, WA", width: "400", height: "400")
</div>
Index.cshtml (where the Partial view is launched)
$('.modal_link_map').on('click', function (e) {
$('.modal_part').show();
var id = $(this).attr('data-id');
var context = $('#tn_select').load('/Experience/ShowMap?id=' + id, function () {
initSelect(context);
});
e.preventDefault();
return false;
});
And the controller Action is as follows:-
public ActionResult ShowMap()
{
_ItemID = Convert.ToInt32(Request.QueryString["id"]);
viewModel.ExperienceViewModel.Experience = unitOfWork.ExperienceRepository.GetByID(_ItemID);
return PartialView(viewModel);
}
Do I need to include anything else for this map to work?
I am not familiar with the
@Maps.GetGoogleHtmlhelper but I am afraid that somehow this helper is including the following script:Except that this script cannot be loaded asynchronously because it uses
document.write()to load the actual API. To make this work you should specify acallbackparameter. Here’s how your partial could look like:Notice how a callback parameter is passed to the
http://maps.google.com/maps/api/jsscript and the actual initialization of the maps is done inside this callback.Also notice that I have removed the jquery script from the partial. I guess that jQuery is already loaded in your layout because you seem to be using it to attach to a
.on()handler.As an alternative you could have included the
<script src="//maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>in your main view but once you load the partial, the@Maps.GetGoogleHtmlhelper would have included it a second time.Personally I am not a big fan of those kind of helpers because they are completely obscuring the underlying calls leaving you without much control and understanding of what’s happening.