向谷歌地图添加多个标记

10
我想在 Google 地图 v3 API 的 此页面 右上角为每个企业列表添加标记。

我不确定如何为多个邮政编码完成此操作,但我们当前在独立企业页面中使用的一个 URLencode 来处理存储在数据库中的动态邮政编码。

以下是我们用于独立页面的代码:

<script src="http://maps.googleapis.com/maps/api/js?q=London&key=AIzaSyBaPEDyFbbnWjtvT8W3UBOM34Y7g6vK69A&sensor=false"></script>

    var map;
    function initialize() {
      var mapOptions = {
        zoom: 15,
        center: new google.maps.LatLng(51.511214,-0.119824),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var geolocate = function(address, callback) {
            $.ajax({
                    url: "http://maps.googleapis.com/maps/api/geocode/json",
                    data: {
                        "sensor": true,
                        "address": address
                    },
                    dataType: "json",
                    success: function(d) {
                        if (d.status == "ZERO_RESULTS") callback(false);
                        if (d.results && d.results[0] && d.results[0].geometry) {
                            callback({
                                "ne": d.results[0].geometry.bounds.northeast,
                                "sw": d.results[0].geometry.bounds.southwest,
                                "center": d.results[0].geometry.location
                            });
                        }
                        else callback(false);
                    }
                });
      };
      map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);
      geolocate("<%=server.URLEncode(""&rsAdvert("ContactPostcode"))%>", function(c) {
            map.setCenter(new google.maps.LatLng(c.center.lat, c.center.lng));
     });
    }
    google.maps.event.addDomListener(window, 'load', initialize);

$('#myModal').on('shown', function () {
  google.maps.event.trigger(map, 'resize');
})

该页面的邮政编码是由ASP生成的:

    if rsDB_Ads("ContactPostcode") <> "" then
        strTempHTML = "[ContactPostcode]"
        strDB_AdvertItem = Replace(strDB_AdvertItem, "<!--ContactPostcode-->", strTempHTML)
    Else
        strDB_AdvertItem = Replace(strDB_AdvertItem, "<!--ContactPostcode-->", "")
    End if
希望能有人能够帮忙..
2个回答

20

首先,以下是如何管理多个标记的方法。(将代码复制并粘贴到html文件中即可运行…)然后您可以通过经典asp编写从数据库等位置输出的代码进行调整:

    <!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>
    </head>
    <body>
      <div id="map" style="width: 1000px; height: 1000px;"></div>

      <script type="text/javascript">
        var locations = [
          ['Stadtbibliothek Zanklhof', 47.06976, 15.43154, 1],
          ['Stadtbibliothek dieMediathek', 47.06975, 15.43116, 2],
          ['Stadtbibliothek Gösting', 47.09399, 15.40548, 3],
          ['Stadtbibliothek Graz West', 47.06993, 15.40727, 4],
          ['Stadtbibliothek Graz Ost', 47.06934, 15.45888, 5],
          ['Stadtbibliothek Graz Süd', 47.04572, 15.43234, 6],
          ['Stadtbibliothek Graz Nord', 47.08350, 15.43212, 7],
          ['Stadtbibliothek Andritz', 47.10280, 15.42137, 8]
        ];

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 13,
          center: new google.maps.LatLng(47.071876, 15.441456),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker, i;

        for (i = 0; i < locations.length; i++) {
          marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
          });

          google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
              infowindow.setContent(locations[i][0]);
              infowindow.open(map, marker);
            }
          })(marker, i));
        }
      </script>
    </body>
    </html>

1
我想补充一点,如果你恰好需要从 Django 传递一个变量列表到 HTML,你可以使用以下代码将列表作为可读取的值传递:var locations = {{locationlist|safe}}; "|safe" 部分是关键,它允许 JavaScript 解析该值。 - marty331

0

我在这里看到我的代码!

我猜你想在地图上添加标记。这可以通过调整我之前提供的代码来轻松完成。简单来说:

     geolocate("Your postcode here", function(c) {
         var marker = new google.maps.Marker({
            position: new google.maps.LatLng(c.center.lat, c.center.lng),
            map: map,
            // Your other google maps marker options here
         });
     });

只需使用ASP打印此代码,根据需要重复多次,确保mapgeolocate都在范围内。每次,它将简单地添加您的标记!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接