将 Google 地图集中在数据库中的点上?
我正在尝试将谷歌地图集中在从数据库中提取的单个标记上,到目前为止我所做的是使用此函数从数据库中获取单个标记
function getMarkers(){
var urlstr="readsingle.php?v=<?php echo $cam ?>";
var request = GXmlHttp.create();
request.open('GET', urlstr , true); // request XML from PHP with AJAX call
request.onreadystatechange = function () {
if (request.readyState == 4) { // 4 is a completed request
var xmlDoc = request.responseXML;
locations = xmlDoc.documentElement.getElementsByTagName("location");
markers = [];
if (locations.length){
for (var i = 0; i < locations.length; i++) { // cycle thru locations
markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")));
markers[i].infowindow = "This is "+locations[i].getAttribute("name");
markers[i].markerindex = i;
markers[i].db_id = locations[i].getAttribute("location_id");
map.addOverlay(markers[i]);
}
}
}
}
request.send(null);
}
并使用此函数加载地图
function onLoad() {
map = new GMap(document.getElementById("div_map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(53.76221, -2.71227), 8);
map.enableScrollWheelZoom();
getMarkers();
GEvent.addListener(map, "click", function(overlay, point) {
if (overlay){ // marker clicked
overlay.openInfoWindowHtml(overlay.infowindow); // open InfoWindow
} else if (point) { // background clicked
}
});
}
如何更改纬度此行上的 Lng 值
map.setCenter(new GLatLng(53.76, -2.71), 5);
是否与单个标记的 lat lng 值相匹配?或者是否有更简单的方法从数据库中提取单个标记并将地图置于其中心?
I'm trying to center a google map on a single marker pulled from a database, what I'm doing so far is using this function to get a single marker from the database
function getMarkers(){
var urlstr="readsingle.php?v=<?php echo $cam ?>";
var request = GXmlHttp.create();
request.open('GET', urlstr , true); // request XML from PHP with AJAX call
request.onreadystatechange = function () {
if (request.readyState == 4) { // 4 is a completed request
var xmlDoc = request.responseXML;
locations = xmlDoc.documentElement.getElementsByTagName("location");
markers = [];
if (locations.length){
for (var i = 0; i < locations.length; i++) { // cycle thru locations
markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")));
markers[i].infowindow = "This is "+locations[i].getAttribute("name");
markers[i].markerindex = i;
markers[i].db_id = locations[i].getAttribute("location_id");
map.addOverlay(markers[i]);
}
}
}
}
request.send(null);
}
and this function to load the map
function onLoad() {
map = new GMap(document.getElementById("div_map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(53.76221, -2.71227), 8);
map.enableScrollWheelZoom();
getMarkers();
GEvent.addListener(map, "click", function(overlay, point) {
if (overlay){ // marker clicked
overlay.openInfoWindowHtml(overlay.infowindow); // open InfoWindow
} else if (point) { // background clicked
}
});
}
How can I change the Lat Lng values on this line
map.setCenter(new GLatLng(53.76, -2.71), 5);
to match the lat lng values from the single marker? or is there a simpler way to pull a single marker from a database and center the map on it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
getMarkers()
仅检索一个标记,您似乎可以简单地添加:在将标记添加到地图之后或之前:
If
getMarkers()
is retrieving just one marker, it looks like you can simply add:After or before you add the marker to the map: