[C# 实战]Google Map开发实战参考
<script type="text/javascript"> document.body.oncopy = function() { if (window.clipboardData) { setTimeout(function() { var text = clipboardData.getData("text"); if (text && text.length > 300) { text = text + "\r\n\n本文来自CSDN博客,转载请标明出处:" + location.href; clipboardData.setData("text", text); } }, 100); } } </script><script type="text/javascript"> function StorePage() { d = document; t = d.selection ? (d.selection.type != 'None' ? d.selection.createRange().text : '') : (d.getSelection ? d.getSelection() : ''); void (keyit = window.open('http://www.365key.com/storeit.aspx?t=' + escape(d.title) + '&u=' + escape(d.location.href) + '&c=' + escape(t), 'keyit', 'scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }</script>
该类封装了Google Maps API的一些最基本功能,供大家参考。 详情请参考: http://code.google.com/apis/maps/documentation/index.html
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;
}
}
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;
}
}