我有这段代码来填充谷歌地图上数据库中的标记,但它不起作用,xml 文件工作正常,我提到 https://developers.google.com/maps/articles/phpsqlajax_v3 ,但没有用。
markers.php代码
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jainmunilocator";
$connection=mysqli_connect($servername, $username, $password, $dbname);;
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT name,id,lat,lng FROM muni_location,munishri WHERE mid=id";
header('Content-Type: application/xml');
$result=mysqli_query($connection,$sql);
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)){
if(isset($row['id'])){ $id = $row['id']; }
if(isset($row['name'])){ $name = $row['name']; }
if(isset($row['lat'])){ $lat = $row['lat']; }
if(isset($row['lng'])){ $lng = $row['lng']; }
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("id",$id);
$newnode->setAttribute("name", $name);
$newnode->setAttribute("lat", $lat);
$newnode->setAttribute("lng", $lng);
}
}
echo $dom->saveXML();
?>
*GOOGLE map 标记代码**
<!DOCTYPE html >
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>PHP/MySQL & Google Maps Example</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
//<![CDATA[
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(22.6145,77.3418),
zoom: 7,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
var gm_marker = new Array();
// Change this depending on the name of your PHP file
downloadUrl("markers.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var id = markers[i].getAttribute("id");
var name = markers[i].getAttribute("name");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
//var html = "<b>" + name + "</b> <br/>" + id;
var html = name;
var icon = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
var marker = new google.maps.Marker({
map: map,
position: point,
title: name
});
addMarker(point,name);
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
//stackoverflow
function addMarker(latLng, contentstr) {
var marker = new google.maps.Marker({
position: latLng,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow = new google.maps.InfoWindow({
content: contentstr
});
});
gm_markers.push(marker);
}
setAllmap(map);
function setAllMap(map) {
for (var i = 0; i < gm_markers.length; i++) {
gm_markers[i].setMap(map);
}
function showMarkers() { setAllMap(map);}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function( ){
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
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>
</head>
<body onload="load()">
<div id="map-canvas-show" style="width: 100%; height: 300px;"></div>
</body>
</html>
最佳答案
我认为有两个元素是必不可少的,但我在您的代码中没有看到它们。这是来 self 的来源,我将它们放在这里是为了展示这些概念。您可以在 for 循环中实现此类事物或分解为函数。我比较喜欢函数,所以就这样做了。
首先,我要确认您的标记数组的长度不为 0——这是一个简单的健全性检查——确保您有要加载的标记。
构建 Google map 标记堆栈
虽然有标记...
使用 new 创建标记
使用 new 创建监听器
将标记作为标记对象数组插入 Google 的标记对象
// Add a marker to the map and push to the array.
function addMarker(latLng, contentstr) {
var marker = new google.maps.Marker({
position: latLng,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow = new google.maps.InfoWindow({
content: contentstr
});
});
gm_markers.push(marker);
}
要显示标记,请将标记分配给 map
将数组中的每个标记关联到 map Canvas 。
// Sets the map on all markers in the array.
function setAllMap(map) {
for (var i = 0; i < gm_markers.length; i++) {
gm_markers[i].setMap(map);
}
清除 Google map 标记(关闭)
强化这个概念。要清除标记,请从 map 对象中拔下 map 标记对象数组。
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setAllMap(null);
}
显示已加载的标记(打开)
// Shows any markers currently in the array.
function showMarkers() {
setAllMap(map);
}
在 View 中
显示带有标记的 map
<div id="map-canvas-show" style="width: 100%; height: 300px;"></div>
<小时/>
调试步骤(已添加)
我建议首先确认有效坐标已加载到您的 php 中的标记变量中。事实上,我会从使用一个标记进行测试开始。放入调试语句以显示您的纬度和经度。这是至关重要的。确认从 php 到您的 js 的有效标记数据。
然后,您需要确认这些标记位于 Google map 的可见范围内。这些标记是否存在,只是不可见?为了提供帮助,我建议您将 Google map 缩小 - 进行缩放,这样您就可以看到地球另一端的标记。
关于javascript - 使用 php sql 从数据库中获取谷歌地图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31051321/