gpt4 book ai didi

javascript - 标记管理器不会显示标记

转载 作者:行者123 更新时间:2023-11-28 00:02:07 26 4
gpt4 key购买 nike

我正在尝试制作一个带有 Google map 窗口的简单网页。在这个窗口中,我有一个站点位置标记(可以通过填写新的纬度和经度并单击“检查站点”按钮来更改)。按下时,它会将站点标记和中心移动到站点。除此之外,我想用标记管理器加载范围标记。我已经预定义了一些要测试的标记。稍后,这将是一个巨大的标记列表(NOAA 站点 FYI)。我希望这些站点标记中的每一个都可以单击,并在单击时选中相应的复选框。我尝试实现这个,但似乎我的标记没有显示。我搜索了又搜索,但似乎无法找出它们不显示的原因。我的网页代码如下。

附带问题:将数千 (10k+) 个这些标记(分布在世界各地)加载到应用程序中的合适方法是什么。标记管理器仅通过显示边界内的标记真的那么有效,还是仍然有限制。我还可以想象加载数千个标记需要加载的数据相当多。也请您在这里输入。谢谢!

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBPtpV_saW7qtACfPVYBWIB-Jw253iBca4&sensor=false"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/src/markermanager.js"></script>
<script type="text/javascript">
var stations =[
{ "name": "Oostende", "posn": [51.233, 2.917], "code":"(64080,99999)" },
{ "name": "Gent", "posn": [51.183, 3.817], "code":"(64310,99999)" },
{ "name": "Koksijde", "posn": [51.083, 2.65], "code":"(64000,99999)" },
{ "name": "Wevelgem", "posn": [80.817, 3.2], "code":"(64160,99999)" },
{ "name": "Melle", "posn": [50.983, 3.983], "code":"(64340,99999)" },
{ "name": "Uccle", "posn": [50.8, 4.35], "code":"(64470,99999)" },
{ "name": "Antwerpen", "posn": [51.2, 4.467], "code":"(64500,99999)" },
{ "name": "Retie", "posn": [51.217, 5.033], "code":"(64640,99999)" },
{ "name": "Goetsenhoven", "posn": [50.783, 4.95], "code":"(64630,99999)" },
{ "name": "Schaffen", "posn": [51, 5.067], "code":"(64650,99999)" },
{ "name": "Diepenbeek", "posn": [50.917, 5.45], "code":"(64770,99999)" },
{ "name": "Chievres", "posn": [50.567, 3.833], "code":"(64320,99999)" },
{ "name": "Bierset", "posn": [50.65, 5.45], "code":"(64780,99999)" },
{ "name": "Kleine Brogel", "posn": [51.167, 5.467], "code":"(64790,99999)" },
{ "name": "Ernage", "posn": [50.567, 4.683], "code":"(64590,99999)" }
];
var red_marker = "http://maps.google.com/mapfiles/ms/icons/red-dot.png";
var green_marker = "http://maps.google.com/mapfiles/ms/icons/green-dot.png";
var map;
var mgr;
var site_marker;
var markers;
function setupMap() {
var latlng = new google.maps.LatLng(50.85, 4.35);
var mapElem = document.getElementById("map-canvas-9");
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(mapElem, mapOptions);
site_marker = new google.maps.Marker({
position: latlng,
map: map,
title: " latitude N, longitude ",
icon: red_marker
});
var listener = google.maps.event.addListener(map, 'bounds_changed', function(){
setupNoaaMarkers();
google.maps.event.removeListener(listener);
});
}
function setupNoaaMarkers() {
mgr = new MarkerManager(map);
google.maps.event.addListener(mgr, 'loaded', function(){
mgr.addMarkers(getNoaaMarkers(),8);
mgr.refresh();
});
}
function getNoaaMarkers() {
var markers=[];
for (var place in stations) {
var title = place.name;
var posn = new google.maps.LatLng(place.posn[0], place.posn[1]);
var code = place.code;
var marker = createMarker(posn, title, code);
google.maps.event.addListener(marker, 'click', function() {
if (document.getElementById(code).checked)
{
document.getElementById(code).checked=false;
}
else
{
document.getElementById(code).checked=true;
}
});
markers.push(marker);
}
return markers;
}
function changeSitePosition() {
var lat = document.getElementById("id_latitude").value;
var lon = document.getElementById("id_longitude").value;
if (lat === 0 & lon===0)
{
lat = 50.85;
lon = 4.35;
}
var latlng = new google.maps.LatLng(lat, lon);
site_marker.setPosition(latlng);
map.setCenter(latlng);
}
</script>
</head>
<body onload="setupMap()">
<div id="map-canvas-9" style="width: 800px; height: 300px;" class="easy-map-googlemap">
</div>
<table>
<tr>
<td>Latitude</td>
<td><input id="id_latitude" max="90" min="-90" name="latitude" step="0.0001" type="number" /></td>
<td></td>
<td>In decimal degree, within [-90,90], max. 4 decimals, e.g. : 45.5 </td>
</tr>
<tr>
<td>Longitude</td>
<td><input id="id_longitude" max="180" min="-180" name="longitude" step="0.0001" type="number" /></td>
<td></td>
<td>In decimal degree, within [0,360], max. 4 decimals, e.g. : 12.3 </td>
</tr>
<tr>
<td><button onclick="changeSitePosition()">Check site</button></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64080, 99999)" /> Oostende</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64310, 99999)" /> Gent</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64000, 99999)" /> Koksijde</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64160, 99999)" /> Wevelgem</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64340, 99999)" /> Melle</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64470, 99999)" /> Uccle</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64500, 99999)" /> Antwerpen</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64640, 99999)" /> Retie</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64630, 99999)" /> Goetsenhoven</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64650, 99999)" /> Schaffen</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64770, 99999)" /> Diepenbeek</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64320, 99999)" /> Chievres</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64780, 99999)" /> Bierset</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64790, 99999)" /> Kleine Brogel</label></td>
</tr>
<tr>
<td><label for="check"><input name="stations" type="checkbox" id="(64590, 99999)" /> Ernage</label></td>
</tr>
</table>
</body>
</html>

