4

我有一个 MVC4 应用程序,它将 GPS 坐标绘制到 twitter 引导模式中加载的谷歌地图。我的问题是,当使用 IE 10 时,地图有时会加载所有的图块(有时不会),如下图所示。

在此处输入图像描述

该地图在 Chrome 版本 27 中根本不显示。

在此处输入图像描述

这是地图的 HTML 代码

<div id="VehicleMovementModal" class="modal hide fade">
<div class="modal-header">
    <button class="close" data-dismiss="modal">×</button>
    <h3>Vehicle movement for the last 24 hours</h3>
</div>
<div class="modal-body">
    <div id="mapCanvas" class="mapCanvas">
    </div>
</div>
<br />
<div id="VehicleMovementLinkContainer" class="VehicleMovementLinkContainer">
    <i class="icon-download-alt"></i> <a href="~/Vehicle/GetGPSCoordinateCSV?vehicleId=@vehicleModel.VehicleData.Id&hours=24">Export GPS coordinates for the last 24 hours</a>
    <br />
    <i class="icon-download-alt"></i> <a href="~/Vehicle/GetGPSCoordinateCSV?vehicleId=@vehicleModel.VehicleData.Id&hours=48">Export GPS coordinates for the last 48 hours</a>
</div>

我如何从我的 HTML 页面中调用 javascript

<script src="http://maps.googleapis.com/maps/api/js?key=aaaaaaa&sensor=false"></script>
<script type="text/javascript" src="~/Content/js/PlotVehicleMovement.js"></script>
<script type="text/javascript">
    google.maps.event.addDomListener(window, 'load', initialize());
</script> 

以及我用来绘制地图的 javascript

function initialize() {

var gpsCoordinateCollection = $('#gpsCoordinates').val().split(',');

if (gpsCoordinateCollection.length > 0) {

    // The last GPS coordinates received + the date it was received.
    var gpsCoordinatePacket = gpsCoordinateCollection[0].split(';');

    if (gpsCoordinatePacket.length > 0) {

        var mapProp = {
            center: new google.maps.LatLng(gpsCoordinatePacket[0], gpsCoordinatePacket[1]),
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.HYBRID
        };

        var map = new google.maps.Map(document.getElementById("mapCanvas"), mapProp);

        plotVehicleMovement(map, gpsCoordinateCollection);
    }
  }
}

function plotVehicleMovement(map, gpsCoordinateCollection) {

// Define the clickable area of the markers.
var shape = {
    coord: [1, 1, 1, 20, 18, 20, 18, 1],
    type: 'poly'
};

var polyLineLatLongCollection = [];

for (var i = 0; i < gpsCoordinateCollection.length; i++) {

    var gpsCoordinatePacket = gpsCoordinateCollection[i].split(';');

    var latLng = new google.maps.LatLng(gpsCoordinatePacket[0], gpsCoordinatePacket[1]);

    polyLineLatLongCollection.push(latLng);

    var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        icon: {
            path: google.maps.SymbolPath.CIRCLE,
            scale: 4,
            strokeColor: '#F5B71A'
        },
        shape: shape
    });
    marker.setMap(map);
}

var polyLine = new google.maps.Polyline({
    path: polyLineLatLongCollection,
    strokeColor: '#F5B71A',
    strokeOpacity: 1.0,
    strokeWeight: 1
});

polyLine.setMap(map);
}
4

1 回答 1

4

好的,问题是我的地图包含在 twitter 引导模式 div 中,在单击链接之前,它对用户是隐藏的。我的谷歌地图高度和宽度被指定为相对于它包含父级的百分比。因此,我必须包含以下 javascript 函数,以确保在显示模式时调整我的地图大小。

function ResizeMap() {
    google.maps.event.trigger(map, "resize");
}

我添加了这个 jquery 函数来触发调整大小

$("#VehicleMovementModal").on('shown', function () {
    ResizeMap();
});

现在一切都按预期工作。

于 2013-07-01T15:35:36.767 回答