gpt4 book ai didi

javascript - Google Maps V3 API 链接到 infowindow

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

我正在开发 google map API v3(使用 php 和 mysql),并且正在寻找从链接打开信息窗口的解决方案。

说明:

我创建了一个带有一些标记(客户)的谷歌地图,并直接从我的数据库在另一个循环中创建了一个列表,我希望能够单击我的客户的名字来打开 map 上的信息窗口。

感谢@MrUpsidown,我找到了这段代码并对其进行了修改,但它不起作用。

客户页面:

<div>
<? include('phpsqlajax_map_v5.php');?>
</div>
<div>
<?
$reponseENT = $bdd->query('
SELECT *
FROM client
WHERE adresse !=""
AND archive = "FAUX"
');
while($repE = $reponseENT->fetchObject())
{
?>
<p><a><? echo $repE->Nom." ".$repE->departement;?></a></p>
<?
}
?>
</div>

这里是myphpsqlajax_map_v5.php(测试用的自定义名称)

<script type="text/javascript">
//<![CDATA[
var markers = [];


function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(46.70, 2.00),
zoom: 6,
mapTypeId: 'roadmap'
});
var marker, i;

var infoWindow = new google.maps.InfoWindow;

// Change this depending on the name of your PHP file
downloadUrl("gen_map_xml2.php",function(data){
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var nument = markers[i].getAttribute("nument");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b><a href='reporting_client.php?client=" + nument + "'>" + name +"</a></b><br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}

function bindInfoWindow(marker, i,map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function(marker, i) {
infoWindow.setContent(html);
infoWindow.open(map, marker);
}
(marker, i));

// Push the marker to the 'markers' array
markers.push(marker);
}

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

// The function to trigger the marker click, 'id' is the reference index to the 'markers' array.
function myClick(id){
google.maps.event.trigger(markers[id], 'click');
});
}

function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;

request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};

request.open('GET', url, true);
request.send(null);
}

function doNothing() {}

//]]>
</script>


<body onload="load()">
<div id="map" style="width: 100%; height: 550px;display:block;"></div>
</body>

然后,这是我的 gen_map-xml2.php

<?php
require("inc/header.php");

function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
$xmlStr=str_replace("é",'&eacute;',$xmlStr);
$xmlStr=str_replace("è",'&egrave;',$xmlStr);
$xmlStr=str_replace("à",'&agrave;',$xmlStr);
return $xmlStr;
}

// Connexion à la base de données
$connection=mysql_connect ($host, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

// Vérification de la connexion
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

// Commande sql de sélection
$query = "
SELECT *
FROM markers
WHERE 1
";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'nument="' . $row['nument'] . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}

// End XML file
echo '</markers>';

?>

你能帮我吗?

最佳答案

成功!我创建了一个包含所有位置的 php 文件,并创建了一个具有相同条件的循环来进行精确的监听

<?php
require("inc/header.php");

$fichier ="location.php";
$ouverture = fopen($fichier, "r" );
$ecriture = fopen($fichier, "w+" );

$reponse = $bdd->query('
SELECT *
FROM client
WHERE 1
ORDER BY nom
');
while ($donnees = $reponse->fetch())
{
$id = $donnees['nument'];
$rsent = preg_replace("/'/","\'",$donnees['nom']);
$adresse = preg_replace("/'/","\'",$donnees['adresse']);
$lat = $donnees['lat'];
$lng = $donnees['lng'];
$type = $donnees['type'];
$location= "
['<a href=\"reporting_client.php?client=".$id."\">".$rsent."</a>
<br/>".$adresse."', ".$lat.", ".$lng."],\n
";
fwrite($ecriture,$location);
}
fclose($ouverture);
fclose($ecriture);
?>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
var markers = [];
function initialize() {

var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(40.714364, -74.005972),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);


var locations = [
<? include('location.php');?>
];

var marker, i;
var infowindow = new google.maps.InfoWindow();


google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: locations[i][3]
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));

markers.push(marker);
}

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

function myClick(id){
google.maps.event.trigger(markers[id], 'click');
}
</script>
<div id="googlemap" style="width: 100%; height: 500px;"></div>
<?
$i=0;
$reponseNOM = $bdd->query('
SELECT *
FROM client
WHERE 1
ORDER BY nom
');
while ($donneesNOM = $reponseNOM->fetch())
{
?>
<a href="#" onclick="myClick(<? echo $i;$i++?>);"><? echo $donneesNOM['nom'];?></a><br/>
<?
}
?>

关于javascript - Google Maps V3 API 链接到 infowindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25135535/

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