我是 NerdDinner 教程。我想上传一张地图来显示每顿晚餐的地理信息。我定义了一个名为“Map”的 JS 文件。和一个“地图”局部视图。然后我在我的 DinnerForm 视图中呈现“地图”部分视图。但是当我运行项目时,地图没有显示出来。并向我展示了以下错误:
Unhandled exception at line 149, column 9 in http://localhost:52372/Dinner/Edit/14
0x800a1391 - Microsoft JScript runtime error: 'Center' is undefined
相关代码以及我的问题如下所示。
地图js:
var map = null;
var points = [];
var shapes = [];
var center = null;
function LoadMap(latitude, longitude, onMapLoaded) {
map = new VEMap('theMap');
options = new VEMapOptions();
options.EnableBirdseye = false;
// Makes the control bar less obtrusize.
map.SetDashboardSize(VEDashboardSize.Small);
if (onMapLoaded != null)
map.onLoadMap = onMapLoaded;
if (latitude != null && longitude != null) {
center = new VELatLong(latitude, longitude);
}
map.LoadMap(center, null, null, null, null, null, null, options);
}
function LoadPin(LL, name, description) {
var shape = new VEShape(VEShapeType.Pushpin, LL);
//Make a nice Pushpin shape with a title and description
shape.SetTitle("<span class=\"pinTitle\"> " + escape(name) + "</span>");
if (description !== undefined) {
shape.SetDescription("<p class=\"pinDetails\">" +
escape(description) + "</p>");
}
map.AddShape(shape);
points.push(LL);
shapes.push(shape);
}
function FindAddressOnMap(where) {
var numberOfResults = 20;
var setBestMapView = true;
var showResults = true;
map.Find("", where, null, null, null,
numberOfResults, showResults, true, true,
setBestMapView, callbackForLocation);
}
function callbackForLocation(layer, resultsArray, places,
hasMore, VEErrorMessage) {
clearMap();
if (places == null)
return;
//Make a pushpin for each place we find
$.each(places, function (i, item) {
description = "";
if (item.Description !== undefined) {
description = item.Description;
}
var LL = new VELatLong(item.LatLong.Latitude,
item.LatLong.Longitude);
LoadPin(LL, item.Name, description);
});
//Make sure all pushpins are visible
if (points.length > 1) {
map.SetMapView(points);
}
//If we've found exactly one place, that's our address.
if (points.length === 1) {
$("#Latitude").val(points[0].Latitude);
$("#Longitude").val(points[0].Longitude);
}
}
function clearMap() {
map.Clear();
points = [];
shapes = [];
}
地图局部视图:
@model NerdDinner.Models.Dinner
<script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2" type="text/javascript"></script>
<script src="/Scripts/Map.js" type="text/javascript"></script>
<div id="theMap">
</div>
<script type="text/javascript">
$(document).ready(function () {
var latitude = @Model.Latitude;
var longitude = @Model.Longitude;
if ((latitude == 0) || (longitude == 0))
LoadMap();
else
LoadMap(latitude, longitude, mapLoad);
});
function mapLoad()
{
var title = "@Html.Encode(Model.Title)"
var address = "@Html.Encode(Model.Address)"
LoadPin(Center,title,address);
map.setZoomLevel(14);
}
</script>
晚餐表格视图:
@model NerdDinner.Models.DinnerFormViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Dinner</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dinner.Title)
@Html.ValidationMessageFor(model => model.Dinner.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.Latitude)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dinner.Latitude)
@Html.ValidationMessageFor(model => model.Dinner.Latitude)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.Longitude)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dinner.Longitude)
@Html.ValidationMessageFor(model => model.Dinner.Longitude)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.EventDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dinner.EventDate)
@Html.ValidationMessageFor(model => model.Dinner.EventDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.ContactPhone)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dinner.ContactPhone)
@Html.ValidationMessageFor(model => model.Dinner.ContactPhone)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dinner.Address)
@Html.ValidationMessageFor(model => model.Dinner.Address)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.Country)
</div>
<div class="editor-field">
@Html.DropDownList("Country", @Model.Countries)
@Html.ValidationMessageFor(model => model.Countries)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.HostedBy)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dinner.HostedBy)
@Html.ValidationMessageFor(model => model.Dinner.HostedBy)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Dinner.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dinner.Description)
@Html.ValidationMessageFor(model => model.Dinner.Description)
</div>
<p>
<input type="submit" value="Save" />
</p>
<div id="mapDiv">
@{Html.RenderPartial("Map", Model.Dinner);}
</div>
</fieldset>
<script type="text/javascript">
$(document).ready(function () {
$("#Address").blur(function (evt) {
$("#Latitude").val("");
$("#Longitude").val("");
var address = jQuery.trim($("#Address").val());
if (address.length < 1)
return;
FindAddressOnMap(address);
});
});
</script>
}