如何在HTML文档中从外部JS调用/启动此函数

时间:2016-03-01 15:01:29

标签: javascript jquery function google-maps

我有map.js这是一个设置Google Map with Icons的功能。我想知道我现在如何将这张地图放在我的网站上。我知道我需要调用一个函数并以ID标记以某种方式启动它。但我不完全确定这个过程是如何运作的。

外部JS map.js代码:

//in case the db connection is timed out (I hope this helps...)
if ($this->em->getConnection()->ping() === false) {
    $this->em->getConnection()->close();
    $this->em->getConnection()->connect();
}

当前HTML:

function locationsMap() {
/* GEO BROCATION MAP */
// Define your locations: HTML content for the info window, latitude, longitude

if ($('#map').length > 0) {
    var locations = [
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES],
      ['<h4 class="locations-map-link"><a href="URL" target="_blank">ADDRESS</a></p>', COORDINATES]
    ];

    // Setup the different icons and shadows
    var iconURLPrefix = 'http://www.msbcollege.edu';

    var icons = ['/wp-content/uploads/2016/02/map-icon-rebrand.png']
    var icons_length = icons.length;


    var shadow = {
        anchor: new google.maps.Point(15, 33),
        url: iconURLPrefix + 'msmarker.shadow.png'
    };

    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(-37.92, 151.25),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false,
        streetViewControl: false,
        panControl: false,
        zoomControlOptions: {
            position: google.maps.ControlPosition.LEFT_BOTTOM
        }
    });

    var infowindow = new google.maps.InfoWindow({
        maxWidth: 160
    });

    var marker;
    var markers = new Array();

    var iconCounter = 0;

    // Add the markers and infowindows to the map
    for (var i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map,
            icon: icons[iconCounter],
            shadow: shadow
        });

        markers.push(marker);

        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));

        iconCounter++;
        // We only have a limited number of possible icon colors, so we may have to restart the counter
        if (iconCounter >= icons_length) {
            iconCounter = 0;
        }
    }

    function AutoCenter() {
        //  Create a new viewpoint bound
        var bounds = new google.maps.LatLngBounds();
        //  Go through each...
        $.each(markers, function (index, marker) {
            bounds.extend(marker.position);
        });
        //  Fit these bounds to the map
        map.fitBounds(bounds);
    }
    AutoCenter();


} else {
    //do nothing
}
});

1 个答案:

答案 0 :(得分:1)

您需要包含带有脚本标记的javascript文件(确保路径正确)

<script src="map.js"></script>

然后在页面加载完成后调用javascript文件中的函数。

<script language="javascript">
        window.onload = function(){locationsMap()}
    </script>

执行此操作的便捷方法是使用jQuerys .ready方法。

$(document).ready(locationsMap)