gpt4 book ai didi

javascript - IE8 的 Google map API 问题

转载 作者:行者123 更新时间:2023-11-28 10:59:05 25 4
gpt4 key购买 nike

我正在使用 Google map API 编写一些代码,它在所有浏览器(FF、IE9、Chrome)中都能正常工作,但 IE8 或更低版本中,我已将 map 分配给一个名为 Map 的全局变量,该变量会被填充,但是当addMarker 函数在 IE8 中被调用,Map 全局为 null,但是当我从定位器函数调用它时,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);
}
});
};

以下是我调用函数的方式:

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') });

感谢您的帮助。

最佳答案

我是这样解决这个问题的。

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

关于javascript - IE8 的 Google map API 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11934385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com