我在wordpress上使用PHP & MYSQL來從數據庫中檢索包含Google Map上座標和顯示標記的數據。標記不顯示在谷歌地圖上API
Google地圖顯示,但沒有任何標記,我沒有在我的PHP代碼中發現錯誤。
代碼 <?php
/*
Template Name: MAP2
*/
get_header();
?>
<!DOCTYPE html>
<html>
<head>
<title>Custom Markers</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=*******&callback=initMap">
</script>
CSS CODE
========
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 600px;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
HTML code & javascript
=========================
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: new google.maps.LatLng(33.888630, 35.495480),
mapTypeId: 'roadmap'
});
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var icons = {
parking: {
icon: iconBase + 'parking_lot_maps.png'
},
library: {
icon: iconBase + 'library_maps.png'
},
info: {
icon: iconBase + 'info-i_maps.png'
}
};
function addMarker(feature) {
var marker = new google.maps.Marker({
position: feature.position,
icon: icons[feature.type].icon,
map: map
});
}
var features = [
PHP code
========
<?php
global $wpdb;
$prependStr ="";
foreach($wpdb->get_results("SELECT siteID, latitude, longitude FROM site_coordinates2", OBJECT) as $key => $row) {
$latitude = $row->latitude;
$longitude = $row->longitude;
$info = $row->siteID;
echo $prependStr;
?>
{
position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
type: '<?php echo $info; ?>'
}
<?php
$prependStr =",";
}
?>
];
for (var i = 0, feature; feature = features[i]; i++) {
addMarker(feature);
}
}
</script>
</body>
</html>
<?php
get_footer();
?>
可能沒有錯誤,因爲它沒有找到圖標。 'icon:圖標[feature.type] .icon,' –
@ H.Brendan所以如何解決這個錯誤? 我評論類型並從** feature.type **中刪除,但仍然沒有任何工作 –
我認爲標記不知道位置是什麼。嘗試'位置:新的google.maps.LatLng(51.727681,4.9574324),'手動 - 只是嘗試把你的座標,而不是從數據庫中。如果它工作很難編碼它的東西與數據庫標籤 –