[C#实战]Google Map开发实战参考

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Text;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for GoogleMapCommon
/// </summary>

public   class  GoogleMapCommon
{
    Page mPageHandel 
= null;   
    
string mMapDivName = string.Empty;
    GoogleMapPoint mCenterPoint;
    
string mMapHandleName;
    
int mMapZoom;
    
private readonly static string mGoogleMapURL = ConfigurationManager.AppSettings["GoogleMapURL"];
    
/// <summary>
    
/// Show Google Map
    
/// </summary>
    
/// <param name="pageHandel">page handle</param>
    
/// <param name="mapHandleName">Map Handle Name.</param>
    
/// <param name="mapDivName">Div where Shows map</param>
    
/// <param name="centerPoint">center point</param>
    
/// <param name="zoom">map zoom 1 to 19</param>

    public GoogleMapCommon(Page pageHandel, string mapHandleName, string mapDivName, GoogleMapPoint centerPoint, int mapZoom)
    
{
        mPageHandel 
= pageHandel;
        mMapHandleName 
= mapHandleName;
        mMapDivName 
= mapDivName;
        mCenterPoint 
= centerPoint;
        
if (mapZoom > 19 || mapZoom < 1)
        
{
            mMapZoom 
= 4;
        }

        
else
        
{
            mMapZoom 
= mapZoom;
        }

    }

    
/// <summary>
    
/// Show Google mape
    
/// </summary>

    public void ShowMap()
    
{
        ShowMap(
null);
    }

    
/// <summary>
    
///  Show Google mape
    
/// </summary>
    
/// <param name="point"></param>

    public void ShowMap(GoogleMapPoint point)
    
{
        GoogleMapPoint[] points 
={ point};
        ShowMap(points);
    }

    
/// <summary>
    
/// Show Google mape
    
/// </summary>
    
/// <param name="points"></param>

    public void ShowMap(GoogleMapPoint[] points)
    
{
        mPageHandel.ClientScript.RegisterClientScriptInclude(
"GoogleMaps", mGoogleMapURL);
        StringBuilder js 
= new StringBuilder("var " + mMapHandleName + " = null;  ");
        js.AppendLine(
"function load()");
        js.AppendLine(
"");
        js.AppendLine(
"     if (GBrowserIsCompatible()) ");
        js.AppendLine(
"     {");
        js.AppendLine(
"          " + mMapHandleName + " = new GMap2(document.getElementById("" + mMapDivName + ""));");
        js.AppendLine(
"     " + mMapHandleName + ".setCenter(new GLatLng(" + mCenterPoint.Latitude + "" + mCenterPoint.Longitude + ")," + mMapZoom + ");");
        
if (points != null)
        
{
            
for (int i = 0; i < points.Length; i++)
            
{
                js.AppendLine(
" var point" + i + " = new GLatLng(" + points[i].Latitude + "" + points[i].Longitude + ")");
                js.AppendLine(
"var blueIcon = new GIcon(G_DEFAULT_ICON);");
                js.AppendLine(
"blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";");
                js.AppendLine(
"blueIcon.iconSize = new GSize(35,35);");
                js.AppendLine(
"blueIcon.shadowSize=new GSize(60,35);");
                js.AppendLine(
"markerOptions = { icon:blueIcon }; "); 
                
if (points[i].IsBuleIcon)   //Create a blue icon
                {
                    js.AppendLine(
" var marker" + i + " = new GMarker(point" + i + ",markerOptions);");
                }

                
else
                
{
                    js.AppendLine(
" var marker" + i + " = new GMarker(point" + i + ");");
                }

                
if (!string.IsNullOrEmpty(points[i].PointClickHandle))
                
{
                    
// add event
                    js.AppendLine("GEvent.addListener(marker" + i + ", "click", function(){ " + mMapHandleName + ".openInfoWindowHtml(point" + i + "" + points[i].PointClickHandle + ")});");
                }

                js.AppendLine(mMapHandleName 
+ ".addOverlay(marker" + i + ");");
            }

        }

        js.AppendLine(
"     }");
        js.AppendLine(
"}"); //create function finsh
        js.AppendLine("load();");   // call the load function.
        mPageHandel.ClientScript.RegisterStartupScript(mPageHandel.GetType(), "Start", js.ToString(),true);
    }

    
/// <summary>
    
/// Google Map Point
    
/// </summary>

    public struct GoogleMapPoint
    
{
        
public GoogleMapPoint(double longitude, double latitude)
        
{
            Longitude 
= longitude;
            Latitude 
= latitude;
            PointClickHandle 
= string.Empty;
            IsBuleIcon 
= false;
        }

        
public GoogleMapPoint(double longitude, double latitude, string pointClickHandle, bool isBuleIcon)
        
{
            Longitude 
= longitude;
            Latitude 
= latitude;
            PointClickHandle 
= pointClickHandle;
            IsBuleIcon 
= isBuleIcon;
        }

        
public double Longitude;
        
public double Latitude;
        
public string PointClickHandle;
        
public bool IsBuleIcon;
    }

}

  该类封装了Google Maps API的一些最基本功能,供大家参考。 详情请参考: http://code.google.com/apis/maps/documentation/index.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值