如何在同一张地图上使用带有多个标记的谷歌地图 API

发布于 2024-07-14 20:38:00 字数 1184 浏览 9 评论 0原文

所以,我有以下脚本来使用谷歌地图 API,一切都很好,但我需要创建一个具有多个标记(指向某物的气球形图标)的地图,并且我需要每个标记都指向地图的不同区域(即不同的坐标),我该怎么做?

<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 变量,它仍然可以正常运行和工作吗? (我这样做是为了使我的脚本动态化,并且根据我将其构建为字符串的方式而有所不同,因为我对 JavaScript 不识字;P)

So, i have the following script to use the google maps API, its all fine, but i need to create a map that has more than one Marker (the balloon shaped icon pointing to something) and i need each of those markers to point on a different area of the map (i.e. different coordinates), how can i do it?

<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>

One more question, if i pass the Script text as a variable, lets say something like:

<script type="text/javascript">
<%=ScriptText%>
</script>

and my <%=ScriptText%> will be a string which i will build and assign its value to a Friend or Public variable called ScriptText, will it still run and work properly? (i am doing this to make my script dynamic and different based on how i build it as a STRING, due to my illiteracy in javascripting ;P)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧城空念 2024-07-21 20:38:00

奥贝蒂和格雷格斯都是对的。 一般来说,您需要将标记参数存储在某种数组中,稍后您将至少使用该数组两次:

  1. 将叠加层添加到地图
  2. 时 将其添加到 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

obeattie and gregers are both right. In general, you need to store the marker parameters in some kind of array which you will later use at-least twice:

  1. when adding the overlay to the map
  2. when adding it to a GLatLngBounds object to calculate the center point and zoom level

The array or markers would look like an array of objects, something like:

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 )
];

The code for adding markers would look something similar to:

  // 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 ) );

As for your question about using server side script inside client side javascript, yes you can mix them together. Judging by your description, i think this is what you need to do:

<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>

I've posted an article at: http://salman-w.blogspot.com/2009/03/zoom-to-fit-all-markers-polylines-or.html

等风来 2024-07-21 20:38:00

您需要创建一个新的 GMarker< /a> 用于您要在地图上标记的每个地点。 此处提供了针对 GMarker< 的非常好的文档/代码>s。

要创建 GMarker,您将从文档中看到,首先需要创建一个 GLatLng 来表示要放置标记的位置。 您没有提到想要在气球中包含任何内容,所以我假设这只是您想要的标记。 在您的情况下,代码可能看起来像这样:

var markerCoords = [
    new GLatLng(<latitude>, <longitude>),
    new GLatLng(<latitude>, <longitude>),
    [...]
];

for (coord in markerCoords){
    map.addOverlay(new GMarker(coord));
};

我相信您可能可以确切地知道这里发生了什么,但为了以防万一,我创建了一个 GLatLng 对象数组(这可以是任何长度 [在边界内哈哈]),然后迭代它,为数组中定义的每个 GLatLng 添加标记到地图。

如果您计划创建大量标记,您可能会发现使用 标记管理器,这将加快同时渲染大量标记的速度(如果屏幕的一个区域中有很多标记,则不渲染屏幕外标记并压缩屏幕上的标记)。

You'll need to create a new GMarker for each place you want to mark on the map. There is quite good documentation available here for GMarkers.

To create a GMarker, you'll see from the documentation that you first need to create a GLatLng representing the location you want to drop the marker. You haven't mentioned wanting any content in a balloon, so I'm assuming it's just the marker you're after. In your case, the code would probably look something like this:

var markerCoords = [
    new GLatLng(<latitude>, <longitude>),
    new GLatLng(<latitude>, <longitude>),
    [...]
];

for (coord in markerCoords){
    map.addOverlay(new GMarker(coord));
};

I'm sure you can probably tell exactly what's going on here, but just in case, I create an array of GLatLng objects (this can be of any length [within bounds haha]), and then iterate over it, adding markers to the map for each GLatLng defined in the array.

If you are planning on creating a lot of markers, you'll probably find it useful to use the Marker Manager, which will speed up rendering a lot of markers simultaneously (by not rendering off-screen markers and condensing on-screen ones if there are a lot in one region of the screen).

