获取图吧的地图
2016-06-07 11:24:30 访问(1834) 赞(0) 踩(0)
using System;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.Projections;
namespace GMapDemo.Mapbar
{
public abstract class MapbarProviderBase :GMapProvider
{
GMapProvider[] overlays;
public MapbarProviderBase()
{
MaxZoom = null;
RefererUrl = "http://10.23.10.102:81";
Copyright = string.Format("?{0} eKingLBS Corporation",DateTime.Today.Year);
}
public override PureProjection Projection
{
get { return MercatorProjection.Instance; }
}
public override GMapProvider[] Overlays
{
get {
if (overlays == null)
{
overlays = new GMapProvider[] { this };
}
return overlays;
}
}
}
}
// MapbarProvider //
using System;
using GMap.NET;
namespace GMapDemo.Mapbar
{
class MapbarProvider:MapbarProviderBase
{
public static readonly MapbarProvider Instance;
private readonly Guid m_id = new Guid("EF3DD303-3F74-4938-BF40-232D0595EE88");
private readonly string m_Name = "MapBar";
public override Guid Id { get { return m_id; } }
public override string Name { get { return m_Name; } }
static MapbarProvider()
{
Instance = new MapbarProvider();
}
public override GMap.NET.PureImage GetTileImage(GMap.NET.GPoint pos, int zoom)
{
try
{
string url = MakeTileImageUrl(pos,zoom,LanguageStr);
return GetTileImageUsingHttp(url);
}
catch(Exception ex)
{
return null;
}
}
//http://10.23.10.102:81/advdynamic/02/GetPng/1/15/26437/14532/?default
static readonly string urlFormat = "http://10.23.10.102:81/advdynamic/02/GetPng/1/{0}/{1}/{2}/?default";
string MakeTileImageUrl(GMap.NET.GPoint pos, int zoom, string language)
{
//GMap.NET.GPoint offset = GetOffset(zoom,pos.Y);
//long num = Convert.ToInt64(Math.Pow(2,21-zoom));
//long X = pos.X * num / 256;
//long Y = pos.Y * num / 256;
//GPoint currPoint = GetPoint(zoom,offsetX,offsetY);
//http://10.23.10.102:81/advdynamic/02/GetPng/1/16/52872/29061/?default
string retUrl = string.Format(urlFormat, zoom, pos.X, pos.Y);
Console.WriteLine("url:" + retUrl);
return retUrl;
}
}
}
// Form1 //
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GMap.NET.WindowsForms;
using GMap.NET.MapProviders;
using GMap.NET;
using GMap.NET.WindowsForms.Markers;
using System.Net;
using GMapDemo.Mapbar;
namespace GMapDemo
{
public partial class Form1 : Form
{
GMapOverlay gMapOverlay = null;
public Form1()
{
InitializeComponent();
toolStripComboBox1.Text = "110.458457966533/19.9400473726004";
try
{
IPAddress ip = IPAddress.Parse("10.23.10.102");
System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry(ip);
//System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("ditu.google.cn");
}
catch
{
mapControl1.Manager.Mode = GMap.NET.AccessMode.CacheOnly;
MessageBox.Show("No internet connection available,going to CacheOnly mode.", "GMapDemo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//mapControl1.Manager.Mode = GMap.NET.AccessMode.CacheOnly;
mapControl1.CacheLocation = Environment.CurrentDirectory + "\\GMapCache\\"; //缓存文件所在位置
//mapControl1.MapProvider = GMapProviders.GoogleChinaSatelliteMap;
mapControl1.MapProvider = MapbarProvider.Instance;
//mapControl1.MapProvider.Area = new RectLatLng(110.47047,19.929807, 0.00555806, 0.025724);
//mapControl1.BoundsOfMap = new RectLatLng(110.47047, 19.929807, 0.00555806, 0.025724);
mapControl1.MinZoom = 14;
mapControl1.MaxZoom = 21;
mapControl1.Zoom = 14;
mapControl1.ShowCenter = true;
mapControl1.DragButton = System.Windows.Forms.MouseButtons.Left;
mapControl1.Position = new PointLatLng(19.942679, 110.442668);
mapControl1.MouseClick += new MouseEventHandler(mapControl_MouseClick);
gMapOverlay = new GMapOverlay("mark"); //创建图层
mapControl1.Overlays.Add(gMapOverlay); //向控件中添加图层
}
void mapControl_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
PointLatLng point = mapControl1.FromLocalToLatLng(e.X,e.Y);
String show = String.Format("Lat={0:00.000000},Lng={1:000.000000}",point.Lat,point.Lng);
MessageBox.Show(show,"CurrentLocation",MessageBoxButtons.OK);
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
protected double GetXY(string str, bool isX)
{
int idx = str.IndexOf('/');
if (isX)
return double.Parse(str.Substring(0, idx));
else
return double.Parse(str.Substring(idx + 1));
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
//创建图层(overlay)和标签(marker),将标签加入图层,再将图层加入控件中
try
{
double dX = 0;
double dY = 0;
dX = GetXY(toolStripComboBox1.Text, true);
dY = GetXY(toolStripComboBox1.Text, false);
GMapMarker gMapMarker
=
new GMarkerGoogle(new PointLatLng(dY,dX),
GMarkerGoogleType.green);//在(45.7,126.695)上绘制一绿色点
gMapOverlay.Markers.Add(gMapMarker); //向图层中添加标签
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
MainForm f = new MainForm();
f.Show();
}
}
}
标签:
获取图吧的地图 


上一条:
下一条:
相关评论
发表评论