简体   繁体   English

IE8的Google Maps API问题

[英]Google Maps API issue with IE8

I'm writing some code with the Google Maps API, it works fine in all browsers (FF, IE9, Chrome) but IE8 or below, I have assigned the map to a global variable called Map, which gets populated but when the addMarker function gets called the Map global is null in IE8, but the addMarker function does work when I call it from the locator function, I have included all these functions below. 我正在使用Google Maps API编写一些代码,它在所有浏览器(FF,IE9,Chrome)上都能正常工作,但在IE8或更低版本中,我已将地图分配给名为Map的全局变量,该变量会被填充,但是使用addMarker函数时在IE8中被称为Map global为null,但是当我从locator函数调用时,addMarker函数确实起作用,我在下面包括了所有这些函数。

var GoogleMaps = {};
var Map = null;
var init = (function () {
"use strict";

var MapType = null;
var ZoomLevel = null;
var ControlPos = null;
var ControlSize = null;
var myLatLong = null;

var Geocoder;
var result = null;

GoogleMaps.setup = function (options) {

    myLatLong = new google.maps.LatLng(24.886436490787712, -70.26855468754);

    if (google.loader.ClientLocation) {
        myLatLong = new google.maps.LatLng(
            google.loader.ClientLocation.latitude,
            google.loader.ClientLocation.longitude);
    } else if (options.Lat !== null && options.Long !== null) {
        options.Location = new google.maps.LatLng(options.Lat, options.Long);
    } else {
        // Else centre to UK
        options.Location = new google.maps.LatLng(52.961875, -1.419433);
    }

    if (options.MapType.toUpperCase() === 'ROADMAP') {
        MapType = google.maps.MapTypeId.ROADMAP;
    } else if (options.MapType.toUpperCase() === 'TERRAIN') {
        MapType = google.maps.MapTypeId.TERRAIN;
    } else if (options.MapType.toUpperCase() === 'HYBRID') {
        MapType = google.maps.MapTypeId.HYBRID;
    } else {
        MapType = google.maps.MapTypeId.SATELLITE;
    }

    // Check zoom level, if not set then set to zoom level 8.
    if (options.ZoomLevel) {
        ZoomLevel = options.ZoomLevel;
    } else {
        ZoomLevel = 8;
    }

    var mapOptions = {
        center: myLatLong,
        zoom: ZoomLevel,
        mapTypeId: MapType
    };

    var mapDiv = document.getElementById('canvas');
    // Map gets initiated here 
    window.Map = new google.maps.Map(mapDiv, mapOptions);

    delete options.MapType;
    delete options.Lat;
    delete options.Long;
    delete options.ZoomLevel;
};

GoogleMaps.addMarker = function (options) {
    var Location = null;
    var Animation = null;
    var Title = null;
    var Draggable = null;
    var Content = null;
    var InfoWindow = null;
    var Flat = null;
    var Clickable = null;

    if (options.lat !== null && options.long !== null) {
        Location = new google.maps.LatLng(options.lat, options.long);
        ;
    } else {
        Location = myLatLong;
    }

    if (typeof(options.position) !== "undefined") {
        Location = options.position;   
    }

    if (options.animation.toUpperCase() === 'BOUNCE') {
        Animation = google.maps.Animation.BOUNCE;
    } else if (options.animation.toUpperCase() === 'DROP') {
        Animation = google.maps.Animation.DROP;
    } else {
        Animation = google.maps.Animation.NONE;
    }

    if (options.draggable !== null && options.draggable === 'true') {
        Draggable = true;
    } else {
        Draggable = false;
    }

    if (options.title !== null) {
        Title = options.title;
    } else {
        Title = null;
    }

    if (options.content !== null) {
        Content = options.content;
        InfoWindow = new google.maps.InfoWindow({
            content: Content
        });
    }

    if (options.flat !== null && options.flat === 'true') {
        Flat = true;
    } else {
        Flat = false;
    }

    if (options.clickable !== null && options.clickable === 'true') {
        Clickable = true;
    } else {
        Clickable = false;
    }

        // Gets used in this section
        var Marker = new google.maps.Marker({
        position: Location,
        map: window.Map,
        animation: Animation,
        draggable: Draggable,
        title: Title,
        flat: Flat,
        clickable: Clickable,
        zIndex: 1
    });
    // and sets map here
    Marker.setMap(window.Map);
    if (options.content !== null) {
        google.maps.event.addListener(Marker, 'click', function (e) {
            InfoWindow.open(window.Map, this);
            google.maps.event.addListener(window.Map, 'click', function (e) {
                InfoWindow.close(window.Map, window.Marker);
            });
        });
    }
    google.maps.event.addListener(Marker, 'dragend', function (e) {
    });

    delete options.lat;
    delete options.long;
    delete options.animation;
    delete options.title;
    delete options.content;
    delete options.flat;
    delete options.draggable;
    delete options.clickable;
};

GoogleMaps.Locator = function (result) {
    var address = null;
    Geocoder = new google.maps.Geocoder();
    address = result;
    Geocoder.geocode({ 'address': address }, function (response, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            window.Map.setCenter(response[0].geometry.location);
            var Location = new google.maps.LatLng(response[0].geometry.location.Xa, response[0].geometry.location.Ya);
            var markerOptions = {
                animation: "drop",
                draggable: "true",
                content: 'Hello World!',
                title: "Hello",
                position: Location
            };
            GoogleMaps.addMarker(markerOptions);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
};

Below is how I am calling the functions: 下面是我如何调用函数:

var markerOptions = {
        lat: 52.48278,
        long: -0.892089,
        animation: "drop",
        draggable: "true",
        content: 'Hello World!',
        title: "Click Me"
    };
 google.maps.event.addDomListener(window, 'load', function () { GoogleMaps.setMarker(markerOptions) });

 google.maps.event.addDomListener(window, 'load', function () { GoogleMaps.Locator('London') });

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

我解决了这样的问题。

<meta http-equiv=”X-UA-Compatible” content=”IE=EmulateIE7; IE=EmulateIE9″/>

Try changing this line in your setup 尝试在设置中更改此行

window.Map = new google.maps.Map(mapDiv, mapOptions);

to just 只是

Map = new google.maps.Map(mapDiv, mapOptions);

This way you accessing the global variable declared. 这样,您可以访问声明的全局变量。

when is GoogleMaps.setup called? 什么时候调用GoogleMaps.setup? Right now it looks like depending on the browser it can be called after functions attached by 现在看来,取决于浏览器,它可以在由

 google.maps.event.addDomListener(window, 'load', function () { ... });

and that's why map is not set when you call addMarker, but is already initialized when you receive callback from 这就是为什么在调用addMarker时未设置map的原因,但是当您收到来自

Geocoder.geocode(...)

To fix this make sure that GoogleMaps.setup is called before addMarker. 要解决此问题,请确保在addMarker之前调用GoogleMaps.setup。

IE8 has always meant trouble. IE8一直意味着麻烦。 :-) Try adding the following meta tag at the beginning of your <head> section: :-)尝试 <head>部分的开头添加以下元标记:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

Description here: 此处说明:

http://blogs.msdn.com/b/ie/archive/2008/06/10/introducing-ie-emulateie7.aspx http://blogs.msdn.com/b/ie/archive/2008/06/10/introducing-ie-emulateie7.aspx

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

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