简体   繁体   English

谷歌映射IE8的.setMap问题

[英]Google maps .setMap issue for IE8

So I've got a bit of a problem here. 所以我在这里遇到了一些问题。 This script works fine in all browsers apart from IE8 除IE8之外,此脚本在所有浏览器中都能正常运行

The route() function is called - and is given a Google maps encoded polygon and polyline. 调用route()函数 - 并给出Google地图编码的多边形和折线。 The script breaks every time IE8 tries to do setMap 每次IE8尝试执行setMap时脚本都会中断

I'm thinking the issue is something to do with map being out of its scope? 我认为问题与地图超出范围有关? Or a trailing comma error? 或者是一个尾随的逗号错误? I just spent the last few hours playing with thisand I couldn't fix it. 我只花了最后几个小时玩这个,我无法解决它。

Any ideas?? 有任何想法吗?? Much appreciated. 非常感激。 Dont normally like to paste the whole code as it's a bit overwhelming but it's needed in this case. 通常不喜欢粘贴整个代码,因为它有点压倒性但在这种情况下需要它。

var markersArray = [];


function initMap(lat,lng) {
    if(lat) {

        var myLatLng = new google.maps.LatLng(lat, lng);
        var zoomi=13;
    }else{

        var myLatLng = new google.maps.LatLng(54.162434, -2.285156);
        var zoomi=5;
    }

    var myOptions = {
        zoom: zoomi,
         zoomControl: true,
        panControl: false,
       streetViewControl: false,
       zoomControlOptions: {
            style: google.maps.ZoomControlStyle.LARGE,
            position: google.maps.ControlPosition.LEFT_CENTER
     },
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map"), myOptions);
}


function route(journeyPolyy,journeyLine,startLat,startLng,endLat,endLng) {

reset();

    var startLatLng=startLat + ',' + startLng;

    var start = new google.maps.LatLng(startLat,startLng);
    var end = new google.maps.LatLng(endLat,endLng);

    var decodedPath = google.maps.geometry.encoding.decodePath(journeyLine);
      var flightPath = new google.maps.Polyline({
        path: decodedPath,
        strokeColor: 'blue',
        strokeOpacity: 0.5,
        strokeWeight: 5
      });
    markersArray.push(flightPath);

    flightPath.setMap(map);
     var marker = new google.maps.Marker({
        position: start
    });

    markersArray.push(marker);


    marker.setMap(map);

     var endPpoint = new google.maps.Marker({
        position: end
    });
    markersArray.push(endPpoint);


    endPpoint.setMap(map);

    poly = journeyPolyy.replace(/\\\\/g,"\\");

    var decodedPath = google.maps.geometry.encoding.decodePath(poly);

    var decodedLevels = decodeLevels("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");

    var setRegion = new google.maps.Polygon({
        path: decodedPath,
        levels: decodedLevels,
        strokeColor: "#FF0000",
        strokeOpacity: 0.8,
        strokeWeight: 1,
        fillColor    : '#FF0000',
        fillOpacity  : 0.1,
        map: map
    });
    setRegion.setMap(map);
    markersArray.push(setRegion);
        var obj= document.getElementById('map');
    obj.style.visibility='visible';
    HideContent('mainContent');
    map.setCenter(start); 
    map.setZoom(13); 
}


function decodeLevels(encodedLevelsString) {
    var decodedLevels = [];

    for (var i = 0; i < encodedLevelsString.length; ++i) {
        var level = encodedLevelsString.charCodeAt(i) - 63;
        decodedLevels.push(level);
    }
    return decodedLevels;
}

function reset() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}

There is a naming-conflict (the map-objects name is the same like the id of the element) 存在命名冲突(map-objects名称与元素的id相同)

You may either use a different id for the div, a different name for the map-variable, or declare the map-variable in global scope: 您可以为div使用不同的id,为map-variable使用不同的名称,或者在全局范围内声明map-variable:

var map;

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

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