Google 地图 API - ControlPosition with Infowindow
Google Maps API - ControlPosition with Infowindow
是否可以使用类似于 maps.controls[google.maps.ControlPosition.TOP_CENTER].push(infoWindow)
的方法让控件 window 显示在地图的顶部中央?
我有一个带有多边形的地图来启用鼠标悬停事件。我还试图在地图的顶部中心显示 polgyon 弹出窗口的信息 window,并在鼠标移出 时消失(我希望避免使用 CSS,因为我想这是尽可能纯粹的 Google API 但我知道有时这是不可能的)。
多边形很棒,但信息window 一直显示在屏幕的左上角。任何帮助,将不胜感激!谢谢!
目前我在尝试鼠标悬停时收到此错误消息:
"Uncaught TypeError: Cannot read property 'zIndex' of undefined"
用于创建多边形和添加侦听器的部分的 JS:
function createPolygon(city, url, name){
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions(
{
fillOpacity: 0.4,
}
);
infoWindow.setContent(name);
console.log(infoWindow);
console.log("City: " + city + " URL: " + url + " Name: " + name);
infoWindow.open(map, this);
map.controls[google.maps.ControlPosition.TOP_CENTER].push(infoWindow);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions(
{
fillOpacity: 0.1,
}
);
infoWindow.close();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function(){
console.log("aaaawww yis");
window.location = url;
});
}
多边形作为 InfoWindow.open (the only thing that can be used there at present is a google.maps.Marker)
的第二个参数无效
要使用信息窗口,请设置位置(使用 .setPosition(latLng))
function createPolygon(city, url, name){
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
for (var i=0; i<city.getPath().getLength(); i++) {
bounds.extend(city.getPath().getAt(i));
}
var center = bounds.getCenter();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions(
{
fillOpacity: 0.4,
}
);
infoWindow.setContent(name);
infoWindow.setPosition(center);
console.log(infoWindow);
console.log("City: " + city + " URL: " + url + " Name: " + name);
infoWindow.open(map);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions(
{
fillOpacity: 0.1,
}
);
infoWindow.close();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function(){
console.log("aaaawww yis");
window.location = url;
});
}
工作代码片段:
var map;
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(40, -117),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var arr = new Array();
var polygons = [];
var bounds = new google.maps.LatLngBounds();
// downloadUrl("subdivision-coordinates.php", function(data) {
var xml = xmlParse(xmlString);
var subdivision = xml.getElementsByTagName("subdivision");
// alert(subdivision.length);
for (var i = 0; i < subdivision.length; i++) {
arr = [];
var coordinates = xml.documentElement.getElementsByTagName("subdivision")[i].getElementsByTagName("coord");
for (var j = 0; j < coordinates.length; j++) {
arr.push(new google.maps.LatLng(
parseFloat(coordinates[j].getAttribute("lat")),
parseFloat(coordinates[j].getAttribute("lng"))
));
bounds.extend(arr[arr.length - 1])
}
polygons.push(new google.maps.Polygon({
paths: arr,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.1
}));
polygons[polygons.length - 1].setMap(map);
createPolygon(polygons[polygons.length - 1], "", "polygon " + polygons.length);
}
// });
map.fitBounds(bounds);
}
function createPolygon(city, url, name) {
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < city.getPath().getLength(); i++) {
bounds.extend(city.getPath().getAt(i));
}
var center = bounds.getCenter();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions({
fillOpacity: 0.4,
});
infoWindow.setContent(name);
infoWindow.setPosition(center);
console.log(infoWindow);
console.log("City: " + city + " URL: " + url + " Name: " + name);
infoWindow.open(map);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions({
fillOpacity: 0.1,
});
infoWindow.close();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function() {
console.log("aaaawww yis");
window.location = url;
});
}
var xmlString = '<subdivisions><subdivision name="Auburn Hills"><coord lat="39.00748" lng="-92.323222"/><coord lat="39.000843" lng="-92.323523"/><coord lat="39.000509" lng="-92.311592"/><coord lat="39.007513" lng="-92.311378"/><coord lat="39.00748" lng="-92.323222"/></subdivision><subdivision name="Vanderveen"><coord lat="38.994206" lng="-92.350645"/><coord lat="38.985033" lng="-92.351074"/><coord lat="38.984699" lng="-92.343092"/><coord lat="38.981163" lng="-92.342234"/><coord lat="38.984663" lng="-92.3335"/><coord lat="38.993472" lng="-92.333179"/><coord lat="38.994206" lng="-92.350645"/></subdivision><subdivisions>';
/**
* Parses the given XML string and returns the parsed document in a
* DOM data structure. This function will return an empty DOM node if
* XML parsing is not supported in this browser.
* @param {string} str XML string.
* @return {Element|Document} DOM.
*/
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
google.maps.event.addDomListener(window, 'load', initialize);
#map-canvas,
body,
html {
height: 100%;
width: 100%;
}
<script src="http://maps.google.com/maps/api/js"></script>
<div id="map-canvas"></div>
如果您想在地图而不是信息窗口上创建一个控件,您可以这样做(您也可以将内容放在您页面上地图之外的其他固定 div 中):
function createPolygon(city, url, name) {
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < city.getPath().getLength(); i++) {
bounds.extend(city.getPath().getAt(i));
}
var center = bounds.getCenter();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions({
fillOpacity: 0.4,
});
var textonome = document.createElement('DIV');
textonome.className = "caixabranca";
textonome.innerHTML = "<big><big><b><center>" + name + "</center></big></big></b><br>" + url;
map.controls[google.maps.ControlPosition.TOP_CENTER].push(textonome);
console.log("City: " + city + " URL: " + url + " Name: " + name);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions({
fillOpacity: 0.1,
});
map.controls[google.maps.ControlPosition.TOP_CENTER].clear();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function() {
console.log("aaaawww yis");
window.location = url;
});
}
工作代码片段:
var map;
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(40, -117),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var arr = new Array();
var polygons = [];
var bounds = new google.maps.LatLngBounds();
// downloadUrl("subdivision-coordinates.php", function(data) {
var xml = xmlParse(xmlString);
var subdivision = xml.getElementsByTagName("subdivision");
// alert(subdivision.length);
for (var i = 0; i < subdivision.length; i++) {
arr = [];
var coordinates = xml.documentElement.getElementsByTagName("subdivision")[i].getElementsByTagName("coord");
for (var j = 0; j < coordinates.length; j++) {
arr.push(new google.maps.LatLng(
parseFloat(coordinates[j].getAttribute("lat")),
parseFloat(coordinates[j].getAttribute("lng"))
));
bounds.extend(arr[arr.length - 1])
}
polygons.push(new google.maps.Polygon({
paths: arr,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.1
}));
polygons[polygons.length - 1].setMap(map);
createPolygon(polygons[polygons.length - 1], "", "polygon " + polygons.length);
}
// });
map.fitBounds(bounds);
}
function createPolygon(city, url, name) {
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < city.getPath().getLength(); i++) {
bounds.extend(city.getPath().getAt(i));
}
var center = bounds.getCenter();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions({
fillOpacity: 0.4,
});
var textonome = document.createElement('DIV');
textonome.className = "caixabranca";
textonome.innerHTML = "<big><big><b><center>" + name + "</center></big></big></b><br>" + url;
map.controls[google.maps.ControlPosition.TOP_CENTER].push(textonome);
console.log(infoWindow);
console.log("City: " + city + " URL: " + url + " Name: " + name);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions({
fillOpacity: 0.1,
});
map.controls[google.maps.ControlPosition.TOP_CENTER].clear();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function() {
console.log("aaaawww yis");
window.location = url;
});
}
var xmlString = '<subdivisions><subdivision name="Auburn Hills"><coord lat="39.00748" lng="-92.323222"/><coord lat="39.000843" lng="-92.323523"/><coord lat="39.000509" lng="-92.311592"/><coord lat="39.007513" lng="-92.311378"/><coord lat="39.00748" lng="-92.323222"/></subdivision><subdivision name="Vanderveen"><coord lat="38.994206" lng="-92.350645"/><coord lat="38.985033" lng="-92.351074"/><coord lat="38.984699" lng="-92.343092"/><coord lat="38.981163" lng="-92.342234"/><coord lat="38.984663" lng="-92.3335"/><coord lat="38.993472" lng="-92.333179"/><coord lat="38.994206" lng="-92.350645"/></subdivision><subdivisions>';
/**
* Parses the given XML string and returns the parsed document in a
* DOM data structure. This function will return an empty DOM node if
* XML parsing is not supported in this browser.
* @param {string} str XML string.
* @return {Element|Document} DOM.
*/
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
google.maps.event.addDomListener(window, 'load', initialize);
#map-canvas,
body,
html {
height: 100%;
width: 100%;
}
#infotop {
text-align: center;
height: 50px;
/*n usado*/
}
#infocontent {
margin: 30px;
text-align: justify;
height: 84%;
}
.caixabranca {
border:solid 1px #cccccc;
background-color:#FFFFFF;
font-size:11px;
font-family:verdana,"sans-serif";
color:#000;
padding:5px;
}
<script src="http://maps.google.com/maps/api/js"></script>
<div id="map-canvas"></div>
是否可以使用类似于 maps.controls[google.maps.ControlPosition.TOP_CENTER].push(infoWindow)
的方法让控件 window 显示在地图的顶部中央?
我有一个带有多边形的地图来启用鼠标悬停事件。我还试图在地图的顶部中心显示 polgyon 弹出窗口的信息 window,并在鼠标移出 时消失(我希望避免使用 CSS,因为我想这是尽可能纯粹的 Google API 但我知道有时这是不可能的)。
多边形很棒,但信息window 一直显示在屏幕的左上角。任何帮助,将不胜感激!谢谢!
目前我在尝试鼠标悬停时收到此错误消息: "Uncaught TypeError: Cannot read property 'zIndex' of undefined"
用于创建多边形和添加侦听器的部分的 JS:
function createPolygon(city, url, name){
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions(
{
fillOpacity: 0.4,
}
);
infoWindow.setContent(name);
console.log(infoWindow);
console.log("City: " + city + " URL: " + url + " Name: " + name);
infoWindow.open(map, this);
map.controls[google.maps.ControlPosition.TOP_CENTER].push(infoWindow);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions(
{
fillOpacity: 0.1,
}
);
infoWindow.close();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function(){
console.log("aaaawww yis");
window.location = url;
});
}
多边形作为 InfoWindow.open (the only thing that can be used there at present is a google.maps.Marker)
的第二个参数无效要使用信息窗口,请设置位置(使用 .setPosition(latLng))
function createPolygon(city, url, name){
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
for (var i=0; i<city.getPath().getLength(); i++) {
bounds.extend(city.getPath().getAt(i));
}
var center = bounds.getCenter();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions(
{
fillOpacity: 0.4,
}
);
infoWindow.setContent(name);
infoWindow.setPosition(center);
console.log(infoWindow);
console.log("City: " + city + " URL: " + url + " Name: " + name);
infoWindow.open(map);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions(
{
fillOpacity: 0.1,
}
);
infoWindow.close();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function(){
console.log("aaaawww yis");
window.location = url;
});
}
工作代码片段:
var map;
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(40, -117),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var arr = new Array();
var polygons = [];
var bounds = new google.maps.LatLngBounds();
// downloadUrl("subdivision-coordinates.php", function(data) {
var xml = xmlParse(xmlString);
var subdivision = xml.getElementsByTagName("subdivision");
// alert(subdivision.length);
for (var i = 0; i < subdivision.length; i++) {
arr = [];
var coordinates = xml.documentElement.getElementsByTagName("subdivision")[i].getElementsByTagName("coord");
for (var j = 0; j < coordinates.length; j++) {
arr.push(new google.maps.LatLng(
parseFloat(coordinates[j].getAttribute("lat")),
parseFloat(coordinates[j].getAttribute("lng"))
));
bounds.extend(arr[arr.length - 1])
}
polygons.push(new google.maps.Polygon({
paths: arr,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.1
}));
polygons[polygons.length - 1].setMap(map);
createPolygon(polygons[polygons.length - 1], "", "polygon " + polygons.length);
}
// });
map.fitBounds(bounds);
}
function createPolygon(city, url, name) {
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < city.getPath().getLength(); i++) {
bounds.extend(city.getPath().getAt(i));
}
var center = bounds.getCenter();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions({
fillOpacity: 0.4,
});
infoWindow.setContent(name);
infoWindow.setPosition(center);
console.log(infoWindow);
console.log("City: " + city + " URL: " + url + " Name: " + name);
infoWindow.open(map);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions({
fillOpacity: 0.1,
});
infoWindow.close();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function() {
console.log("aaaawww yis");
window.location = url;
});
}
var xmlString = '<subdivisions><subdivision name="Auburn Hills"><coord lat="39.00748" lng="-92.323222"/><coord lat="39.000843" lng="-92.323523"/><coord lat="39.000509" lng="-92.311592"/><coord lat="39.007513" lng="-92.311378"/><coord lat="39.00748" lng="-92.323222"/></subdivision><subdivision name="Vanderveen"><coord lat="38.994206" lng="-92.350645"/><coord lat="38.985033" lng="-92.351074"/><coord lat="38.984699" lng="-92.343092"/><coord lat="38.981163" lng="-92.342234"/><coord lat="38.984663" lng="-92.3335"/><coord lat="38.993472" lng="-92.333179"/><coord lat="38.994206" lng="-92.350645"/></subdivision><subdivisions>';
/**
* Parses the given XML string and returns the parsed document in a
* DOM data structure. This function will return an empty DOM node if
* XML parsing is not supported in this browser.
* @param {string} str XML string.
* @return {Element|Document} DOM.
*/
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
google.maps.event.addDomListener(window, 'load', initialize);
#map-canvas,
body,
html {
height: 100%;
width: 100%;
}
<script src="http://maps.google.com/maps/api/js"></script>
<div id="map-canvas"></div>
如果您想在地图而不是信息窗口上创建一个控件,您可以这样做(您也可以将内容放在您页面上地图之外的其他固定 div 中):
function createPolygon(city, url, name) {
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < city.getPath().getLength(); i++) {
bounds.extend(city.getPath().getAt(i));
}
var center = bounds.getCenter();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions({
fillOpacity: 0.4,
});
var textonome = document.createElement('DIV');
textonome.className = "caixabranca";
textonome.innerHTML = "<big><big><b><center>" + name + "</center></big></big></b><br>" + url;
map.controls[google.maps.ControlPosition.TOP_CENTER].push(textonome);
console.log("City: " + city + " URL: " + url + " Name: " + name);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions({
fillOpacity: 0.1,
});
map.controls[google.maps.ControlPosition.TOP_CENTER].clear();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function() {
console.log("aaaawww yis");
window.location = url;
});
}
工作代码片段:
var map;
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(40, -117),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var arr = new Array();
var polygons = [];
var bounds = new google.maps.LatLngBounds();
// downloadUrl("subdivision-coordinates.php", function(data) {
var xml = xmlParse(xmlString);
var subdivision = xml.getElementsByTagName("subdivision");
// alert(subdivision.length);
for (var i = 0; i < subdivision.length; i++) {
arr = [];
var coordinates = xml.documentElement.getElementsByTagName("subdivision")[i].getElementsByTagName("coord");
for (var j = 0; j < coordinates.length; j++) {
arr.push(new google.maps.LatLng(
parseFloat(coordinates[j].getAttribute("lat")),
parseFloat(coordinates[j].getAttribute("lng"))
));
bounds.extend(arr[arr.length - 1])
}
polygons.push(new google.maps.Polygon({
paths: arr,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.1
}));
polygons[polygons.length - 1].setMap(map);
createPolygon(polygons[polygons.length - 1], "", "polygon " + polygons.length);
}
// });
map.fitBounds(bounds);
}
function createPolygon(city, url, name) {
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < city.getPath().getLength(); i++) {
bounds.extend(city.getPath().getAt(i));
}
var center = bounds.getCenter();
google.maps.event.addListener(city, 'mouseover', function() {
this.setOptions({
fillOpacity: 0.4,
});
var textonome = document.createElement('DIV');
textonome.className = "caixabranca";
textonome.innerHTML = "<big><big><b><center>" + name + "</center></big></big></b><br>" + url;
map.controls[google.maps.ControlPosition.TOP_CENTER].push(textonome);
console.log(infoWindow);
console.log("City: " + city + " URL: " + url + " Name: " + name);
console.log("awww yis");
});
google.maps.event.addListener(city, 'mouseout', function() {
this.setOptions({
fillOpacity: 0.1,
});
map.controls[google.maps.ControlPosition.TOP_CENTER].clear();
console.log("awww no");
});
google.maps.event.addListener(city, 'click', function() {
console.log("aaaawww yis");
window.location = url;
});
}
var xmlString = '<subdivisions><subdivision name="Auburn Hills"><coord lat="39.00748" lng="-92.323222"/><coord lat="39.000843" lng="-92.323523"/><coord lat="39.000509" lng="-92.311592"/><coord lat="39.007513" lng="-92.311378"/><coord lat="39.00748" lng="-92.323222"/></subdivision><subdivision name="Vanderveen"><coord lat="38.994206" lng="-92.350645"/><coord lat="38.985033" lng="-92.351074"/><coord lat="38.984699" lng="-92.343092"/><coord lat="38.981163" lng="-92.342234"/><coord lat="38.984663" lng="-92.3335"/><coord lat="38.993472" lng="-92.333179"/><coord lat="38.994206" lng="-92.350645"/></subdivision><subdivisions>';
/**
* Parses the given XML string and returns the parsed document in a
* DOM data structure. This function will return an empty DOM node if
* XML parsing is not supported in this browser.
* @param {string} str XML string.
* @return {Element|Document} DOM.
*/
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
google.maps.event.addDomListener(window, 'load', initialize);
#map-canvas,
body,
html {
height: 100%;
width: 100%;
}
#infotop {
text-align: center;
height: 50px;
/*n usado*/
}
#infocontent {
margin: 30px;
text-align: justify;
height: 84%;
}
.caixabranca {
border:solid 1px #cccccc;
background-color:#FFFFFF;
font-size:11px;
font-family:verdana,"sans-serif";
color:#000;
padding:5px;
}
<script src="http://maps.google.com/maps/api/js"></script>
<div id="map-canvas"></div>