博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过经纬度获取地址信息之百度地图API
阅读量:2492 次
发布时间:2019-05-11

本文共 4930 字,大约阅读时间需要 16 分钟。

环境:Visual Studio 2013  C++

语言:C++

百度地图API

  1. 申请密钥
  2. 填写信息
  3. 创建应用(IP白名单记得填0.0.0.0/0)
  4. 查看应用
  5. 复制访问应用(AK)

  1. 知道自己的AK (E4805d16520de693a3fe707cdc962045)
  2. 知道对应转换的地址链接并了解格式中的含义

/************************************************************************//*                            网页操作		                            *//************************************************************************/// query locale place namechar* 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;}

注:以上函数只是给出了大概的思路。并不能直接复制粘贴使用

函数大概思路流程

  1. 动态创建可用的 URL 链接
  2. 访问对应的 URL 链接并将返回的数据,尽数拷贝到 本地临时文件
  3. 获取 本地临时文件 有效信息

你可能感兴趣的文章
OpenGL ES 3.0(四)图元、VBO、VAO
查看>>
OpenGL ES 3.0(五)纹理
查看>>
OpenGL ES 3.0(八)实现带水印的相机预览功能
查看>>
OpenGL ES 3.0(九)实现美颜相机功能
查看>>
FFmpeg 的介绍与使用
查看>>
Android 虚拟机简单介绍——ART、Dalvik、启动流程分析
查看>>
原理性地理解 Java 泛型中的 extends、super 及 Kotlin 的协变、逆变
查看>>
FFmpeg 是如何实现多态的?
查看>>
FFmpeg 源码分析 - avcodec_send_packet 和 avcodec_receive_frame
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>
MongoDB基本语法和操作入门
查看>>
学习笔记_vnpy实战培训day04_作业
查看>>