黯然#的苍凉 2024-07-21 20:38:00

奥比蒂很好地回答了你的问题。 但从您的示例代码来看,您似乎还希望地图缩放到包含标记的区域。 要使用多个标记执行此操作,您可以创建 GLatLngBounds您为每个标记扩展的内容。 然后,您可以从边界对象中获取适合您的标记集合的中心和缩放。

<代码>
varmarkersBounds = GLatLngBounds();
var 坐标 = null;
for (markerCoords 中的坐标){
坐标 = 新 GMarker(坐标);
地图.addOverlay();
markersBounds.extend(坐标);
};

var markersCenter = markersBounds.getCenter();
var markersZoom = G_HYBRID_MAP.getBoundsZoomLevel(markersBounds);

map.setCenter(markersCenter, markersZoom);

我对 G_HYBRID_MAP 不是 100% 确定。getBoundsZoomLevel,但如果我没记错的话,G_HYBRID_MAP 是 GMapType

obeattie answered your question pretty good. But from your example code, it seems like you also want the map to zoom to the area containing the markers. To do this with multiple markers, you can create a GLatLngBounds that you extend for each marker. You can then get the center and zoom that will fit for your collection of markers from the bounds object.


var markersBounds = GLatLngBounds();
var coord = null;
for (coord in markerCoords){
coord = new GMarker(coord);
map.addOverlay();
markersBounds.extend(coord);
};

var markersCenter = markersBounds.getCenter();
var markersZoom = G_HYBRID_MAP.getBoundsZoomLevel(markersBounds);

map.setCenter(markersCenter, markersZoom);

I'm not 100% sure about the G_HYBRID_MAP.getBoundsZoomLevel, but if I remember correctly, G_HYBRID_MAP is an instance of GMapType.

冰葑 2024-07-21 20:38:00

我有类似的东西,但事件不起作用。 我可以在对象的构造函数中添加侦听器吗?

//Localization object onstructor
function Localization(width, height, description, img_source){
    this.point = new GLatLng(width, height);
    this.marker = new GMarker(this.point);
    this.description = description;
    this.img_source = img_source;
    GEvent.addListener(this.marker, "click", function(){marker.openInfoWindowHtml(this.description);});
}

//map localizations to show on map
var localizations = [
    new Localization( 52.9015797, 15.2602200, 'Poznań1', 'eye1' ),
    new Localization( 52.9025797, 15.1602200, 'Poznań2', 'eye2' ),
    new Localization( 52.9035797, 15.2702200, 'Poznań3', 'eye3' ),
    new Localization( 52.9045797, 15.2102200, 'Poznań4', 'eye4' ),
]

var map = new GMap2(document.getElementById("mapa"));
map.setCenter(localizations[3].point, 13);
map.setUIToDefault();


for(i=0; i < localizations.length; i++){
    localization=localizations[i];
    map.addOverlay(localization.marker);
    localization.marker.openInfoWindowHtml(localization.description);
}

I have something like this but events doesn't work. Can I add listeners inside an constructor of object?

//Localization object onstructor
function Localization(width, height, description, img_source){
    this.point = new GLatLng(width, height);
    this.marker = new GMarker(this.point);
    this.description = description;
    this.img_source = img_source;
    GEvent.addListener(this.marker, "click", function(){marker.openInfoWindowHtml(this.description);});
}

//map localizations to show on map
var localizations = [
    new Localization( 52.9015797, 15.2602200, 'Poznań1', 'eye1' ),
    new Localization( 52.9025797, 15.1602200, 'Poznań2', 'eye2' ),
    new Localization( 52.9035797, 15.2702200, 'Poznań3', 'eye3' ),
    new Localization( 52.9045797, 15.2102200, 'Poznań4', 'eye4' ),
]

var map = new GMap2(document.getElementById("mapa"));
map.setCenter(localizations[3].point, 13);
map.setUIToDefault();


for(i=0; i < localizations.length; i++){
    localization=localizations[i];
    map.addOverlay(localization.marker);
    localization.marker.openInfoWindowHtml(localization.description);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文