;
,我們要訪問百度的webservice介面。
<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>
搜尋函數,包含兩方面內容:調用js介面,在地圖上進行標註;調用webservice介面,在列表中顯示資訊。(這兩點沒太大關係,只是兩種不同的方式)
//搜尋,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;}
以上是核心內容,就這麼簡單,下面貼個圖: