一個基於百度地圖的案頭軟體(附工程源碼)

來源:互聯網
上載者:User

,我們要訪問百度的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;}

以上是核心內容,就這麼簡單,下面貼個圖:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.