最佳答案

使用调试器。

  1. 对于place(0)的第一个值place.posn(0.posn)未定义;应该是 stations[place].posn
  2. 您的代码中没有 createMarker 函数。
  3. HTML id's may not contain spaces

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    html { height: 100% }
    body { height: 100%; margin: 0; padding: 0 }
    #map-canvas { height: 100% }
    </style>
    <script type="text/javascript"
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBPtpV_saW7qtACfPVYBWIB-Jw253iBca4&sensor=false"></script>
    <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/src/markermanager.js"></script>
    <script type="text/javascript">
    var stations =[
    { "name": "Oostende", "posn": [51.233, 2.917], "code":"(64080,99999)" },
    { "name": "Gent", "posn": [51.183, 3.817], "code":"(64310,99999)" },
    { "name": "Koksijde", "posn": [51.083, 2.65], "code":"(64000,99999)" },
    { "name": "Wevelgem", "posn": [80.817, 3.2], "code":"(64160,99999)" },
    { "name": "Melle", "posn": [50.983, 3.983], "code":"(64340,99999)" },
    { "name": "Uccle", "posn": [50.8, 4.35], "code":"(64470,99999)" },
    { "name": "Antwerpen", "posn": [51.2, 4.467], "code":"(64500,99999)" },
    { "name": "Retie", "posn": [51.217, 5.033], "code":"(64640,99999)" },
    { "name": "Goetsenhoven", "posn": [50.783, 4.95], "code":"(64630,99999)" },
    { "name": "Schaffen", "posn": [51, 5.067], "code":"(64650,99999)" },
    { "name": "Diepenbeek", "posn": [50.917, 5.45], "code":"(64770,99999)" },
    { "name": "Chievres", "posn": [50.567, 3.833], "code":"(64320,99999)" },
    { "name": "Bierset", "posn": [50.65, 5.45], "code":"(64780,99999)" },
    { "name": "Kleine Brogel", "posn": [51.167, 5.467], "code":"(64790,99999)" },
    { "name": "Ernage", "posn": [50.567, 4.683], "code":"(64590,99999)" }
    ];
    var red_marker = "http://maps.google.com/mapfiles/ms/icons/red-dot.png";
    var green_marker = "http://maps.google.com/mapfiles/ms/icons/green-dot.png";
    var map;
    var mgr;
    var site_marker;
    var markers;
    var infowindow = new google.maps.InfoWindow({});
    function setupMap() {
    var latlng = new google.maps.LatLng(50.85, 4.35);
    var mapElem = document.getElementById("map-canvas-9");
    var mapOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(mapElem, mapOptions);
    site_marker = new google.maps.Marker({
    position: latlng,
    map: map,
    title: " latitude N, longitude ",
    icon: red_marker
    });
    var listener = google.maps.event.addListener(map, 'bounds_changed', function(){
    setupNoaaMarkers();
    google.maps.event.removeListener(listener);
    });
    }
    function setupNoaaMarkers() {
    mgr = new MarkerManager(map);
    google.maps.event.addListener(mgr, 'loaded', function(){
    mgr.addMarkers(getNoaaMarkers(),8);
    mgr.refresh();
    });
    }
    function getNoaaMarkers() {
    var markers=[];
    for (var place in stations) {
    var title = place.name;
    var posn = new google.maps.LatLng(stations[place].posn[0], stations[place].posn[1]);
    var code = stations[place].code;
    var marker = createMarker(posn, title, code);
    }
    return markers;
    }
    function changeSitePosition() {
    var lat = document.getElementById("id_latitude").value;
    var lon = document.getElementById("id_longitude").value;
    if (lat === 0 & lon===0)
    {
    lat = 50.85;
    lon = 4.35;
    }
    var latlng = new google.maps.LatLng(lat, lon);
    site_marker.setPosition(latlng);
    map.setCenter(latlng);
    }
    function createMarker(latlng,title,code) {
    var marker = new google.maps.Marker({
    map: map,
    title: title,
    position: latlng
    });

    google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(code);
    infowindow.open(map, this);
    });
    google.maps.event.addListener(marker, 'click', function() {
    if (document.getElementById(code).checked) {
    document.getElementById(code).checked=false;
    } else {
    document.getElementById(code).checked=true;
    }
    });


    return marker;
    }

    </script>
    </head>
    <body onload="setupMap()">
    <div id="map-canvas-9" style="width: 800px; height: 300px;" class="easy-map-googlemap">
    </div>
    <table>
    <tr>
    <td>Latitude</td>
    <td><input id="id_latitude" max="90" min="-90" name="latitude" step="0.0001" type="number" /></td>
    <td></td>
    <td>In decimal degree, within [-90,90], max. 4 decimals, e.g. : 45.5 </td>
    </tr>
    <tr>
    <td>Longitude</td>
    <td><input id="id_longitude" max="180" min="-180" name="longitude" step="0.0001" type="number" /></td>
    <td></td>
    <td>In decimal degree, within [0,360], max. 4 decimals, e.g. : 12.3 </td>
    </tr>
    <tr>
    <td><button onclick="changeSitePosition()">Check site</button></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64080,99999)" /> Oostende</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64310,99999)" /> Gent</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64000,99999)" /> Koksijde</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64160,99999)" /> Wevelgem</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64340,99999)" /> Melle</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64470,99999)" /> Uccle</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64500,99999)" /> Antwerpen</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64640,99999)" /> Retie</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64630,99999)" /> Goetsenhoven</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64650,99999)" /> Schaffen</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64770,99999)" /> Diepenbeek</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64320,99999)" /> Chievres</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64780,99999)" /> Bierset</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64790,99999)" /> Kleine Brogel</label></td>
    </tr>
    <tr>
    <td><label for="check"><input name="stations" type="checkbox" id="(64590,99999)" /> Ernage</label></td>
    </tr>
    </table>
    </body>
    </html>

working example

关于javascript - 标记管理器不会显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20783939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com