环境:Visual Studio 2013 C++
语言:C++
百度地图API接口:
坐标转换API接口文档:
/************************************************************************/
/* 网页操作 */
/************************************************************************/
// query locale place name
char* Gps_xjkg::QueryLocale(void)
{
char PlaceName[256] = {};
memcpy(PlaceName, CreateURL(),sizeof(PlaceName));
AccessURL(PlaceName);
GetURLStruct();
CString placename(this->PlaceName);
if (smps[KEY_PLACENAME_INFO])
printf("本地地名:%s\n", placename);
return PlaceName;
}
/*==========================================================
* Function : CreateURL
* Description: 根据经纬度创建http请求链接
* Input :
* Return : const char* URL 链接http地址
==========================================================*/
char* Gps_xjkg::CreateURL(void)
{
char ctemp[256] = {};
// add Baidu Base URL
strcat_s(ctemp, Baidu_GPS);
// add Baidu secret key
strcat_s(ctemp, "/?ak=");
strcat_s(ctemp, ak);
// add longitude and latitude
strcat_s(ctemp, "&location=");
char tempaddr[32] = {};
memcpy(tempaddr, dtoc(Rmc.GPS_Latitude), sizeof(tempaddr));
strcat_s(ctemp, tempaddr);
strcat_s(ctemp, ",");
memcpy(tempaddr, dtoc(Rmc.GPS_Longitude), sizeof(tempaddr));
strcat_s(ctemp, tempaddr);
return ctemp;
}
/*==========================================================
* Function : AccessURL
* Description: 链接百度地图API获取XML信息
* Input : const string URL 链接http地址
* Return : bool 失败返回false
==========================================================*/
#pragma comment(lib, "wininet.lib")
bool Gps_xjkg::AccessURL(const std::string URL)
{
URL_COMPONENTS crackedURL;
char szBuffer[1024] = {};// 缓冲区
TCHAR szHostName[128] = {};
TCHAR szUrlPath[256] = {};
ZeroMemory(&crackedURL, sizeof (URL_COMPONENTS));
crackedURL.dwStructSize = sizeof (URL_COMPONENTS);
crackedURL.lpszHostName = szHostName;
crackedURL.dwHostNameLength = sizeof(szHostName);
crackedURL.lpszUrlPath = szUrlPath;
crackedURL.dwUrlPathLength = sizeof(szUrlPath);
std::wstring wstrURL;
StringToWstring(wstrURL, URL);
InternetCrackUrl(wstrURL.c_str(), wstrURL.length(), 0, &crackedURL);
FILE* file = NULL;
fopen_s(&file, "XJKG_GPS.xml", "wb");
HINTERNET hInt, hConn, hReq;
//启用HTTP建立并创建URL请求
hInt = InternetOpen(_T("Microsoft Internet Explorer"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
hConn = InternetConnect(hInt, crackedURL.lpszHostName, crackedURL.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
hReq = HttpOpenRequest(hConn, _T("GET"), crackedURL.lpszUrlPath, NULL, _T(""), NULL, 0, 0);
char buff[64] = {};
DWORD dwLen = 0;
BOOL bResult = FALSE;
DWORD nBytesGet = 0;
BOOL bEnd = FALSE;
DWORD dwRetCode = 0;
DWORD dwSizeOfRq = sizeof(DWORD);
dwSizeOfRq = sizeof(buff);
if (HttpSendRequest(hReq, NULL, 0, NULL, 0)
&& HttpQueryInfo(hReq, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL)
&& dwRetCode < 400)
bResult = TRUE;
hReq = HttpOpenRequest(hConn, _T("GET"), crackedURL.lpszUrlPath, NULL, _T(""), NULL, 0, 0);
if (HttpSendRequest(hReq, NULL, 0, NULL, 0)
&& HttpQueryInfo(hReq, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_CONTENT_ENCODING, &dwRetCode, &dwSizeOfRq, NULL)
&& dwRetCode < 400)
bResult = TRUE;
while (TRUE)
{
if (bResult)
{
// Begin
bResult = InternetReadFile(hReq, szBuffer, sizeof(szBuffer), &dwLen);
if (bResult)
{
nBytesGet += dwLen;
if (dwLen == 0)
{
bEnd = TRUE;
break;
}
// szBuffer
fwrite(szBuffer, 1, dwLen, file);
}
}
else // end
break;
}
fclose(file);
return true;
}
/*==========================================================
* Function : GetURLStruct
* Description: 获取地址,并将地址放入类变量 PlaceName 中
* Input :
* Return : char *dest 失败返回false
==========================================================*/
bool Gps_xjkg::GetURLStruct(void)
{
FILE* pfile = NULL;
fopen_s(&pfile, "XJKG_GPS.xml", "r");
if (!pfile){
if (smps[KEY_ERROR_INFO])
printf("error: open file happen error.\n");
return false;
}
char cbuff[1024] = {};
int size = fread(cbuff, 1024, 1, pfile);
std::string str_begin = "";
std::string str_end = " ";
// 字段首位
char *addr_begin = NULL;
addr_begin = strstr(cbuff, str_begin.c_str());
addr_begin += str_begin.length();
// 字段末尾
char *addr_end = strstr(cbuff, str_end.c_str());
if (addr_end < addr_begin)
{
fclose(pfile);
if (smps[KEY_ERROR_INFO])
printf("error: GetURLStruct exception!\n");
return false;
}
// 转换字段
memset(this->PlaceName, '\0', sizeof(this->PlaceName));
int pos = 0;
while (addr_begin != addr_end)
{
if (*addr_begin > 0){
this->PlaceName[pos++] = *addr_begin++;
continue;
}
char temp[4] = {};
for (int i = 0; i < 3; ++i, addr_begin++)
temp[i] = *addr_begin;
this->PlaceName[pos++] = Getplacename(temp);
}
fclose(pfile);
return true;
}
注:以上函数只是给出了大概的思路。并不能直接复制粘贴使用
函数大概思路流程: