Javascript 从数据库中获取标记并显示在google地图上
我有一个谷歌地图,可以很好地处理静态值,用不同的颜色标记显示不同的地方。然而,我希望使其动态化,并从数据库中获取标记的位置 数据库结构应该是这样的Javascript 从数据库中获取标记并显示在google地图上,javascript,sql,google-maps,google-maps-api-3,Javascript,Sql,Google Maps,Google Maps Api 3,我有一个谷歌地图,可以很好地处理静态值,用不同的颜色标记显示不同的地方。然而,我希望使其动态化,并从数据库中获取标记的位置 数据库结构应该是这样的 id latitude longitude status 1 -33.890542 151.274856 1 2 -33.923036 151.259052 1 3 -34.028249 151.157507 2 4 -33.80010 151.28747
id latitude longitude status
1 -33.890542 151.274856 1
2 -33.923036 151.259052 1
3 -34.028249 151.157507 2
4 -33.80010 151.28747 1
5 -33.950198 151.259302 2
我希望状态1的职位应该是一种颜色,状态2的职位应该是另一种颜色
目前我拥有的代码是
var locations = [
['<h4>Bondi Beach</h4>', -33.890542, 151.274856],
['<h4>Coogee Beach</h4>', -33.923036, 151.259052],
['<h4>Cronulla Beach</h4>', -34.028249, 151.157507],
['<h4>Manly Beach</h4>', -33.80010128657071, 151.28747820854187],
['<h4>Maroubra Beach</h4>', -33.950198, 151.259302]
];
// Setup the different icons and shadows
var iconURLPrefix = 'http://maps.google.com/mapfiles/ms/icons/';
var icons = [
iconURLPrefix + 'red-dot.png',
iconURLPrefix + 'green-dot.png',
iconURLPrefix + 'blue-dot.png',
iconURLPrefix + 'orange-dot.png',
iconURLPrefix + 'purple-dot.png',
iconURLPrefix + 'pink-dot.png',
iconURLPrefix + 'yellow-dot.png'
]
var iconsLength = icons.length;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-37.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
streetViewControl: false,
panControl: false,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_BOTTOM
}
});
var infowindow = new google.maps.InfoWindow({
maxWidth: 160
});
var markers = new Array();
var iconCounter = 0;
// Add the markers and infowindows to the map
for (var i = 0; i < locations.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: icons[iconCounter]
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
iconCounter++;
// We only have a limited number of possible icon colors, so we may have to restart the counter
if(iconCounter >= iconsLength) {
iconCounter = 0;
}
}
function autoCenter() {
// Create a new viewpoint bound
var bounds = new google.maps.LatLngBounds();
// Go through each...
for (var i = 0; i < markers.length; i++) {
bounds.extend(markers[i].position);
}
// Fit these bounds to the map
map.fitBounds(bounds);
}
autoCenter();
var位置=[
[Bondi Beach',-33.890542151.274856],
[Coogee Beach',-33.923036151.259052],
['Cronulla Beach',-34.028249151.157507],
[‘曼利海滩’,-33.80010128657071151.28747820854187],
[‘马鲁布拉海滩’,-33.950198151.259302]
];
//设置不同的图标和阴影
变量iconURLPrefix=http://maps.google.com/mapfiles/ms/icons/';
变量图标=[
iconURLPrefix+“red dot.png”,
iconURLPrefix+“green dot.png”,
iconURLPrefix+“blue dot.png”,
iconURLPrefix+“orange dot.png”,
iconURLPrefix+“purple dot.png”,
iconURLPrefix+“pink dot.png”,
iconURLPrefix+“黄点.png”
]
var iconsLength=icons.length;
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新google.maps.LatLng(-37.92151.25),
mapTypeId:google.maps.mapTypeId.ROADMAP,
mapTypeControl:false,
街景控制:错误,
泛控制:错误,
ZoomControl选项:{
位置:google.maps.ControlPosition.LEFT_底部
}
});
var infowindow=new google.maps.infowindow({
最大宽度:160
});
var markers=新数组();
var-iconCounter=0;
//将标记和信息窗口添加到地图
对于(var i=0;i=iconsLength){
i计数器=0;
}
}
函数autoCenter(){
//创建新的视点边界
var bounds=new google.maps.LatLngBounds();
//通过每个。。。
对于(var i=0;i
谁能告诉我如何根据数据库中的状态获取标记的位置,以及根据纬度和经度的状态获取标记的许多不同颜色谷歌地图是一个客户端API。标记存储在服务器端数据库中 不要把事情搞混了 使用标准服务器端技术,输出带有位置变量的JavaScript变量:
<?php
$locationsString = '[';
for ($i = 0; $i < count($locations); $i++) {
if ($i > 0) {
$locationsString .= ', ';
}
$locationsString .= '[' . $locations['title'] . ', ' .
$locations['lat'] . ', ' . $locations['lng'] . ']';
}
echo '<script>var locations="' . $locationsString . '";</script>';
?>
这一基本思想几乎有无数种变体。例如,你可以。你到底有什么问题?难道你不知道如何用数据库中的值填充
位置
-变量,或者你不知道如何根据状态创建一个标记
?因此你需要一些服务器端的东西来从数据库中提取数据(什么样的?),并将其输出到客户端,或者直接输出到javascript中,或者通过ajax响应。建议您离开并考虑数据库和服务器端代码。另见示例