网上很多获取google卫星图的url地址都已经过时,在这里为各位使用arcgis api 的朋友们提供一个可用的url。综合,以前分享的“自己写图层加载google在线地图”。所以把两种常用的道路地图与卫星图集合在一起,供大家学习交流。
public class GoogleTileMap : TiledMapServiceLayer
{
private const string TilePathBase = @"http://mt{0}.google.com/vt/lyrs={1}&z={2}&x={3}&y={4}";
private int server_rr = 0;
private const string charStreet = "m";
private const string charSatellite = "s";
private bool _IsTileDownloadStarted;
private GoogleMapModes _MapMode;
//默认是街道地图
public GoogleTileMap()
{
MapMode = GoogleMapModes.Street;
}
public GoogleTileMap(GoogleMapModes mode)
{
MapMode = mode;
}
public GoogleMapModes MapMode
{
get { return _MapMode; }
set
{
if (_IsTileDownloadStarted)
{
throw new InvalidOperationException();
}
_MapMode = value;
}
}
public override string GetTileUrl(int level, int row, int col)
{
_IsTileDownloadStarted = true;
server_rr = (server_rr + 1) % 4;
string url = string.Empty;
switch (MapMode)
{
case GoogleMapModes.Street:
url = string.Format(TilePathBase, server_rr, charStreet, level, col, row);
break;
case GoogleMapModes.Satellite:
url = string.Format(TilePathBase, server_rr, charSatellite, level, col, row);
break;
default:
break;
}
return url;
}
public override void Initialize()
{
double resolution = 156543.033928;
this.FullExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787);//(-180,-85.0511287798066,180, 85.0511287798066)
{
SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102113);
};
this.SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102113);
this.TileInfo = new TileInfo()
{
Height = 256,
Width = 256,
Origin = new ESRI.ArcGIS.Client.Geometry.MapPoint(-20037508.342787,20037508.342787)
{
SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102113)
},
Lods = new Lod[20]
};
for (int i = 0; i < TileInfo.Lods.Length; i++)
{
TileInfo.Lods[i] = new Lod() { Resolution = resolution };
resolution /= 2;
}
base.Initialize();
}
}
使用就很简单:
<mapsvc:GoogleTileMap ID="MyLayer" MapMode="Street" ></mapsvc:GoogleTileMap>