Javascript 设置边界以显示自定义标记
我使用下面的代码显示mySQL数据库中给定位置内的标记。我正在尝试设置LatLng界限,以便可以看到每个给定位置的所有标记,但它不起作用,即地图未显示可视地图中的所有标记,并且我还收到一个错误,表示“findid未定义”。作为一个新手,我不确定是代码还是这段代码的位置错了 我只是想知道是否有人可以看看这个,让我知道我错在哪里Javascript 设置边界以显示自定义标记,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我使用下面的代码显示mySQL数据库中给定位置内的标记。我正在尝试设置LatLng界限,以便可以看到每个给定位置的所有标记,但它不起作用,即地图未显示可视地图中的所有标记,并且我还收到一个错误,表示“findid未定义”。作为一个新手,我不确定是代码还是这段代码的位置错了 我只是想知道是否有人可以看看这个,让我知道我错在哪里 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>All Locations</title>
<link rel="stylesheet" href="css/alllocationsstyle.css" type="text/css" media="all" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
<script type="text/javascript">
var customIcons = {
Artefact: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
Coin: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
Jewellery: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(54.312195845815246,-4.45948481875007),
zoom:14,
mapTypeId: 'satellite'
});
// Change this depending on the name of your PHP file
downloadUrl("phpfile.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var findid = markers[i].getAttribute("findid");
var locationid = markers[i].getAttribute("locationid");
var dateoftrip = markers[i].getAttribute("dateoftrip");
var findcategory = markers[i].getAttribute("findcategory");
var findname = markers[i].getAttribute("findname");
var finddescription = markers[i].getAttribute("finddescription");
var detectorname = markers[i].getAttribute("detectorname");
var searchheadname = markers[i].getAttribute("searchheadname");
var detectorsettings = markers[i].getAttribute("detectorsettings");
var pasref = markers[i].getAttribute("pasref");
var findimage= markers[i].getAttribute("findimage");
var additionalcomments= markers[i].getAttribute("additionalcomments");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("findosgb36lat")),
parseFloat(markers[i].getAttribute("findosgb36lon")));
var icon = customIcons[findcategory] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
title: 'Click to view details',
icon: icon.icon,
shadow: icon.shadow
});
}
});
}
// Creating a LatLngBounds object
var bounds = new google.maps.LatLngBounds();
// Looping through the places array
for (var i = 0; i < findid.length; i += 1) {
// Extending the bounds object with each LatLng
bounds.extend(findid[i]);
}
// Adjusting the map to new bounding box
map.fitBounds(bounds)
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"></div>
</body>
</html>
所有地点
var customIcons={
人工制品:{
图标:'http://labs.google.com/ridefinder/images/mm_20_red.png',
影子:'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
硬币:{
图标:'http://labs.google.com/ridefinder/images/mm_20_green.png',
影子:'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
珠宝:{
图标:'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
影子:'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
函数加载(){
var map=new google.maps.map(document.getElementById(“map”),{
中心:新google.maps.LatLng(54.312195845845815246,-4.45948481875007),
缩放:14,
mapTypeId:'卫星'
});
//根据PHP文件的名称更改此选项
下载URL(“phpfile.php”,函数(数据){
var xml=data.responseXML;
var markers=xml.documentElement.getElementsByTagName(“标记”);
对于(var i=0;i
更新代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>All Locations</title>
<link rel="stylesheet" href="css/alllocationsstyle.css" type="text/css" media="all" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
<script type="text/javascript">
var customIcons = {
Artefact: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
Coin: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
Jewellery: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
var markers;
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(54.312195845815246,-4.45948481875007),
zoom:14,
mapTypeId: 'satellite'
});
// Change this depending on the name of your PHP file
downloadUrl("phpfile.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var findid = markers[i].getAttribute("findid");
var locationid = markers[i].getAttribute("locationid");
var dateoftrip = markers[i].getAttribute("dateoftrip");
var findcategory = markers[i].getAttribute("findcategory");
var findname = markers[i].getAttribute("findname");
var finddescription = markers[i].getAttribute("finddescription");
var detectorname = markers[i].getAttribute("detectorname");
var searchheadname = markers[i].getAttribute("searchheadname");
var detectorsettings = markers[i].getAttribute("detectorsettings");
var pasref = markers[i].getAttribute("pasref");
var findimage= markers[i].getAttribute("findimage");
var additionalcomments= markers[i].getAttribute("additionalcomments");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("findosgb36lat")),
parseFloat(markers[i].getAttribute("findosgb36lon")));
var icon = customIcons[findcategory] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
title: 'Click to view details',
icon: icon.icon,
shadow: icon.shadow
});
}
});
// Creating a LatLngBounds object
var bounds = new google.maps.LatLngBounds();
// Looping through the places array
for (var i = 0; i < markers.length; i += 1) {
// Extending the bounds object with each LatLng
bounds.extend(markers[i]);
}
// Adjusting the map to new bounding box
map.fitBounds(bounds)
}
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"></div>
</body>
</html>
所有地点
var customIcons={
人工制品:{
图标:'http://labs.google.com/ridefinder/images/mm_20_red.png',
影子:'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
硬币:{
图标:'http://labs.google.com/ridefinder/images/mm_20_green.png',
影子:'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
珠宝:{
图标:'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
影子:'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
var标记;
函数加载(){
var map=new google.maps.map(document.getElementById(“map”),{
中心:新google.maps.LatLng(54.312195845845815246,-4.45948481875007),
缩放:14,
mapTypeId:'卫星'
});
//根据PHP文件的名称更改此选项
下载URL(“phpfile.php”,函数(数据){
var xml=data.responseXML;
var markers=xml.documentElement.getElementsByTagName(“标记”);
对于(var i=0;ifunction load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(54.312195845815246,-4.45948481875007),
zoom:14,
mapTypeId: 'satellite'
});
var bounds = new google.maps.LatLngBounds();
var markers = [];
downloadUrl("phpfile.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
// loop through the markers in the xml file
for (var i = 0; i < markers.length; i++) {
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("findosgb36lat")),
parseFloat(markers[i].getAttribute("findosgb36lon"))
);
var icon = customIcons[findcategory] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
title: 'Click to view details',
icon: icon.icon,
shadow: icon.shadow
});
// LatLngBounds expects a LatLng object - not a marker object
bounds.extend(point);
}
// fit the map after all the points and markers have been created - i.e. after the loop
map.fitBounds(bounds);
});
}