光阴似箭,日月如梭,恍然间,2013就这样平平淡淡的过去了。想想工资卡上的余额,心中不免浮起蛋蛋的忧桑。如果再回到2013。。。。。罢了,青春就是如此,每个人都肆无忌惮的挥霍着,在年华正当之时。或许只有当岁月的烙印狠狠落下的那一刻,我们才会想起那再也回不去的美好年华,那落英缤纷的流年似水。
----------------------------------------------------------------------华丽分割线------------------------------------------------------------------------------------------
以上纯属扯淡,下面言归正传。本文介绍用Qt实现对百度地图的调用。
1.涉及到的点
Qt调用javascript代码(在Qt代码中调用百度js api);
调用百度webservice api(在Qt代码中调用百度webservice api);
解析返回的json串。
2.准备工作
到百度开发者中心申请key,要带着key,百度地图才能让咱调起来;
下载并编译jsoncpp,我们要用它解析json,请自行度娘;
准备http请求工具类,可参考新浪微博开放平台研究-实现微博自动评论(下),我们要访问百度的webservice接口。
3.实现
map.html文件,Qt中利用qwebview控件加载此html文件显示百度地图。
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的key"></script> <title></title> </head> <body> <div id="allmap"></div> </body> </html> <script type="text/javascript"> var map = new BMap.Map("allmap"); map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); map.addControl(new BMap.NavigationControl()); map.addControl(new BMap.ScaleControl()); map.addControl(new BMap.OverviewMapControl()); map.enableScrollWheelZoom(); map.setCurrentCity("北京"); //qt代码中要调用此方法,在地图上进行标注,key是搜索关键字,area是搜索地址 function search(key,area) { var local = new BMap.LocalSearch(map, { renderOptions:{map: map, autoViewport:true} }); local.searchNearby(key, area); } </script>
//搜索,1:在地图上标注(调用js api),2:列表显示(调用webservice api) void BMap::search() { QString key = this->edit_key->text(); QString area = this->edit_area->text(); //调用js方法search QString method = QString("search(\"%1\", \"%2\")").arg(key).arg(area); QWebFrame *frame = webview->page()->mainFrame(); frame->evaluateJavaScript(method); //根据地名获取经纬度 QString code = this->geocode(area); HttpClient * http = new HttpClient(); QUrl url; //圆形区域内搜索 url.setUrl("http://api.map.baidu.com/place/v2/search"); url.addQueryItem("query",key); url.addQueryItem("output","json"); url.addQueryItem("ak","你的key"); url.addQueryItem("location",code); url.addQueryItem("radius","1000"); QNetworkRequest request; request.setUrl(url); QString ret = http->get(request); list_result->clearContents(); Json::Reader reader; Json::Value value; //解析json if (reader.parse(ret.toStdString(), value)) { value = value["results"]; list_result->setRowCount(value.size()); for (int i=0; i<value.size(); i++) { std::string name = value[i]["name"].asString(); std::string address = value[i]["address"].asString(); QTableWidgetItem *item = new QTableWidgetItem(QString::fromStdString(name)); this->list_result->setItem(i,0,item); item = new QTableWidgetItem(QString::fromStdString(address)); this->list_result->setItem(i,1,item); } } delete http; }
//根据地名获取经纬度 QString BMap::geocode(const QString &area) { QString ret; HttpClient * http = new HttpClient(); QUrl url; url.setUrl("http://api.map.baidu.com/geocoder/v2/"); url.addQueryItem("address",area); url.addQueryItem("city",tr("北京市")); url.addQueryItem("output","json"); url.addQueryItem("ak","你的key"); QNetworkRequest request; request.setUrl(url); QString retstr = http->get(request); Json::Reader reader; Json::Value value; if (reader.parse(retstr.toStdString(), value)) { value = value["result"]; value = value["location"]; ret+=QString::number(value["lat"].asDouble()); ret+=","; ret+=QString::number(value["lng"].asDouble()); } delete http; return ret; }
猛击下载源码(我看有人评论说编译不过,我机器上编译运行都没问题,如果你是搞软件的,相信你让它编译过只是分分钟的事儿)