1、核心代码
/**************************************************************************************
* Project: MapWindow Open Source (MapWinGis ActiveX control)
**************************************************************************************
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at http://www.mozilla.org/mpl/
* See the License for the specific language governing rights and limitations
* under the License.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**************************************************************************************
* Contributor(s):
* (Open source contributors should list themselves and their modifications here). */
// BaiduProjection.h
#pragma once
#include "BaseProvider.h"
#include "BaiduProjection.h"
#include <math.h>
class BaiduBaseProvider: public BaseProvider
{
protected:
CString Styles;
public:
BaiduBaseProvider()
{
_minZoom = 4;
_maxZoom = 19;
_refererUrl = "http://maps.baidu.com/";
_licenseUrl = "https://mapwingis.codeplex.com/wikipage?title=baidutiles";
int year = Utility::GetCurrentYear();
_copyright.Format(L"Copyright @ %d, Baidu,All Rights Reserved", year);
_urlFormat = "http://p3.map.gtimg.com/maptilesv2/%d/%d/%d/%d_%d.png?version=20130701";
//"http://online%d.map.bdimg.com/tile/?qt=tile&x=%s&y=%s&z=%d&styles=%s";
_projection = new BaiduProjection();//MercatorProjection();
}
CString MakeTileImageUrl(CPoint &pos, int zoom)
{
int offsetX = pow((double)2, zoom-1);
int offsetY = offsetX - 1;
int numX = pos.x - offsetX;
int numY = -pos.y + offsetY;
CString sNumX;
sNumX.Format("%d", numX);
sNumX.Replace("-", "M");
CString sNumY;
sNumY.Format("%d", numY);
sNumY.Replace("-", "M");
CString s;
// int x = pos.x;
// int y = pos.y;
// int z = zoom;
// y = pow(double(z), 2) - 1 - y;
//s.Format(UrlFormat, zoom, floor((double)x/16), floor((double)y/16), x, y);
s.Format(_urlFormat, GetServerNum(pos, 3), sNumX, sNumY, zoom, Styles);
return s;//"http://online1.map.bdimg.com/onlinelabel/?qt=tile&x=709&y=216&z=12&styles=pl&udt=20151031&scaler=1&p=1";
// 原来:http://q4.baidu.com/it/u=x=12730;y=4778;z=16;v=016;type=web&fm=44
// 更新:http://online1.map.bdimg.com/tile/?qt=tile&x=23144&y=6686&z=17&styles=pl
}
};
class BaiduMapProvider: public BaiduBaseProvider
{
public:
BaiduMapProvider()
{
Styles = "pl";
Id = tkTileProvider::BaiduMaps;
Name = "BaiduMaps";
_subProviders.push_back(this);
_urlFormat = "http://online%d.map.bdimg.com/tile/?qt=tile&x=%s&y=%s&z=%d&styles=pl";
//"http://online%d.map.bdimg.com/onlinelabel/?qt=tile&x=%s&y=%s&z=%d&styles=sl&v=083&udt=20150815&p=1";
}
CString MakeTileImageUrl(CPoint &pos, int zoom)
{
// pos.x = 554;
// pos.y = 401;
// zoom = 10;
int offsetX = pow((double)2, zoom-1);
int offsetY = offsetX - 1;
int numX = pos.x - offsetX;
int numY = -pos.y + offsetY;
CString sNumX;
sNumX.Format("%d", numX);
sNumX.Replace("-", "M");
CString sNumY;
sNumY.Format("%d", numY);
sNumY.Replace("-", "M");
CString s;
// int x = pos.x;
// int y = pos.y;
// int z = zoom;
// y = pow(double(z), 2) - 1 - y;
//s.Format(UrlFormat, zoom, floor((double)x/16), floor((double)y/16), x, y);
s.Format(_urlFormat, GetServerNum(pos, 8), sNumX, sNumY, zoom, Styles);
return s;//"http://online1.map.bdimg.com/onlinelabel/?qt=tile&x=709&y=216&z=12&styles=pl&udt=20151031&scaler=1&p=1";
// 原来:http://q4.baidu.com/it/u=x=12730;y=4778;z=16;v=016;type=web&fm=44
// 更新:http://online1.map.bdimg.com/tile/?qt=tile&x=23144&y=6686&z=17&styles=pl
}
};
class BaiduSatelliteProvider: public BaiduBaseProvider
{
public:
BaiduSatelliteProvider()
{
//maxZoom = 13;
Styles = "sate";
Id = tkTileProvider::BaiduSatellite;
Name = "BaiduSatellite";
_urlFormat = "http://shangetu%d.map.bdimg.com/it/u=x=%s;y=%s;z=%d;v=009;type=%s&fm=46";
_subProviders.push_back(this);
}
CString MakeTileImageUrl(CPoint &pos, int zoom)
{
int offsetX = pow((double)2, zoom-1);
int offsetY = offsetX - 1;
int numX = pos.x - offsetX;
int numY = -pos.y + offsetY;
CString sNumX;
sNumX.Format("%d", numX);
sNumX.Replace("-", "M");
CString sNumY;
sNumY.Format("%d", numY);
sNumY.Replace("-", "M");
CString s;
// int x = pos.x;
// int y = pos.y;
// int z = zoom;
// y = pow(double(z), 2) - 1 - y;
//s.Format(UrlFormat, zoom, floor((double)x/16), floor((double)y/16), x, y);
s.Format(_urlFormat, GetServerNum(pos, 3), sNumX, sNumY, zoom, Styles);
return s;//"http://online1.map.bdimg.com/onlinelabel/?qt=tile&x=709&y=216&z=12&styles=pl&udt=20151031&scaler=1&p=1";
// 原来:http://q4.baidu.com/it/u=x=12730;y=4778;z=16;v=016;type=web&fm=44
// 更新:http://online1.map.bdimg.com/tile/?qt=tile&x=23144&y=6686&z=17&styles=pl
}
};
class BaiduSatelliteHybridProvider: public BaiduBaseProvider
{
public:
BaiduSatelliteHybridProvider()
{
//maxZoom = 13;http://shangetu2.map.bdimg.com/it/u=x=7;y=M1;z=5;v=009;type=sate&fm=46&udt=20150601
Styles = "sl";
Id = tkTileProvider::BaiduSatelliteHybrid;
Name = "BaiduSatelliteHybrid";
_urlFormat = "http://online%d.map.bdimg.com/onlinelabel/?qt=tile&x=%s&y=%s&z=%d&styles=%s&v=083&udt=20150815&p=0";
_subProviders.push_back(this);
//http://shangetu2.map.bdimg.com/it/u=x=42;y=16;z=8;v=009;type=sate&fm=46&udt=20150601 // 卫星底图
//http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=47&y=16&z=8&styles=sl&v=083&udt=20150815&p=0 路线图
}
};
2、效果图
![](https://img-blog.csdnimg.cn/20210226165227880.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTIxNTY4NzI=,size_16,color_FFFFFF,t_70)