【问题标题】:How to reference Google Map tag in a JS file如何在 JS 文件中引用 Google Map 标签
【发布时间】:2019-11-11 14:51:04
【问题描述】:

我正在研究 ASP.Net MVC,我们正在使用外部 .JS 文件来放置所有 javascript 代码。 我首先将下面的谷歌地图代码放在视图中,它很好地在视图页面中显示地图......但是当我发送我的代码进行代码审查时,代码审查人员要求我在 .JS 中引用它文件。我尝试了很多次将其放入 .JS 文件中,但无法将其显示在视图中。

似乎他们想要 js 文件 airplanDetail.js 中的谷歌地图初始化变量和谷歌地图 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')

}

【问题讨论】:

  • 为什么没人愿意回复? 17 次观看,但 0 人想提供帮助。请先生/女士,我真的需要帮助。我被困了一整天。

标签: c# jquery asp.net asp.net-mvc


【解决方案1】:

尝试将脚本标签放在section中,即:

<html>
<head>
@Scripts.Render("~/Scripts/Views/Product/airplanDetail.js")
</head>
<body>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 2017-03-22
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    相关资源
    最近更新 更多