我正在使用ASP.Net MVC,我们正在使用一个外部.JS文件放置所有JavaScript代码。我首先将下面的Google Map代码放置在View中,并且在视图页面中显示该地图时效果很好...但是当我发送代码进行代码审查时,代码审查人员要求我在.JS中引用它。文件。我尝试了很多次将其放入.JS文件中,但无法在视图中显示它。
好像他们想要js文件airplanDetail.js中的google map初始化变量和google map js脚本,并且仅在视图页面中具有引用。他们想要干净的代码/查看页面
您能帮忙吗?
<div class="googleMapAPI">
@{
var googleAPIKey = ConfigurationSettings.AppSettings["CWU_GoogleAPI_Key"];
<script src="https://maps.googleapis.com/maps/api/js?key=@googleAPIKey&callback=initMap" type="text/javascript"></script>
}
因此,在ASP.Net MVC视图页面的底部,有一个下面的代码引用了JS文件。
@section Scripts {
@Scripts.Render("~/Scripts/Views/Product/airplanDetail.js")
}
并且在[[airplanDetails.js文件中,有一个类似于下面的代码
$(function () {
Initialize();
});
function Initialize() {
google.maps.visualRefresh = true;
var propertyLatitude = document.getElementById("latitudId").value;
var propertyLongitude = document.getElementById("longitudId").value;
var propertyLocation = new google.maps.LatLng(propertyLatitude, propertyLongitude);
var mapOptions = {
zoom: 14,
center: propertyLocation,
mapTypeId: google.maps.MapTypeId.G_NORMAL_MAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var myLatlng = new google.maps.LatLng(propertyLatitude, propertyLongitude);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Location found'
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')
}
<html>
<head>
@Scripts.Render("~/Scripts/Views/Product/airplanDetail.js")
</head>
<body>
</body>
</html>