现在要做一个网站,实现google地图功能,
1、查找点,根据地名查找相应的点并剧中显示(貌似google上还有相关信息介绍)
2、绘制路线,从某地到某地之间的最短路线绘制,并计算里程
3、绘制标签,标签需要在相应的点绘制图片
4、绘制区域,方形区域,特殊显示
希望各位朋友给介绍一下,给我指点一下都有什么函数,我看了http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/v2/reference.html#GControlPosition上的说明,由于本人英语水平有限,而且函数太多,没有找到,希望大家给帮个忙~~谢谢了

解决方案 »

  1.   

    大哥啊,我也在做类似的东西,很是郁闷,不知道咋搞,你知道目前主流博客的API吗?
      

  2.   

    前台:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoogleMap.aspx.cs" Inherits="Interface_GoogleMap_GoogleMap" %><%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Namespace="System.Web.UI" TagPrefix="asp" %>
    <!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>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>XXX</title>    <script language="javascript" src="../../SubModal/jquery.js"></script>    <!--This is the second version of Google APIs -->    <script src="http://www.google.com/jsapi?key=ABQIAAAANnGLeebiO_H65hLs7pkJqBTBJ-CyJGAlAVc19j4_jqPdBNjPTBQiVtI_VCjNezW7M6N8hJWTo604Aw"
            type="text/javascript"></script>    <script language="Javascript" type="text/javascript">
         google.load("maps","2");
         var map;
         function init()
         {
         map= new google.maps.Map2(document.getElementById("map2"));        
         var CenterLatLng = document.getElementById("HiddenSetCenter").value;   
                var Center_latlng = CenterLatLng.split("|");
                if(Center_latlng.length == 3)
                {
             if(Center_latlng[2]==12)
                    {            
             map.setCenter(new GLatLng(Center_latlng[0], Center_latlng[1]),12);
             }
             else if(Center_latlng[2]==14)
             {            
             map.setCenter(new GLatLng(Center_latlng[0], Center_latlng[1]),14);
             }
             else if(Center_latlng[2]==16)
             {
                 map.setCenter(new GLatLng(Center_latlng[0], Center_latlng[1]),16);
             }
             else
             {
                 map.setCenter(new GLatLng(Center_latlng[0], Center_latlng[1]),15);
             }
         }
         else
         {
             document.getElementById("map2").innerHTML = "加载地图出现错误,具体原因是:地图中心设置不正确";
             return;
         }
               
                //map.setCenter(new GLatLng(32.00712978658444, 118.80557298660278),12);
         map.setUIToDefault();
        
         GEvent.addListener(map, "moveend", function() {
       var center = map.getCenter();
       document.getElementById("mapCenterMSG").innerHTML = center.toString();
    });

                //想改进成在此处添加一个右击事件,直接添加设备
              GEvent.addListener(map,"singlerightclick", function(point,src,overlay) {     
              var myHtml = "此处(右击处)坐标为: " + map.fromContainerPixelToLatLng(point);
              document.getElementById("mapCenterMSG").innerHTML = myHtml.toString();
              var latlng = myHtml.split("(")[2].split(")")[0];
              var lat = latlng.split(",")[0];
              var lng = latlng.split(",")[1];
              document.getElementById("hLat").value = lat;
              document.getElementById("hLng").value = lng;
              
            });
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
      /***
      *下面是添加点击事件
      **/
      /*
     GEvent.addListener(map,"click", function(overlay,latlng) {     
              var myHtml = "当前坐标 为: " + map.fromLatLngToDivPixel(latlng) + ",<br>缩放级别为:" + map.getZoom();
              map.openInfoWindow(latlng, myHtml);
            });
            */
            //map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());//添加混合地图
            //map.openInfoWindow(new GLatLng(32.00712978658444, 118.80557298660278),document.createTextNode("Hello, world"));
            //----------------------------------------------------
            // 为所有标记创建指定阴影、图标尺寸灯的基础图标
            
            
            //返回数据排列
            //FixingInfoTable.Rows[i]["Fixing_name"].ToString() + "|" + FixingInfoTable.Rows[i]["Fixing_address"].ToString() + "|" + DepartmentTable.Rows[0]["Department_Name"].ToString() + "|" + FixingInfoTable.Rows[i]["lat"].ToString() + "|" + FixingInfoTable.Rows[i]["lng"].ToString()"|" + DataInfoTable.Rows[j]["MinValue"].ToString() + "#" + DataInfoTable.Rows[j]["MaxValue"].ToString() + "#" + DataInfoTable.Rows[j]["AvgValue"].ToString() + "#" + DataInfoTable.Rows[j]["CouValue"].ToString()
            var department_id = document.getElementById("hiddenBiaozhi").value;
            PageMethods.GetTerminalSize(department_id,BindTiminalSizeS,failS);
            function BindTiminalSizeS(result){ 
            var sk = result;
            var baseIcon = new GIcon();
            baseIcon.shadow = "http://www.google.cn/mapfiles/shadow50.png";
            baseIcon.iconSize = new GSize(20, 34);
            baseIcon.shadowSize = new GSize(37, 34);
            baseIcon.iconAnchor = new GPoint(9, 34);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            baseIcon.infoShadowAnchor = new GPoint(18, 25);        // 创建信息窗口显示对应给定索引的字母的标记
            function createMarker(point, index,Info,state) {
              // Create a lettered icon for this point using our icon class
              var letter = String.fromCharCode("A".charCodeAt(0) + index);
              var letteredIcon = new GIcon(baseIcon);
              var iconIndex = index+1;
               //设备工作不正常警告标志
              if(state == "ErrorData")
              {         
                  letteredIcon.image ="http://gmaps-samples.googlecode.com/svn/trunk/ers/red/er"+iconIndex+".png"; //"http://gmaps-samples.googlecode.com/svn/trunk/ers/red/er"+iconIndex+".png";
                  
              } 
              else if(state == "NomalData")
              {
                  letteredIcon.image="http://gmaps-samples.googlecode.com/svn/trunk/ers/green/er"+iconIndex+".png";
                 
              }
              else if(state == "NewFixing")//新设备已经取消
              {
                 letteredIcon.image ="http://gmaps-samples.googlecode.com/svn/trunk/ers/green/er"+iconIndex+".png";
              }
              // 设置 GMarkerOptions 对象
              erOptions = { icon:letteredIcon };
              var er = new GMarker(point, erOptions);
              GEvent.addListener(er, "click", function() {
                er.openInfoWindowHtml("" + Info + "");
              });
              return er;
            }
            
            // 向地图添加设备标记
            var bounds = map.getBounds();
            var southWest = bounds.getSouthWest();
            var northEast = bounds.getNorthEast();
            var lngSpan = northEast.lng() - southWest.lng();
            var latSpan = northEast.lat() - southWest.lat();
            var a = result;
            var k= a.length;
            var nomalNum=0;
            var errorNum=0;
            for (var i = 0; i < k; i++) {
            var c = a;
            var b = c.shift();
            var FixInfoA = b.split('|'); 
            //数据长度(fixInfoA.length>4)非正常数据仅有设备信息,没有数据信息长度才为4
            var state = 'NomalData';
            if(state == "NomalData")
            {
                nomalNum++;
            }
            if(state == "ErrorData")
            {
                errorNum ++;
            }
            document.getElementById("TotleFixing").innerHTML = k.toString();
            document.getElementById("ErrorFixing").innerHTML= errorNum.toString();
            document.getElementById("NomalFixing").innerHTML= nomalNum.toString();
            var Info = "<b>设备"+FixInfoA[0]+"</b><br>所属部门:"+FixInfoA[2];//+"<br>瞬间流量为:<b>"+FixInfoA[5]+"m³/h</b><br>累加流量为:<b>"+FixInfoA[3]+"m³</b>"//<br>上次累加读数为:<b>"+FixInfoA[5]+"</b>
            //for循环添加数据
            var latlng = new GLatLng(FixInfoA[3],FixInfoA[4]);
            map.addOverlay(createMarker(latlng, i,Info,state));
              
            }
            }
              function failS(result){
            }
            //点击地图事件--显示所有信息
    //        GEvent.addListener(map, "singlerightclick", function() { 
    //                    GetTerminalSizeX();
    //                    
    //           
    //        });     //}//End of Init Function
            var count = 0;
            function GetTerminalSizeX(){
            PageMethods.GetTerminalSize(BindTiminalSize,fail);
            
            }
             function BindTiminalSize(result){ 
              var a = result;
              var k= a.length;
              var infoCenter = "数据采集实时显示:<br>"
              for(var i = 0;i<k;i++ )
              {
                 var b = a.shift();
                 var FixInfoA = b.split('|');
                 infoCenter += "<b>"+FixInfoA[0]+"</b><br>当前计量为:<b>"+FixInfoA[3]+"</b><br>累加计量为:<b>"+FixInfoA[4]+"</b><br>上次累加计量为:<b>"+FixInfoA[5]+"</b><br>***********<br>"
              } 
    //             var c = a.shift();  
    //             var FixInfoB = c.split('|'); 
                  map.openInfoWindow(map.getCenter(),infoCenter);
    //            map.openInfoWindow(map.getCenter(),"<div id='tom2'>"+"数据采集实时显示:<br>"+FixInfoA[0]+"累加量为:"
    //+FixInfoA[4]+"<br>"+FixInfoB[0]+"累加量为:"+FixInfoB[4]+"</div>");
            //  document.createTextNode( "数据采集实时显示||"+FixInfoA[0]+"累加量为:"+FixInfoA[4]+"||"+FixInfoB[0]+"累加量为:"+FixInfoB[4]));        //setTimeout("GetTerminalSizeX()",1000);
            }
            //请求后台 失败处理方法  异常
            function fail(result){
            } 
         }//End of Init Function
        
        
         //导航到默认位置
         function gotoDefaultPlace()
         {
         if(map!=null)
         {
         map.panTo(new GLatLng(32.00712978658444, 118.80557298660278));    
         }
         }
      

  3.   


    //     //导航到某个经纬度
    //     function gotoPlace()
    //     {
    //     if(map!=null)
    //     {
    //     map.panTo(
    //     new GLatLng(
    //     document.getElementById("la").value, 
    //     document.getElementById("lo").value), 1000);
    //     }
    //     }
    //     function _ss()
    //     {
    //          alert(document.getElementById("Hidden1").value);
    //     }
         google.setOnLoadCallback(init);
        </script>    <%--<script>setTimeout("location.href='../GoogleMap/googleMap.aspx'",2000)</script>--%>
        <!--End of the second version of Google Apis. -->
        <link href="../../Style/Basic.css" type="text/css" rel="stylesheet" />
    </head>
    <body onunload="GUnload()">
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
            </asp:ScriptManager>
            <!--V1 Google Maps container:
        <div id="map_canvas" style="width: 500px; height: 300px"></div>
        -->
            <p />
            <!--V2 Google Maps Container -->
            <asp:Button ID="btnOK" runat="server" Text="用列表显示" CssClass="ButtonCss" OnClick="btnOK_Click" /><br />
            <div id="viewNum">
                当前共有设备<asp:Label ID="TotleFixing" runat="server" Text="0" ForeColor="blue" />个(其中异常设备有<asp:Label
                    ID="ErrorFixing" runat="server" Text="0" ForeColor="blue" />个,正常设备为<asp:Label ID="NomalFixing"
                        runat="server" Text="0" ForeColor="blue" />个)
            </div>
              <div style="display: none;z-index:10000;" id="MenuDiv">
                <table border="0" cellpadding="0" cellspacing="0" width="100px" >
                    <tr>
                        <td style="border: black 0px solid;  height: 20px; color: White"
                            align="center">
                             <asp:LinkButton ID = "AddFixing" runat ="server" Text = "在此处增加设备" OnClick="AddFixing_Click" />
                             </td>
                    </tr>
                    <tr>
                        <td style="height: 1px">
                        </td>
                    </tr>
                    <tr>
                        <td class="none" onmouseout="this.className='none';" onmouseover="this.className='over';"
                            style="height: 20px" align="center">
                          
                        </td>
                    </tr>
                </table>
            </div>
            <br />
            <asp:Label ID="lblNote" runat="server" ForeColor="Red" Text="小提示:在需要添加设备的地方单击右键即可"></asp:Label><br />
            &nbsp;<asp:Panel ID="MapPanel" runat="server">
                <table runat="server" id="Table1">
                    <tr style="height: 500px">
                        <td style="width: 52%; height: 521px;">
                            <div id="map2" style="width: 550px; height: 500px">
                                当前网络连接不正常,地图无法显示,请用列表显示!!!
                            </div>
                        </td>
                        <td style="height: 520px" runat="server" id="Td1">
                            <div id="treeView" runat="server" style="height: 500px">
                                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                    <ContentTemplate>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                                <asp:TreeView ID="tvLatLng" runat="server" Height="360px" ImageSet="Arrows" OnSelectedNodeChanged="tvLatLng_SelectedNodeChanged">
                                    <SelectedNodeStyle BackColor="#C0C0FF" />
                                </asp:TreeView>
                            </div>
                        </td>
                    </tr>
                </table>
                地图中心坐标
                <div id="mapCenterMSG">
                </div>
                <!--End of V2 Google Maps container-->
                <!--
        Show the center of the map.
        显示右击时坐标
        -->
                <br />
                <table>
                    <tr>
                        <td colspan="2">
                            &nbsp; &nbsp;&nbsp; 图标示例
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Image ID="Image1" runat="server" ImageUrl="http://gmaps-samples.googlecode.com/svn/trunk/ers/green/er1.png" /></td>
                        <td>
                            设备工作正常图标</td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Image ID="Image2" runat="server" ImageUrl="http://gmaps-samples.googlecode.com/svn/trunk/ers/red/er1.png" />
                        </td>
                        <td>
                            设备工作异常图标
                        </td>
                    </tr>
                </table>
            </asp:Panel>
            <!--
        End of show the center of the map.
        -->
            <!--Go to a place: -->
            <input id="HiddenSetCenter" runat="server" type="hidden" />
            <input id="hiddenBiaozhi" type="hidden" runat="server" />
            <input id="Hidden1" type="hidden" runat="server" />
            <input id="hiddenState" runat="server" type="hidden" />
            <input id="hDepartmentID" runat="server" type="hidden" />
            <input id="hLat" runat="server" type="hidden" />
            <input id="hLng" runat="server" type="hidden" />
            <br />
            <!--
        End of go to a place
        -->
        </form>
    </body>
    </html>
      <script>
      //添加右击事件,在右击处显示层,可以添加设备
     if (window.Event) 
        document.captureEvents(Event.MOUSEUP); 
        
        //IE 5.0以上
        function nocontextmenu() 
        {
        event.cancelBubble = true
        event.returnValue = false;    //return false;
        showDiv();
        
        }
        
        //其他浏览器
        function norightclick(e) 
        {
            if (window.Event) 
            {
                if (e.which == 2 || e.which == 3)
                showDiv();
                //return false;
            }
            else
                if (event.button == 2 || event.button == 3)
                {
                    event.cancelBubble = true
                    event.returnValue = false;
                    showDiv();
                    //return false;
                }
        }
        
        //显示菜单
        function showDiv(){
        var role_sn = document.getElementById("hDepartmentID").value;
            if(role_sn == 1)//如果不是管理员,不允许添加设备
            {
                document.getElementById("MenuDiv").style.top = event.clientY;
                document.getElementById("MenuDiv").style.left = event.clientX;
                document.getElementById("MenuDiv").style.position = "absolute";
                document.getElementById("MenuDiv").style.display = "block";
            }
        }
        //关闭菜单
        function closeDiv(){
            document.getElementById("MenuDiv").style.display = "none";
        }
        
        document.getElementById("map2").oncontextmenu=nocontextmenu;
        document.getElementById("map2").onmousedown = norightclick; //对其它浏览器
        document.getElementById("map2").onclick = closeDiv;
        
    </script>
      

  4.   

    后台代码:
    public partial class Interface_GoogleMap_GoogleMap : System.Web.UI.Page
    {
        //session信息
            //emp.Employee_Sn = Convert.ToInt32(table.Rows[0][0]);
            //emp.SysName = table.Rows[0][1].ToString();
            //emp.PassWord = table.Rows[0][2].ToString();
            //emp.Name = table.Rows[0][3].ToString();
            //emp.Memo = table.Rows[0][4].ToString();
            //emp.LastInDate = Convert.ToDateTime(DateTime.Now);
            //emp.IsValid = table.Rows[0][6].ToString();
            //emp.Department_id = table.Rows[0][7].ToString();
            //emp.Role_sn = Convert.ToInt32(table.Rows[0][8]);
        private string index = "1";
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                this.lblNote.Visible = false;
                DataTable table = (DataTable)Session["Employee"];
                if (table == null)
                {
                    Response.Redirect("../Login/login.aspx");
                }
                else
                {
                    
                    string department_id = table.Rows[0][7].ToString();
                    this.hiddenBiaozhi.Value = department_id;
                    GetLatLngByDepartment_id(department_id);
                    this.hDepartmentID.Value = table.Rows[0][8].ToString();
                    if (table.Rows[0][8].ToString() == "1")
                    {
                        this.lblNote.Visible = true;
                    }
                    else
                    {
                        this.lblNote.Visible = false;
                    } 
                    InitTree(null);
                }
            }
        }
        //通过部门ID获取部门坐标信息
        public void GetLatLngByDepartment_id(string department_id)
        {
            string LatLng = "";
            //部门表结构
            //DepartMent_id, Department_Name, Memo, Isvalid, lat, lng
            DataTable table = new DataTable();
            string msg = DepartmentManager.GetDepartOutTable(department_id, out table);
            if (msg == "")
            {
                LatLng = table.Rows[0][4] + "|" + table.Rows[0][5];
                string IsZoon = "";
                if (department_id.Length == 2)
                {
                    IsZoon = "12";
                }
                else if (department_id.Length == 4)
                {
                    IsZoon = "14";
                }
                else  if(department_id.Length == 6)
                {
                    IsZoon = "16";
                }
                if (IsZoon != "")
                {
                    LatLng += "|" + IsZoon;
                }        }
            this.HiddenSetCenter.Value = LatLng;
        }
        //webService方法返回数据到前台
        [System.Web.Services.WebMethod]
        public static string[] GetTerminalSize(string department_id)
        {
            return GoogleMapManager.returnDataAndFixingInfoByCondition(department_id);
        }
        protected void tvLatLng_SelectedNodeChanged(object sender, EventArgs e)
        {
            string CenterLatLng = "";
            string nodeValue = tvLatLng.SelectedValue;
            if (nodeValue.Length < 7)//如果长度小于7说明不是设备级别,是工厂级别,那么用nodevalue去数据库中的部门表中查处坐标,然后再地图上定位
            {
                string Error = "";
                DataTable dataTable = new DataTable();
                Error = DepartmentManager.GetDepartOutTable(nodeValue, out dataTable);
                if (Error != "")
                {
                    return;
                }
                CenterLatLng = dataTable.Rows[0]["lat"] + "|" + dataTable.Rows[0]["lng"] + "|14";//1代表设备,2代表部门
            }
            else
            {
                CenterLatLng = nodeValue;
            }
            this.HiddenSetCenter.Value = CenterLatLng;
            //treeView点击切换GIS位置以及定位
            ScriptManager.RegisterStartupScript(tvLatLng, this.GetType(), "xx", "<script>init()</script>", false);
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            Response.Redirect("table.aspx");
        }    //加载部门tree
        private void InitTree(TreeNode node)
        {
            string department_id = "";
            //由于 是递归调用,所以此处重新获取部门Id
            DataTable table = (DataTable)Session["Employee"];
            if (table == null)
            {
                Response.Redirect("../Login/login.aspx");
            }
            else
            {
                department_id = table.Rows[0][7].ToString();        }
            string sError = "";
            string[] saParms = new string[2];
             List<Department> list = null;
            if (node == null)
            {
                saParms[0] = department_id.Length.ToString();
                saParms[1] = department_id;
            }
            else
            {
                if (node.Value.Length == 8)
                    return;
                saParms[0] = Convert.ToString(node.Value.Length + 2);
                saParms[1] = node.Value;
            }
            try
            {
                list = (List<Department>)DepartmentManager.GetDepartmentLikeId(saParms);
            }
            catch (Exception ex)
            {
                Response.Redirect("../../../ErrorPage.htm?msg='增加部门树的时候出错了'");
            }
            if (list.Count != 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (node == null)
                    {
                        TreeNode oNode = new TreeNode();
                        oNode.Value = list[i].DepartMent_id.ToString();
                        oNode.Text = list[i].Department_Name.ToString();
                        this.tvLatLng.Nodes.Add(oNode);
                        //加载设备,如果直接加载工厂级别,则从此处加载最下层设备
                        //如果部门长度为6,说明这个部门是工厂级别,那么开始向工厂中加载工厂所有的设备
                        if (oNode.Value.Length == 6)
                        {
                            InitFixingTree(oNode, oNode.Value.ToString());
                        }                    this.InitTree(oNode);
                    }
                    else
                    {
                        TreeNode oNode = new TreeNode();
                        oNode.Value = list[i].DepartMent_id.ToString();
                        oNode.Text = list[i].Department_Name.ToString();
                        node.ChildNodes.Add(oNode);
                        //加载设备
                        //如果部门长度为6,说明这个部门是工厂级别,那么开始向工厂中加载工厂所有的设备
                        if (oNode.Value.Length == 6)
                        {
                            InitFixingTree(oNode, oNode.Value.ToString());
                        }
                        this.InitTree(oNode);
                    }
                }
            }
        }
        private void InitFixingTree(TreeNode node, string department_id)
        {
            
            DataTable table = new DataTable();
            string msg = FixingManager.GetFixingByDepartment_idOutTable(department_id, out table);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                TreeNode oNode = new TreeNode();
                oNode.Value = table.Rows[i]["lat"].ToString() + "|" + table.Rows[i]["lng"].ToString() + "|16";
                oNode.Text = table.Rows[i]["Fixing_name"].ToString() + "(图标" + index + ")";
                node.ChildNodes.Add(oNode);
                int indexTemp = (Convert.ToInt32(index) + 1);
                index = indexTemp.ToString();
            }
        }
        protected void AddFixing_Click(object sender, EventArgs e)
        {
            //在地图上点击,新增设备
            string lat = this.hLat.Value;
            string lng = this.hLng.Value.Trim();
            this.hLat.Value = "";
            this.hLng.Value = "";
            Response.Redirect("../DeviceManager/device.aspx?lat="+lat+"&&lng="+lng);
            
        }
    }
      

  5.   

    GoogleMapControl
    http://www.51aspx.com/CV/GoogleMapControl/
    希望对你有用
      

  6.   

    http://topic.csdn.net/u/20090110/09/f773a435-8542-4faa-85c7-be25f2726711.html
      

  7.   

    我看了你写的,并copy过去调试,有几个类应该是你自己定义的吧,能说明一下什么作用么?
    1、FixingManager.GetFixingByDepartment_idOutTable(部门id,out 表)
    2、DepartmentManager.GetDepartmentLikeId()
    3、Department
    4、DpartmentManager.GetDepartOutTable(点,表)
    5、GoogleMapManager
      

  8.   

    walkghost
     
    (Hello World!) 麻烦看看我10楼的问题呀,还有怎么实时获取鼠标在地图上的坐标呢