繁体   English   中英

将服务器端纬度/经度值添加到 Google Map javascript

[英]Add server-side latitude/longitude values to Google Map javascript

我正在尝试将 Google map 添加到我的一个页面。

在我的代码隐藏中,我有一个 map 的中心点,以及我想要 map 的纬度/经度数组:

protected void Page_Load(object sender, EventArgs e)
{
    Point center = new Point { latitude = 28.42693F, longitude = -81.4673F };

    List<Point> points = new List<Point>();
    points.Add(new Point { latitude = 28.43039F, longitude = -81.47186F });
    points.Add(new Point { latitude = 28.36906F, longitude = -81.56063F });

    Point[] pointArray = points.ToArray();
}


public class Point
{
    public float latitude;
    public float longitude;
}

在我的页面上,我有这个 javascript:

<script type="text/javascript">

    function initialize() {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(28.42693, -81.4673), 13);
            //map.setUIToDefault();

            var blueIcon = new GIcon(G_DEFAULT_ICON);
            blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
            markerOptions = { icon: blueIcon };


            var point = new GLatLng(28.43039, -81.47186);
            map.addOverlay(new GMarker(point, markerOptions));

            point = new GLatLng(28.36906, -81.56063);
            map.addOverlay(new GMarker(point, markerOptions));
        }
    }

</script>

这些值现在被硬编码到 javascript 中进行测试,但我需要从代码隐藏中获取动态值。 我怎样才能做到这一点?

一种快速而肮脏的方法可能是在您的代码隐藏中创建一个字符串,该字符串创建一个 JavaScript 纬度/经度值数组。 然后,您可以在 your.ASPX 中添加一个并将其设置为您的字符串值。 或者创建一个 JSON 表示您的点。 它适用于小型一次性场景。 所以你的 JavaScript 最终可能看起来像这样:

<script type="text/javascript">

    function initialize() {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            <asp:Literal id="litMapCenter" runat="server"/>
            //map.setUIToDefault();

            var blueIcon = new GIcon(G_DEFAULT_ICON);
            blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
            markerOptions = { icon: blueIcon };

            <asp:Literal id="litMapPoints" runat="server"/>
        }
    }

</script>

然后在您的代码隐藏中使用适当的 JavaScript 设置 litMapPoints 和 litMapCenter。

您可以在后面的代码中使用类似的东西

ClientScript.RegisterStartupScript(this.getType(), "whateveryourkeyis", string.Format("longitude={0};", pointArray[0].longitude), true);

这样,您只需创建一个名为“longitude”的 jscript 变量,并使用您的 .NET 代码值对其进行初始化。

(这是即时编写的,如果其中有错误,请原谅我:-))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM