【发布时间】:2017-01-20 17:07:32
【问题描述】:
var marker = new google.maps.Marker({
map: map,
position: point,
icon: 'pointer.png',
title: "test"
});
我的地图加载器完美,但我的标记不会出现。
我不太明白为什么会这样?
编辑 1:
这是整个函数,我希望它能帮助回答你的一些问题:
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: new google.maps.LatLng(<?php echo $userRow['latitude']; ?>, <?php echo $userRow['longitude']; ?>),
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
downloadUrl('http://xxx.esy.es/test/test-marker.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var name = markerElem.getAttribute('username');
var address = markerElem.getAttribute('address');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('latitude')),
parseFloat(markerElem.getAttribute('longitude')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var marker = new google.maps.Marker({
map: map,
position: point,
icon: 'pointer.png',
title: "test"
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
});
}
编辑 2
这是我的 xml 代码,它工作正常!只是我在地图上的标记不起作用
<?php
include_once 'test-dbconnect.php';
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
$sql = "select * from tbl_users";
$result = mysqli_query($DBcon, $sql) or die("Error in Selecting " . mysqli_error($DBcon));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
echo '<marker ';
echo 'name="' . parseToXML($row['username']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['latitude'] . '" ';
echo 'lng="' . $row['longitude'] . '" ';
echo '/>';
}
//close the db connection
mysqli_close($DBcon);
// End XML file
echo '</markers>';
?>
【问题讨论】:
-
您是否抛出任何 javascript 错误?你确定你的图片路径正确吗?
-
我没有收到任何错误,是的,我确定我的图像路径是正确的。
-
将给定代码保留在地图“空闲”事件中
-
什么是
map?point是什么?这段代码在哪里执行? (it works for me if the answers to those questions make sense) 请提供一个 minimal reproducible example 来证明问题。 -
使用你的javascript控制台!并调试您的代码。
point是你所期望的吗?'pointer.png'是否可用(意味着它与您的 javascript 文件位于同一目录中)?等
标签: javascript php xml google-maps google-maps-api-3