简体   繁体   English

Google Map API-未定义“ google”-MVC5 javascript

[英]Google Map API - 'google' not defined - MVC5 javascript

I get JavaScript "google is undefined" error. 我收到JavaScript“未定义Google”错误。

I apologize if this question is similar to this but I am using it in a different setting, so this may be an MVC issue. 如果这个问题与相似,我深表歉意,但是我在其他环境中使用它,因此这可能是MVC问题。

I use MCV5 website standard template and I put in the head of _layout.chtml main template: 我使用MCV5网站标准模板,并放在_layout.chtml主模板的头部:

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

This code goes into one of the views, for the account/index action: 对于帐户/索引操作,此代码进入视图之一:

<div id="map_canvas"></div>
<span id="result"></span>
<script>
    var map = null;
    var markersArray = [];

    function initialize() {

        var latlng = new google.maps.LatLng(13.73868, 1.07143);

        var settings = {
            zoom: 14,
            center: latlng,
            mapTypeControl: true,
            scaleControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
            },
            navigationControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.DEFAULT
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            backgroundColor: 'white'
        };

        map = new google.maps.Map(document.getElementById('map_canvas'), settings);
        google.maps.event.addListener(map, 'click', function (event) {
            document.getElementById('result').innerHTML = 'Lat: ' + event.latLng.lat() + ' Lng: ' + event.latLng.lng();
        });
    }

    window.onload = initialize;

</script>

Somehow the linked google js file does not seem to load by the time function initialize() runs and I get JavaScript "google is undefined" error in the first line of initialize() function. 以某种方式,链接的Google js文件似乎在函数initialize()运行时未加载,并且在initialize()函数的第一行出现JavaScript“ google is undefined”错误。

Thanks for your help. 谢谢你的帮助。

The only real point where MVC could be an issue is in the loading of your layout. MVC可能成为问题的唯一真实点是布局的加载。 Have you inspected the page source to ensure that the Google Maps API script is actually on the page? 您是否检查过页面源以确保页面上确实存在Google Maps API脚本? If it's not, then you just need to figure out why your view isn't utilizing the layout you think it should be. 如果不是,那么您只需要弄清楚为什么视图未使用您认为应该使用的布局即可。

If it is on page, then open the link to the JS file and ensure that it loads properly. 如果页面上,然后打开链接到JS文件,并确保其正确加载。 Especially if you on a corporate network, sometimes firewalls or other security software may prevent certain scripts from loading properly. 特别是在公司网络中,有时防火墙或其他安全软件可能会阻止某些脚本正确加载。 You'll need to work with your Infrastructure department if that's the case. 在这种情况下,您需要与基础架构部门合作。

Also, watch your usage of SSL. 另外,请注意您对SSL的使用。 If you're using SSL for the site you're loading this on, then this script won't load because it's coming over HTTP, not HTTPS. 如果您正在使用SSL加载网站,则该脚本将不会加载,因为它是通过HTTP而不是HTTPS发送的。 In general, it's always better to use the HTTPS version of external resources, as that should work whether your site is running on HTTPS or HTTP. 通常,最好使用外部资源的HTTPS版本,因为无论您的站点是在HTTPS还是HTTP上运行都可以。 Also, you can just make the URL protocol relative: 另外,您可以使URL协议相对:

<script src="//maps.google.com/maps/api/js?sensor=false"></script>

Assuming it's not one of those annoying instances where the third-party uses an entirely different domain for SSL, it will then request either HTTP or HTTPS depending on what your site is using. 假设它不是第三方使用完全不同的SSL域名的烦人实例之一,然后它将根据您的站点使用HTTP或HTTPS请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM