因此,我有以下脚本来使用 google maps API,一切正常,但我需要创建一个具有多个标记(指向某物的气球形图标)的 map ,并且我需要这些标记中的每一个要指向 map 的不同区域(即不同的坐标),我该怎么做?
<script type="text/javascript">
function load() {
var map = new GMap2(document.getElementById("map"));
var marker = new GMarker(new GLatLng(<%=coordinates%>));
var html="<img src='simplemap_logo.jpg' width='20' height='20'/> " +
"<%=maptitle%><br/>" +
"<%=text%>";
map.setCenter(new GLatLng(<%=coordinates%>), <%=zoom%>)
map.setMapType(G_HYBRID_MAP);
map.addOverlay(marker);
map.addControl(new GLargeMapControl());
map.addControl(new GScaleControl());
map.addControl(new GMapTypeControl());
marker.openInfoWindowHtml(html);
}
//]]>
</script>
还有一个问题,如果我将脚本文本作为变量传递,可以这样说:
<script type="text/javascript">
<%=ScriptText%>
</script>
我的 <%=ScriptText%> 将是一个字符串,我将构建它并将其值分配给一个名为 ScriptText 的 Friend 或公共(public)变量,它是否仍能正常运行和工作? (由于我对 javascripting 的文盲,我这样做是为了使我的脚本动态且不同,这取决于我将其构建为 STRING 的方式;P)
最佳答案
obeattie 和 gregers 都是对的。通常,您需要将标记参数存储在某种数组中,稍后您将至少使用两次:
- 将叠加层添加到 map 时
- 将其添加到 GLatLngBounds 对象以计算中心点和缩放级别时
数组或标记看起来像一个对象数组,类似于:
var latlng1 = [
new GLatLng( 48.1234, -120.1234 ),
new GLatLng( 48.5678, -120.5678 ),
new GLatLng( 48.9101, -120.9112 ),
new GLatLng( 48.1121, -120.1314 ),
new GLatLng( 48.3145, -120.1516 ),
new GLatLng( 48.1617, -120.1718 ),
new GLatLng( 48.1819, -120.1920 ),
new GLatLng( 48.2021, -120.2122 )
];
添加标记的代码类似于:
// assume that map1 is an instance of a GMap2 object
// #0 -- google maps api requires us to call setCenter first before calling any other operation on the map
map1.setCenter( new GLatLng( 0, 0 ), 0 );
// #1 -- add markers
for ( var i = 0; i < latlng1.length; i++ )
{
var marker = new GMarker( latlng1[ i ] );
map1.addOverlay( marker );
}
// #2a -- calculate center
var latlngbounds = new GLatLngBounds( );
for ( var i = 0; i < latlng1.length; i++ )
{
latlngbounds.extend( latlng1[ i ] );
}
// #2b -- set center using the calculated values
map1.setCenter( latlngbounds.getCenter( ), map1.getBoundsZoomLevel( latlngbounds ) );
关于在客户端 javascript 中使用服务器端脚本的问题,是的,您可以将它们混合在一起。从您的描述来看,我认为这是您需要做的:
<script type="text/javascript">
var latlng1 = new Array( );
</script>
<script type="text/javascript">
<%
do until rs.eof
%>
latlng1.push( new GLatLng( parseFloat( '<%= rs( "Lat" ) %>' ), parseFloat( '<%= rs( "Lng" ) %>' ) ) );
<%
rs.movenext
loop
%>
</script>
我在以下位置发布了一篇文章:http://salman-w.blogspot.com/2009/03/zoom-to-fit-all-markers-polylines-or.html
关于javascript - 如何在同一张 map 上使用带有多个标记的谷歌地图 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/605786/