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