GoogleMaps简单示例

GoogleMaps.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Google
{
    [DefaultProperty("AppKey")]
    [ToolboxData("<{0}:GoogleMaps runat=server></{0}:GoogleMaps>")]
    public class GoogleMaps : WebControl
    {
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        public string AppKey
        {
            get
            {
                if (ViewState["AppKey"] != null)
                    return (string)ViewState["AppKey"];
                return string.Empty;
            }
            set
            {
                ViewState["AppKey"] = value;
            }
        }

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        public double Long
        {
            get
            {
                if (ViewState["Long"] != null)
                    return (double)ViewState["Long"];
                return 0;
            }
            set
            {
                ViewState["Long"] = value;
            }
        }

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        public double Lat
        {
            get
            {
                if (ViewState["Lat"] != null)
                    return (double)ViewState["Lat"];
                return 0;
            }
            set
            {
                ViewState["Lat"] = value;
            }
        }

        [Bindable(true)]
        [Category("Layout")]
        [DefaultValue("500px")]
        public override Unit Width
        {
            get
            {
                if (ViewState["Width"] != null)
                    return (Unit)ViewState["Width"];
                return 0;
            }
            set
            {
                ViewState["Width"] = value;
            }
        }

        [Bindable(true)]
        [Category("Layout")]
        [DefaultValue("500px")]
        public override Unit Height
        {
            get
            {
                if (ViewState["Height"] != null)
                    return (Unit)ViewState["Height"];
                return 0;
            }
            set
            {
                ViewState["Height"] = value;
            }
        }

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("5")]
        public int Zoom
        {
            get
            {
                if (ViewState["Zoom"] != null)
                    return (int)ViewState["Zoom"];
                return 0;
            }
            set
            {
                ViewState["Zoom"] = value;
            }
        }

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        public string KmlFile
        {
            get
            {
                if (ViewState["KmlFile"] != null)
                    return (string)ViewState["KmlFile"];
                return string.Empty;
            }
            set
            {
                ViewState["KmlFile"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            var divTag = "<div id='map_canvas' style='width: " + Width + "; height: " + Height + "'></div>";
            output.Write(divTag);
            var jsGMapSrc =
                "<script src='http://maps.google.com/maps?file=api&v=2&sensor=true_or_false&key=" +
                AppKey + "'type='text/javascript'></script>";


            //TODO:  Find the best place to place this javascript
            Page.ClientScript.RegisterStartupScript(GetType(), "jsGMapSrc", jsGMapSrc);
            var jsInitializae = "<script language='javascript'>function initialize() {";
            jsInitializae += "var map = new GMap2(document.getElementById('map_canvas'));";
            jsInitializae += "var point = new GLatLng(" + Lat + ", " + Long + ");";
            jsInitializae += "map.setCenter(point, " + Zoom + ");";
            jsInitializae += "map.setUIToDefault();";

            if (!(string.IsNullOrEmpty(KmlFile)))
            {
                jsInitializae += "geoXml = new GGeoXml('" + KmlFile + "');";
                jsInitializae += "map.addOverlay(geoXml);";
            }

            jsInitializae += "var ui = new GMapUIOptions();";
            jsInitializae += "ui.maptypes = { hybrid: true };";
            jsInitializae += "ui.zoom = { scrollwheel: true };";
            jsInitializae += "ui.controls = { largemapcontrol3d: true };";
            jsInitializae += "map.setUI(ui);}</script>";
            Page.ClientScript.RegisterStartupScript(GetType(), "GMap2Init", jsInitializae);
            if (!Page.ClientScript.IsStartupScriptRegistered("onload"))
            {
                Page.ClientScript.RegisterStartupScript
                    (GetType(), "onload", "initialize();", true);
            }
        }
    }
}

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="Google" namespace="Google" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <cc1:GoogleMaps ID="GoogleMaps1" runat="server" Long="-115.814431" Lat="37.242303" Zoom="4" Height="500px" Width="500px" />
    </form>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值