将数据库中的多个纬度/经度点加载到谷歌地图标记中的好方法?

2024-02-22

我有一个包含多个地址的表,包括它们的纬度/经度坐标,并且我想使用 asp.net webforms 和 Google Maps Javascript API V3 将其中许多标记一次性放置到 google 地图上。

这些教程展示了如何添加一个标记:
http://code.google.com/apis/maps/documentation/javascript/overlays.html#Markers http://code.google.com/apis/maps/documentation/javascript/overlays.html#Markers

 var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  var marker = new google.maps.Marker({
      position: myLatlng, 
      map: map, 
      title:"Hello World!"
  });

我的问题是,在我加载了服务器端的多个地址集(代码隐藏)之后,什么是good将此集合输出到 html 中的方法,以便客户端 JavaScript 可以迭代该集合并将标记放置到地图上。

Update

If I already创建了我的集合,在渲染时将其推送到页面的 html 中而不调用外部脚本的好方法是什么? (将复杂的过滤器参数传递给脚本会很复杂,所以我宁愿避免这种情况。)我是否应该只使用 stringbuilder 来构造一个包含正确 json 数组的 javascript 函数,然后将该函数附加到页面?这可行,但似乎不太合适。


您可以采用您建议的方法。

这是一个非常简单的示例,展示了使用 v3 API 绘制多个标记是多么容易:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>

  <script type="text/javascript">

    var map;

    // Cretes the map
    function initialize() {
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(-33.92, 151.25),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
    }

    // This function takes an array argument containing a list of marker data
    function generateMarkers(locations) {
      for (var i = 0; i < locations.length; i++) {  
        new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map,
          title: locations[i][0]
        });
      }
    }
  </script>

</head> 
<body> 
  <div id="map" style="width: 500px; height: 400px;"></div>
</body>
</html>

然后要生成标记,您可以将以下脚本转储到您的文件中的任何位置<body> tags.

<body> 
  <div id="map" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
    window.onload = function () {
      initialize();
      generateMarkers(
        ['Bondi Beach', -33.890542, 151.274856],
        ['Coogee Beach', -33.923036, 151.259052],
        ['Cronulla Beach', -34.028249, 151.157507],
        ['Manly Beach', -33.800101, 151.287478],
        ['Maroubra Beach', -33.950198, 151.259302]
      );
    };
  </script>
</body>

您只需要生成数组文字['Bondi Beach', -33.890542, 151.274856] ...来自服务器端数据集,因为其余部分是静态的。确保最后一个元素不以逗号结尾。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将数据库中的多个纬度/经度点加载到谷歌地图标记中的好方法? 的相关文章

随机推荐