I'm working on google maps API v3 (with php and mysql) and I'm searching for a solution to open an infowindow from a link.
Explanation:
I create a google maps with some marker (customer) and I create a list in an another loop directly from my database and I wish to have the possibility to click on the name of my customer to open the infowindow on map.
Thanks to @MrUpsidown I found this code and adapt it, but it doesn't work.
customer page:
<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>
Here myphpsqlajax_map_v5.php (customized name for test)
<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>
Then, here my gen_map-xml2.php
<?php
require("inc/header.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
$xmlStr=str_replace("é",'é',$xmlStr);
$xmlStr=str_replace("è",'è',$xmlStr);
$xmlStr=str_replace("à",'à',$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>';
?>
Can you help me ?