Gain a deep understanding of the most powerful desktop map control GMAP. NET-Baidu Map

Source: Internet
Author: User

The first two articles introduce some basic functions of GMAP. NET and how to use it in your own projects.

Deep understanding of the most powerful desktop map control GMAP. NET

Gain a deep understanding of the most powerful desktop map control GMAP. NET --- initial use

 

GMAP. NET supports Google, Bing, Ovi, Openstreetmap, Yahoo, GIS, and other maps, but it does not support many maps in China.

But it does not matter. We can add support for Baidu map for GMAP. NET. It is easy to integrate as long as we understand the principle of map loading,

Most importantly, it supports offline, that is, using GMAP. NET, we can make various offline maps for our desktop applications to use,

That's why I call itStrongest map control.

 

How to Use GMAP. NET Baidu map in your program

The entire Code has been submitted to the http://ypmap.googlecode.com/, interested friends can Check out the code.

If you want to use it, first refer to the article: a deep understanding of the most powerful desktop map control GMAP. NET --- the first use,

And change the initialization code to the following.

This. mainMap. position = new PointLatLng (double. parse (ConfigurationManager. etettings ["defaultLat"]), double. parse (ConfigurationManager. etettings ["defaultLng"]); this. mainMap. mapProvider. area = new RectLatLng (30.981178, 105.351914, 2.765142, 4.120995); this. mainMap. boundsOfMap = new RectLatLng (30.981178, 105.351914, 2.765142, 4.120995); this. mainMap. manager. mode = AccessMode. cacheOnly; this. mainMap. mapProvider = GMapProviders. baiduMapProvider; // or BaiduSateliteMapProvider this. mainMap. dragButton = MouseButton. left; this. mainMap. zoom = 13; this. mainMap. minZoom = 8; this. mainMap. maxZoom = 24;

 

Effect of GMAP. NET Baidu Map

Normal Map

Satellite Map

  Baidu map loading principle

To understand how GMAP. NET uses Baidu maps, it is necessary to first understand the loading principle of Baidu maps.

Open Baidu map in Chrome browser, locate a certain place (for example, Chongqing), and open the developer tool, as shown in:

 

Continue to look at the original Baidu map is composed of such an image, and then look at the link address of one of the images:

Bytes. What do these parameters mean?

Q5 should be the address of the distributed server, so can q3 and q4;

U, x, y should be a value obtained based on the MercatorProjection algorithm, which will be elaborated in the theoretical chapter;

Z = 13 indicates the current Zoom, and v indicates the version number;

Type Indicates access through a browser. fm is a fixed value.

Then GMAP. NET should also construct a url based on the user's mouse position and current scaling level to get the image.

  GMapProvider

GMAP. NET adopts a good code structure, and the cache and data structure all follow the principle of low coupling-high cohesion. The relationship between each module

It is also programmed based on interfaces. The MapProvider interface of the map data source also follows this principle.

GMapProvider is an interface for map data sources. When the client calls this. MainMap. MapProvider = GMapProviders. BaiduMapProvider during initialization,

Data will be loaded according to different map rules. The following uses Baidu map as an example to describe how to extend an interface for map data sources.

 

1). BaiduMapProviderBase

Add abstract class BaiduMapProviderBase to inherit GMapProvider

BaiduMapProviderBaseThree things have been completed.

Information such as RefereUrl and Copyright is initialized, which is only useful for copyrights;

Returns a MercatorProjection instance. When talking about MercatorProjection, you can refer to Google's tile engine explain, which will be described in detail in the theoretical chapter;

Initializes Overlays.

Although I don't know much about it yet, it doesn't matter. I didn't know much about it at the very beginning, but I can see that basically all providers are writing like this, just like a gourd. The only thing to note is that the name here is XXXProviderBase,

The reason is that the map has two modes: satellite and common map, so there must be some common methods.

    public abstract class BaiduMapProviderBase : GMapProvider    {        public BaiduMapProviderBase()        {            MaxZoom = null;            RefererUrl = "http://map.baidu.com";            Copyright = string.Format("{0} Baidu 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;            }        }    }

 

2 ).BaiduMapProvider

Add BaiduMapProvider, inherit from BaiduMapProviderBase, and implement the GetTileImage method. GetTileImage obtains the image based on the current mouse information pos and zoom ratio.

GetTileImageUsingHttp (url) obtains the Image Based on the url. OK. Now we can construct a url Based on the Baidu map loading principle.

The Code is as follows:

 public class BaiduMapProvider : BaiduMapProviderBase    {        ....public override PureImage GetTileImage(GPoint pos, int zoom)        {            string url = MakeTileImageUrl(pos, zoom, LanguageStr);            return GetTileImageUsingHttp(url);        }        string MakeTileImageUrl(GPoint pos, int zoom, string language)        {            zoom = zoom - 1;            var offsetX = Math.Pow(2, zoom);            var offsetY = offsetX - 1;            var numX = pos.X - offsetX;            var numY = -pos.Y + offsetY;            zoom = zoom + 1;            var num = (pos.X + pos.Y)%8 + 1;            var x = numX.ToString().Replace("-", "M");            var y = numY.ToString().Replace("-", "M");            //http://q3.baidu.com/it/u=x=721;y=209;z=12;v=014;type=web&fm=44            string url = string.Format(UrlFormat, num, x, y, zoom, "014", "web", "44");            Console.WriteLine("url:" + url);            return url;        }        static readonly string UrlFormat = "http://q{0}.baidu.com/it/u=x={1};y={2};z={3};v={4};type={5}&fm={6}";            }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.