gpt4 book ai didi

javascript - 如何单独显示信息框中的标记信息?

转载 作者:行者123 更新时间:2023-12-03 08:14:43 24 4
gpt4 key购买 nike

我买了this template来自 themeforest.com 的一切都很好。但有一件事。每次我单击标记时,信息框都会显示相同的内容。它的位置和信息不会改变。

我希望它在单击时显示标记信息。我可以为 locations 创建数组,但不能为 myOptions 创建数组。这是我的代码:

<div id="map"></div>

Javascript

$(document).ready(function() {
InitMap();
});
function LoadMap() {
var locations = new Array(<?= implode(',',$gps);?>);
var markers = new Array();
var mapOptions = {
center: new google.maps.LatLng(7.9597293,98.3333532),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};

var map = new google.maps.Map(document.getElementById('map'), mapOptions);

$.each(locations, function(index, location) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(location[0], location[1]),
map: map,
icon: "http://<?=$_SERVER['HTTP_HOST']?>/images/marker-transparent.png"
});

var myOptions = {
content: '<div class="infobox"><div class="image"><img src="http://html.realia.byaviators.com/assets/img/tmp/property-tiny-1.png" alt=""></div><div class="title"><a href="detail.html">1041 Fife Ave</a></div><div class="area"><span class="key">Area:</span><span class="value">200m<sup>2</sup></span></div><div class="price">€450 000.00</div><div class="link"><a href="detail.html">View more</a></div></div>',
disableAutoPan: false,
maxWidth: 0,
pixelOffset: new google.maps.Size(-146, -130),
zIndex: null,
closeBoxURL: "",
infoBoxClearance: new google.maps.Size(1, 1),
position: new google.maps.LatLng(location[0], location[1]),
isHidden: false,
pane: "floatPane",
enableEventPropagation: false
};
marker.infobox = new InfoBox(myOptions);
marker.infobox.isOpen = false;

var myOptions = {
draggable: true,
content: '<div class="marker"><div class="marker-inner"></div></div>',
disableAutoPan: true,
pixelOffset: new google.maps.Size(-21, -15),
position: new google.maps.LatLng(location[0], location[1]),
closeBoxURL: "",
isHidden: false,
// pane: "mapPane",
enableEventPropagation: true
};
marker.marker = new InfoBox(myOptions);
marker.marker.open(map, marker);
markers.push(marker);

google.maps.event.addListener(marker, "click", function (e) {
var curMarker = this;

$.each(markers, function (index, marker) {
// if marker is not the clicked marker, close the marker
if (marker !== curMarker) {
marker.infobox.close();
marker.infobox.isOpen = false;
}
});


if(curMarker.infobox.isOpen === false) {
curMarker.infobox.open(map, this);
curMarker.infobox.isOpen = true;
map.panTo(curMarker.getPosition());
} else {
curMarker.infobox.close();
curMarker.infobox.isOpen = false;
}

});
});
}

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

最佳答案

您可以创建 InfoBox单个实例,然后设置信息窗口的内容/位置,而不是为每个标记创建 InfoBox 实例单击标记后,如下所示:

function loadMap() {
var locations = new Array(['Phuket',7.877285, 98.392099],['Wat Chalong Temple',7.845826, 98.328928],['nn+Patong+Beach+Hotel',7.8739695,98.343519,13]);
var markers = new Array();
var mapOptions = {
center: new google.maps.LatLng(7.866062, 98.365492),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};

var infoBoxOptions = {
disableAutoPan: false,
maxWidth: 0,
pixelOffset: new google.maps.Size(-146, -130),
zIndex: null,
closeBoxURL: "",
infoBoxClearance: new google.maps.Size(1, 1),
isHidden: false,
pane: "floatPane",
enableEventPropagation: false,
closeBoxMargin: "10px 2px 2px 2px",
closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
};
var infoBox = new InfoBox(infoBoxOptions);

var map = new google.maps.Map(document.getElementById('map'), mapOptions);

$.each(locations, function(index, location) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(location[1], location[2]),
map: map,
});


google.maps.event.addListener(marker, "click", function (e) {
var curMarker = this;

map.panTo(curMarker.getPosition());
//infoBox.setPosition(curMarker.getPosition());
infoBox.setContent('<div class="infobox"><h2>Some information for ' + location[0] + ' goes here</h2></div>');
infoBox.open(map, this);
});
});
}


google.maps.event.addDomListener(window, 'load', loadMap);
#map {
width: 100%;
height: 400px;
}

.infobox {
display: inline-block;
zoom: 1;
background-color: #fff;
padding: 10px;
position: relative;
width: 270px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js"></script>
<div id="map"></div>

关于javascript - 如何单独显示信息框中的标记信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33993769/

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