【问题标题】:Multiple Google maps on single page单个页面上的多个 Google 地图
【发布时间】:2013-03-14 02:00:07
【问题描述】:

我想将 Google Maps API 的多个地图添加到我的 C# ASP.NET 应用程序的页面中。但是,使用我当前的代码,只显示最后一张地图。应该包含地图的所有其他 div 都是空的。正如您在我的代码中看到的那样,地图已经有了唯一的名称(不是很好,但它是唯一的)。有人可以解释我做错了什么吗? 将不胜感激。

初始化谷歌:

protected void loadGoogle()
{
    var googleScript = "function loadScript() {" +
        "var script = document.createElement(\"script\");" +
        "script.type = \"text/javascript\";" +
        "script.src = \"http://maps.google.com/maps/api/js?sensor=false& callback=initialize\"; " +
        "document.body.appendChild(script);" +
        "}" +
        "window.onload = loadScript;";
    ClientScript.RegisterStartupScript(typeof(Page), "CreateGoogleMapScript", googleScript, true);
}

创建地图(多次调用):

protected void createMap(String mapId, String address)
{
    var script = "function initialize() {" +
        "var geocoder = new google.maps.Geocoder();" +
        "var latlng = new google.maps.LatLng(-34.397, 150.644);" +
        "var mapOptions" + mapId + " = {" +
        "zoom: 16," +
        "center: latlng," +
        "mapTypeId: google.maps.MapTypeId.ROADMAP" +
        "};" +
        " var map" + mapId + " = new google.maps.Map(document.getElementById(" + mapId + "), mapOptions" + mapId + ");" +

        "var address = \"" + address + "\";" +
        "geocoder.geocode( { 'address': address}, function(results, status) {" +
        "if (status == google.maps.GeocoderStatus.OK) {" +
        "map" + mapId + ".setCenter(results[0].geometry.location);" +
        "var marker = new google.maps.Marker({" +
        "map: map" + mapId + "," +
        "position: results[0].geometry.location" +
        "});" +
        "} else {" +
        "alert(\"Geocode was not successful for the following reason: \" + status); " +
        "}" +
        "});" +
        "}";
    ClientScript.RegisterClientScriptBlock(typeof(Page), "CreateMapScript"+mapId, script, true);
}

调用者:

While 循环,literal.Text 添加一个新的 div,然后是 createMap(mapId, address);

更新:

我的新代码:

 var script = "function initialize() { var geocoder = new google.maps.Geocoder();";                  

            for (int i = 0; i < maps.Count; i++)
            {
                String currentId = maps[i] as String;
                String currentLocation = locations[i] as String;

                script += " " + 
                    "var latlng = new google.maps.LatLng(" + 500 + "," + 400 + ");" +
                    "var myOptions = {" +
                        "zoom: 16," +
                        "center: latlng," +
                        "mapTypeId: google.maps.MapTypeId.ROADMAP" +
                    "};" +                    
                    "var map = new google.maps.Map(document.getElementById(\"" + currentId + "\"), myOptions);" +

                    "var address = \"" + currentLocation + "\";" +
                        "geocoder.geocode( { 'address': address}, function(results, status) {" +
                          "if (status == google.maps.GeocoderStatus.OK) {" +
                            "map.setCenter(results[0].geometry.location);" +
                            "var marker = new google.maps.Marker({" +
                                "map: map," +
                                "position: results[0].geometry.location" +
                            "});" +
                          "} else {" +
                                "alert(\"Geocode was not successful for the following reason: \" + status); " +
                            "}" +
                         "});";
            }
            script += "};";

现在所有地图都绘制好了,但只有最后一张地图的位置正确。其他的基于 latlng 变量。为什么地理编码器不能多次使用?

【问题讨论】:

  • 首先要做的是暂时忽略 C# 方面的内容,并让解决方案在 javascript 中运行。创建一个或多个数组来保存您想要的信息并循环通过它来创建您的地图。一旦工作正常,添加 C#。
  • 我认为您在每个循环中都添加了一个新的“var latlng”,这将导致其值被重新定义。尝试在每个循环中声明一个唯一的 latlng 变量名称,例如"var latlng" + i + " = new ...."。 myOptions、map 和 addess 也可能需要相同的内容。

标签: c# javascript asp.net google-maps


【解决方案1】:

也许只是最后一个initialize 函数在浏览器中触发?尝试只使用 1 个初始化函数来初始化所有地图。

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多