wpf GMap

Hey,Coder!發表於2024-08-13

AMapProviderBase

public abstract class AMapProviderBase : GMapProvider
{
    public AMapProviderBase()
    {
        MaxZoom = null;
        RefererUrl = "http://www.amap.com/";
        //Copyright = string.Format("©{0} 高德 Corporation, ©{0} NAVTEQ, ©{0} Image courtesy of NASA", DateTime.Today.Year);    
    }

    public override PureProjection Projection
    {
        get { return MercatorProjection.Instance; }
    }

    GMapProvider[] overlays;
    public override GMapProvider[] Overlays
    {
        get
        {
            if (overlays == null)
            {
                overlays = new GMapProvider[] { this };
            }
            return overlays;
        }
    }
}

public class AMapProvider : AMapProviderBase
{
    public static readonly AMapProvider Instance;

    readonly Guid id = new Guid("EF3DD303-3F74-4938-BF40-232D0595EE88");
    public override Guid Id
    {
        get { return id; }
    }

    readonly string name = "AMap";
    public override string Name
    {
        get
        {
            return name;
        }
    }

    static AMapProvider()
    {
        Instance = new AMapProvider();
    }

    public override PureImage GetTileImage(GPoint pos, int zoom)
    {
        try
        {
            string url = MakeTileImageUrl(pos, zoom, LanguageStr);
            return GetTileImageUsingHttp(url);
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    string MakeTileImageUrl(GPoint pos, int zoom, string language)
    {
        var num = (pos.X + pos.Y) % 4 + 1;
        //string url = string.Format(UrlFormat, num, pos.X, pos.Y, zoom);
        string url = string.Format(UrlFormat, pos.X, pos.Y, zoom);
        return url;
    }

    //static readonly string UrlFormat = "http://webrd04.is.autonavi.com/appmaptile?x={0}&y={1}&z={2}&lang=zh_cn&size=1&scale=1&style=7";
    static readonly string UrlFormat = "http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={0}&y={1}&z={2}";
}

介面上新增控制元件

<usercontrol:MapControl
    x:Name="mapControl"
    Panel.ZIndex="100"
    MaxZoom="24"
    MinZoom="1"
    Zoom="13" />

在介面的Loaded中新增

  mapControl.CacheLocation = Environment.CurrentDirectory + "\\GMapCache\\"; //快取位置
  //mapControl.MapProvider = GMapProviders.GoogleChinaMap; // 這裡需要填寫地圖提供商,官方預設包含Google地圖
  //mapControl.MapProvider = GaoDeimgProvider.Instance; // 這裡需要填寫地圖提供商,官方預設包含Google地圖
  mapControl.MapProvider = AMapProvider.Instance; // 這裡需要填寫地圖提供商,官方預設包含Google地圖
  mapControl.MinZoom = 2;  //最小縮放
  mapControl.MaxZoom = 17; //最大縮放
  mapControl.Zoom = 5;     //當前縮放 
  mapControl.ShowCenter = true; //不顯示中心十字點
  mapControl.DragButton = MouseButton.Left; //左鍵拖拽地圖
  mapControl.Position = new PointLatLng(32.064, 118.704); //地圖中心位置:珠  海

相關文章